Armstrong Numbers in C Program
Armstrong Number : The Armstrong Numbers is a Number. Such That the Sum ! of it's digits raised to the third power is equal to the number ! itself.
For Example : Take A Armstrong Number Like 153
1 3 + 53 +33 = 1+ 125 + 27 = 153 // Here Power of 3 is number of digits in Given number.
Program For Armstrong Number Find in c Program
#include <stdio.h>
int power(int, int);
int main()
{
int n, sum = 0, temp, remainder, digits = 0;
printf("Enter an integer\n");
scanf("%d", &n);
temp = n;
while (temp != 0)
{
digits++;
temp = temp/10;
}
temp = n;
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
printf("%d is an Armstrong number.\n", n);
else
printf("%d is not an Armstrong number.\n", n);
return 0;
}
int power(int n, int r)
{
int c, p = 1;
for (c = 1; c <= r; c++)
p = p*n;
return p;
}
Outputs
Program For Armstrong Number Find in c Program
#include <stdio.h>
int power(int, int);
int main()
{
int n, sum = 0, temp, remainder, digits = 0;
printf("Enter an integer\n");
scanf("%d", &n);
temp = n;
while (temp != 0)
{
digits++;
temp = temp/10;
}
temp = n;
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
printf("%d is an Armstrong number.\n", n);
else
printf("%d is not an Armstrong number.\n", n);
return 0;
}
int power(int n, int r)
{
int c, p = 1;
for (c = 1; c <= r; c++)
p = p*n;
return p;
}
Outputs
No comments:
Post a Comment