Armstrong number is a number that is equal to the sum of cubes of its individual digits. For example 1, 153, 370, 371 and 407 are the Armstrong numbers.
e.g 153 is Armstrong Number the sum of cube of its individual digits is 153.
cube of 1 is =1
cube of 5 is =125
cube of 3 is=27
sum of cube of individual digits =1+125+27 =153
#include <stdio.h>
/* Write C Program to find all Armstrong numbers from 1 to N using while loop provided by the user.*/
#include <stdio.h>
int main()
{
int r=0, limit, sum=0 ,n=1,n1;
/* Input upper limit from user */
printf("Enter upper limit to find Armstrong number: ");
scanf("%d", &limit);
printf("Armstrong number between 1 to %d are: \n", limit);
while(n<=limit)
{
sum = 0;
/* Calculate sum of power of digits */
n1=n;
while(n1!=0)
{
r=n1%10;
sum+=r*r*r;
n1=n1/10;
}
/* Check for Armstrong number */
if(n == sum)
{
printf("%d, ", n);
}
n++;
}
return 0;
}
Output:
Enter upper limit to find Armstrong number:
407
Armstrong number between 1 to 407 are:
1 153 370 371 407
Program Explanation:
1. In the program given above the variables r=0, limit, sum=0 ,n=1,n1; is declared as integer variable and assigned values.
variable limit is declared as the integer, the value in limit is used as the range to find Armstrong number 1 to limit .
2.
printf("Enter upper limit to find Armstrong number: ");
scanf("%d", &limit);
printf() function prompt the user "Enter upper limit to find Armstrong number:" then control jumps to the input function
scanf("%d",&limit); and wait for the user input.
User inputs/enters number 407 which is get stored(scaned)in variable limit.
printf("Armstrong number between 1 to %d are: \n", limit);
--> displays message Armstrong number between 1 to 407 are:
1 153 370 371 407
Note:Here in this example we just explain working for number 153.
Consider the 153rd iteration of outer while loop.
value of n is 153 i.e. n=153.
let test the condition while (n<=limit) which is true ( while 153<=407)
variable sum=0 and n1=n is initialized , here value of n1 and n is 153.
3.1 control jumps to inner while loop and test the condition while(n1!=0).
i.e. while(153!=0) which results in true, statements inside body of loop
r=n1%10
sum+=r*r*r;
n1=n1/10; is get processed.
r=n1%10 ---> calculates the remainder r=153%10 ,here calculated reaminder 3 is stored in r i.e r=3.
sum+=r*r*r ---> calculate sum of cube of r , here value of sum=27.
n1=n1/10 ---> divide n1 by 10 after calculation here n1 is 15 i.e n1=15.
(153/10 result is 15 not 15.3 because of integer division.)
3.2 control jumps to inner while loop and test the condition while(n1!=0).
i.e. while(15!=0) which results in true, statements inside body of loop
r=n1%10;
sum+=r*r*r;
n1=n1/10; is get processed.
r=n1%10 ---> calculates the remainder r=15%10 ,here calculated reaminder 5 is stored in r i.e r=5.
sum+=r*r*r ---> calculate sum of cube of r add it to sum ,
previous value of sum is 27 ,here value of sum=sum+r*r*r is 152 i.e sum=152
n1=n1/10 ---> divide n1 by 10 after calculation here n1 becomes 1 i.e n1=1.
(15/10 result is 1 not 1.5 because of integer division.)
3.3 control jumps to inner while loop and test the condition while(n1!=0).
i.e. while(1!=0) which results in true, statements inside body of loop
r=n1%10
sum+=r*r*r;
n1=n1/10; is get processed.
r=n1%10 ---> calculates the remainder r=1%10 ,here calculated reaminder 1 is stored in r i.e r=1.
sum+=r*r*r ---> calculate sum of cube of r add it to sum , previous value of sum is 152 ,here value of sum=sum+r*r*r becomes 153 i.e sum=153
n1=n1/10 ---> divide n1 by 10 after calculation here n1 becomes 0 i.e n1=0.
(1/10 result is 0 not 0.1 because of integer division.)
3.4 control jumps to inner while loop and test the condition while(n1!=0).
i.e. while(0!=0) which results in false, control jumps out of inner while loop and start executing stataments inside body of outer while loop.
4. Executes the Statement.
if(n == sum)
{
printf("%d, ", n);
}
here n=153 and sum=153
The condition tested in above if statement is true i.e if(153==153)
then executes statements printf("%d, ", n); inside body of if statement and displays output 153 .
value of n is incremented by 1 i.e. n++, here n becomes 154 i.e n=154
control jumps back to outer loop and test the condition while(n<=limit) which is true then start executing statemets inside body of outer loop.
The process continues to find all Armstrong number between 1 to 407 while value of n not becomes 408 i.e. n=408.
4. finally control come out of outer while loop when the value of n becomes greater than limit. Finally program stop the execution.