Write a c program to check whether a given number is an Armstrong number or not. Armstrong number in c using a while loop and Armstrong number in C using For loop. program of Armstrong number in C.. Logic is very simple you have to have to separate all digits and Find The sum of the cube of digits.
Let's check 371 then divide by 10 we got 371/10=1 .cube of 1 is 1 store this result and again divide by 10 we got 37/10=7. cube of 7 is 343 store and add to previously got result 1 so now it becomes 344, again divide by 10 we got 3/10=3 cube of 3 is 27, Now again add 27+344=371. So the number is Armstrong Number in C Language.
Note: Divide the number by 10 until it becomes Zero
Read: C Program to Check Prime or Armstrong Number Using Function
An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.
Note: Divide the number by 10 until it becomes Zero
Read: C Program to Check Prime or Armstrong Number Using Function
Program of Armstrong Number in C Using For Loop
#include <stdio.h>
void main()
{
int num, r, sum = 0, temp;
printf("Input any number: ");
scanf("%d", &num);
/*Armstrong Number in C Using For Loop*/
for (temp = num; num != 0; num = num / 10)
{
r = num % 10;
sum = sum + (r *r *r);
}
if (sum == temp)
printf("%d is an Armstrong number.\n", temp);
else
printf("%d is not an Armstrong number.\n", temp);
}
Armstrong Number in C Using While Loop
#include <stdio.h>
int main()
{
int n, n1, rem, num = 0;
/*Armstrong Number in C Using While Loop*/
while (1)
{
printf("Enter a Positive Integer To Check For Armstrong : \n");
scanf("%d", &n);
n1 = n;
while (n1 != 0)
{
rem = n1 % 10;
num += rem *rem * rem;
n1 /= 10;
}
if (num == n)
printf("\n%d Is An Armstrong Number.\n\n", n);
else
printf("\n%d Is Not an Armstrong Number.\n\n", n);
}
return 0;
}
0 Comments: