Example of Armstrong number
153 = 1*1*1 + 5*5*5 + 3*3*3
An Armstrong number of three digits is the integer number such that the sum of the cubes of its all digits is equal to the number itself.
Program to check Armstrong number
#include <stdio.h>
int getPower(int, int);
int main()
{
int num, tmp, total = 0, remainder, no_of_digits = 0;
printf("Enter integer number: ");
scanf("%d", &num);
tmp = num;
// Count number of digits in given number
while (tmp != 0)
{
no_of_digits++;
tmp = tmp/10;
}
//Logic to get total as per Armstrong formula.
tmp = num;
while (tmp != 0)
{
remainder = tmp%10;
total = total + getPower(remainder, no_of_digits);
tmp = tmp/10;
}
if (total == num)
printf("%d is Armstrong number.", num);
else
printf("%d is not Armstrong number.", num);
return 0;
}
int getPower(int n1, int r1) {
int i, power=1;
for (i=1; i<=r1; i++)
power = power * n1;
return power;
}
Output-1
Enter integer number: 153
153 is Armstrong number.
Output-2
Enter integer number: 125
125 is not Armstrong number.
153 = 1*1*1 + 5*5*5 + 3*3*3
An Armstrong number of three digits is the integer number such that the sum of the cubes of its all digits is equal to the number itself.
Program to check Armstrong number
#include <stdio.h>
int getPower(int, int);
int main()
{
int num, tmp, total = 0, remainder, no_of_digits = 0;
printf("Enter integer number: ");
scanf("%d", &num);
tmp = num;
// Count number of digits in given number
while (tmp != 0)
{
no_of_digits++;
tmp = tmp/10;
}
//Logic to get total as per Armstrong formula.
tmp = num;
while (tmp != 0)
{
remainder = tmp%10;
total = total + getPower(remainder, no_of_digits);
tmp = tmp/10;
}
if (total == num)
printf("%d is Armstrong number.", num);
else
printf("%d is not Armstrong number.", num);
return 0;
}
int getPower(int n1, int r1) {
int i, power=1;
for (i=1; i<=r1; i++)
power = power * n1;
return power;
}
Output-1
Enter integer number: 153
153 is Armstrong number.
Output-2
Enter integer number: 125
125 is not Armstrong number.
No comments:
Post a Comment