Assignment – 7
7. WAP in C to read a number and check the followings:
a. Armstrong Number
b. Krishnamurthy Number
c. Perfect Number
ALGORITHM
1. Start
2. Declare necessary integer variables (n, a, b, c, d, i, sum, choice).
3. Display options to the user for checking different types of numbers.
4. Read the user’s choice and the number.
5. Use switch-case to perform checks:
• If 1: Check if the number is an Armstrong number.
• If 2: Check if the number is a Krishnamurthy number (sum of factorials of digits equals the
number).
• If 3: Check if the number is a Perfect number (sum of divisors equals the number).
6. Display the result accordingly.
7. End
SOURCE CODE
#include <stdio.h>
int main()
{
int n, a, b, c = 0, d, i, sum = 0, choice;
printf("\nCheck:\n1.Armstrong Number\n2.Krishnamurthy Number\n3.Perfect
Number\n");
printf("\nEnter your choice:");
scanf("%d", &choice);
printf("Enter a number: ");
scanf("%d", &n);
switch (choice)
{
case 1:
a = n;
while (n > 0)
{
b = n % 10;
c = (b * b * b) + c;
n = n / 10;
}
if (a == c)
printf("%d is Armstrong Number.", a);
else
printf("%d is not an Armstrong Number.", a);
break;
case 2:
a = n;
while (n > 0)
{
b = n % 10;
d = 1;
for (i = 1; i <= b; i++)
{
d = d * i;
}
sum = d + sum;
n = n / 10;
}
if (a == sum)
printf("%d is a Krishnamurthy Number.");
else
printf("%d is not a Krishnamurthy Number.");
break;
case 3:
a = n;
for (i = 1; i < n; i++)
{
if (n % i == 0)
sum = i + sum;
}
if (a == sum)
printf("The number is a Perfect Number");
else
printf("The number is not a Perfect Number");
break;
default:
printf("\nInvalid Option");
}
return 0;
}
OUTPUT
■Choice 1:
● Case 1:
● Case 2:
■Choice 2:
● Case 1:
● Case 2:
■Choice 3:
● Case 1:
● Case 2:
CONCLUSION
This program allows the user to check whether a number is an Armstrong number,
Krishnamurthy number, or Perfect number. It demonstrates the use of switch-case, loops, and
number manipulation in C.