C006 A C program to find all Armstrong numbers in the range of 0 to 999

By | August 8, 2013

An Armstrong number of three digits is an number 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.

Let’s identify variables needed for this program. one will be for FOR Loop that is i, other is to save the equation value in it that is NUM, others are to hold the single digit of the number such as 000 to 999. For example 371 is the number so its first digit is 3, second digit is 7, and third digit is 1, Hence  to calculate cube of single digit we need three variables (i.e.FIRST_DGT,SECOND_DGT,THIRD_DGT) and one TEMP for temporary variable So in all we need six variables

Now, Selection of data type is int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient.

In this program there is a requirement of Checking the values from 000 to 999 numbers. It indicates LOOP is to be taken. Best Loop for such kind of condition is FOR Loop.
Loop selected is FOR Loop.

Now give the user a message to ARMSTRONG NUMBERS BETWEEN 0 TO 999 ARE : .FOR Loop will be like this for(i=0;i<999;i++). For Loop will start with 0 to 999. To identify whether the number is Armstrong number or not. We have to divide that number for getting three separate digits in the given number(i).

To find the first digit we have to divide the number by 100  (FIRST_DGT=i/100;)

To find the third digit we have to divide the number by 10 and save the remainder with the help of modulus (THIRD_DGT=i%10;)

nd the second digit which is complex because it is in middle of other digit, we have to divide the number by 10 the save it in TEMP and then divide TEMP by 10 we get middle digit  (TEMP=i/10;   SECOND_DGT=TEMP%10;)

Now, calculating equation  NUM=FIRST_DGT**3 + SECOND_DGT**3 + THIRD_DGT**3. We have to use another header file math.h  inside which there is a pow function which will help us to power of the number passed as second argument that is 3. So the equation formed is NUM = pow(FIRST_DGT,3) + pow(SECOND_DGT,3) + pow(THIRD_DGT,3);

The if condition will be i==NUM to check whether the calculated value is equal to number ranging form 000 to 999 in FOR Loop. If the calculated value is equal to number ranging between 000 to 999, we will go for printing the number as output values which is the Armstrong number at the end of this program.

  1. #include<stdio.h>
  2. #include<math.h>
  3. void main()
  4. {
  6. clrscr();
  7. printf("ARMSTRONG NUMBERS BETWEEN 0 TO 999 ARE : ");
  8. for(i=0;i<=999;i++)
  9. {
  10. FIRST_DGT=i/100;
  11. THIRD_DGT=i%10;
  12. TEMP=i/10;
  14. NUM=pow(FIRST_DGT,3)+pow(SECOND_DGT,3)+pow(THIRD_DGT,3);
  15. if(i==NUM)
  16. printf("\nNUMBER = %d",NUM);
  17. }
  18. getch();
  19. }

screen shots:-




Note:- To understand program for sequence in detail Please SEARCH numerically example: C001, C002, etc.

Leave a Reply