A Learning Platform

SUCCESS

Success = Discipline + Consistency + Sacrifice.
Discipline - in what you do.
Consistency - in what you do.
Sacrifice - for what you do.

                   -- Manjunatha C. P. --

Armstrong Number: C program

C Program to find the Armstrong Number

Armstrong number is a number which is equal to the Sum of cubes of individual digits of that number.

Example:

0, 1, 153, 370, 371 and so on.

Let’s consider an example of 153.

1*1*1 = 1
5*5*5 = 125
3*3*3 = 27
1+125+27=153

Description:

num – Input number which is to be checked for Armstrong number.
rem – Remainder
sum – To store the sum of cubes of each digit.
t – Stores the copy of original number ‘num’.

Algorithm to check for Armstrong number

STEP 1: START
STEP 2: INITIALIZE the value of sum to zero
              sum = 0
STEP 3: READ a number to check for Armstrong number
              READ num
STEP 4: Take the COPY of input number
              temp <- num
STEP 5: REPEAT until greater than zero
              LOOP until num > 0
              rem = num mod 10
              sum = sum + (rem)3
              num = num / 10
STEP 6: CHECK for Armstrong number
              if temp = sum
              PRINT “It is an Armstrong number”
              else
               PRINT “It is not an Armstrong number”
STEP 7: END

C Program to check for Armstrong number

#include<stdio.h>  
#include<conio.h>
 void main()    
{    
    int num,rem,sum=0,t;    
    printf("Enter a number: ");    
    scanf("%d",&num);    
    t=num;    
    while(num>0)    
    {    
        rem=num%10;    
        sum=sum+(rem*rem*rem);    
        num=num/10;    
    }    
    if(t==sum)    
    printf("It is an Armstrong  number ");    
    else    
    printf("It is not an armstrong number");    
    getch();  
}   

Output 1:

Enter a number: 153
It is an Armstrong number

Output 2:

Enter a number: 123
It is not an armstrong number

QUIZ:

1] % operator calculates/returns
  1. quotient
  2. remainder
  3. divisor
  4. dividend