Examples of C part 25 print armstrong number till n

Program :

#include<stdio.h>
#include<math.h>
main()
{
  int num,i,j,temp1,temp2,sum=0;;
  printf("Enter a number to know all armstrong number between them\n");
  scanf("%d",&num);
  printf("Armstrong numbers are:\n");
  for(i=1;i<=num;i++)
  {
    sum=0;
    temp1=i;
    temp2=i;
    while(temp1>0)
    {
      j=temp1%10;
      sum+=pow(j,3);
      temp1=temp1/10;
    }
    if(sum==temp2)
    {
      printf("%d\n",sum);
    }
  }
}
Output:
Enter a number to know all armstrong number between them
1000
Armstrong numbers are:
1
153
370
371
407
Explanation:
  1. The program starts with initializing :
    • i → used as a variable
    • j → used variable
    • temp1 → to store value of number fetching from 'i' for future reference.
    • temp2 → to store value of number fetching from 'i' for future reference.
    • num → To store user input
    • sum → To store the final output initialized with zero
  2. printf("Enter a number to know all armstrong
            number between them\n");
    scanf("%d",&num);
    Used to take input from user say (num=1000)
  3. for(i=1;i<=num;i++)
    {
    sum=0;
    temp1=i;
    temp2=i;
    To traverse for 1 to num(1000 from example) to obtain all armstrong numbers one by one by checking every number from 1 to 1000(num).
    • temp1,temp2 stores the value of 'i' which will generate number to check whether they are armstrong or not.If the i value is '1' as it is armstrong then 1 will be printed.Same as 370 and 371 else it won't print.
  4. while(temp1>0)
    {
    j=temp1%10;
    sum+=pow(j,3);
    temp1=temp1/10;
    }
    if(sum==temp2)
    {
    printf("%d\n",sum);
    }
    This is the main logic written inside for loop
    • Iteration 1 of forloopi=1;temp1=i →temp1=1;temp2=i →temp2=1;
      • Iteration 1 of while loop as temp1>0 so the loop executes
        • j=temp1%10 → 1%10 → 1
        • sum=sum+pow(j,3) → 0+1^3 →sum=1
        • temp1=temp1/10 →1/10 →0 →temp1=0
        • final values after 1st iteration of while loop i=1,temp2=1,j=1,sum=1,temp1=0
      • Iteration 2 of while loop:-Now temp1 is not >0 so the loop terminates
      • Now compiler checks sum==temp2 or not where temp2 stores the original value of number which we wanted to check for armstrong number. As sum=1,temp2=1.Condition proves to be true so the 1 is printed
    • Iteration 2 of forloopi=2;temp1=i →temp1=2;temp2=i →temp2=2;
      • Iteration 1 of while loop as temp1>0 so the loop executes
        • j=temp1%10 → 2%10 → 2
        • sum=sum+pow(j,3) → 0+2^3 →sum=8
        • temp1=temp1/10 →2/10 →0 →temp1=0
        • final values after 1st iteration of while loop i=2,temp2=2,j=2,sum=8,temp1=0
      • Iteration 2 of while loop:-Now temp1 is not >0 so the loop terminates
      • Now compiler checks sum==temp2 . As sum=8,temp2=2.Condition proves to be false so if condition is not executed and then moves to next step.
    • Step 2 which is iteration 2 of forloop will not print 3,4,5,6,...152 as they are not armstrong numbers.
    • The above will go on until next Armstrong is detected which is 153.Lets see how it will print 153 as armstrong number.
    • Iteration 153 of forloop: i=153;temp1=i →temp1=153;temp2=i →temp2=153;
      • Iteration 1 of while loop as temp1>0 so the loop executes
        • j=temp1%10 → 153%10 → 3
        • sum=sum+pow(j,3) → 0+3^3 →sum=27
        • temp1=temp1/10 →153/10 →15 →temp1=15
        • final values after 1st iteration of while loop i=153,temp2=153,j=3,sum=27,temp1=15
      • Iteration 2 of while loop:-15>0 which is true and while loop is executed
        • j=temp1%10 → 15%10 → 5
        • sum=sum+pow(j,3) → 27+5^3 →sum=27+125 →152
        • temp1=temp1/10 →15/10 →1 →temp1=1
        • final values after 1st iteration of while loop i=153,temp2=153,j=5,sum=152,temp1=1
      • Iteration 3 of while loop:-1>0 which is true and while loop is executed
        • j=temp1%10 → 1%10 → 1
        • sum=sum+pow(j,3) → 152+1^3 →sum=152+1→153
        • temp1=temp1/10 →1/10 →0 →temp1=0
        • final values after 1st iteration of while loop i=153,temp2=153,j=1,sum=153,temp1=0
      • Iteration 4 of while loop:-0>0 which is not true so while loop is terminated
      • Now compiler checks sum==temp2 or not. As sum=153,temp2=153.Condition proves to be true so the 153 is printed
    • The same way as above continues till 1000 and prints all the armstrong numbers which are remaining 370,371,407.