Print armstrong number between 100 to 999 using One Dimensional Array in C


This program is written in C programming language and it does the following:

  • It first declares some integer variables i, j, n, a, b, c, d, e and an array m
  • The program then prints "Armstrong number between 100 to 999 are:"
  • Then it uses a for loop to iterate through the range of 100 to 999
  • Within the for loop, it separates the digits of each number by using mathematical operations such as integer division (/) and modulus (%).
  • Then it cubes each separated digit and stores the result in variables b, c, and d
  • Next, it adds the cubes of all separated digits and stores the result in variable e
  • Then it uses an if statement to check if the current number is equal to the sum of cubes of its digits, if yes , it increments the value of j and prints the number using printf statement
  • Finally, it prints the total number of Armstrong numbers found and returns 0 as the value of main function.

Source Code

#include<stdio.h>
int main()
{
  int i,j=0,n=999,m[1000],a,b,c,d,e;
  printf("\nArmstromg number betweeen 100 to 999 are:");
  for(i=100;i<n;i++)
  {
    a=i/10;
    b=i%10;
    c=a/10;
    d=a%10;
    b=b*b*b;
    c=c*c*c;
    d=d*d*d;
    e=b+c+d;
    if(i==e)
    {
      j++;
      printf("\nm[%d]=%d",j,i);
    }
  }
  printf("\nTotal number of amstrong no's=%d",j);
  return 0;
}
            
To download raw file Click Here

Output

Armstrong number between 100 to 999
m[1]=153
m[2]=370
m[3]=371
m[4]=407
Total number of armstrong no's=4

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C