A Platform to LEARN | CODE | ACHIEVE



Start Coding
Take Challenge
Subscribe us !

Connect :






TO CHECK WHETHER A GIVEN NUMBER IS AN ARMSTRONG NUMBER OR NOT IN C

Code

#include<stdio.h>
int main()
{
 int num;
 int rem=0,sum=0,n=0;
 printf("ENTER THE NUMBER : ");
 scanf("%d",&num);
 n=num;
 while(n>0)
 {
   rem=n%10;
   sum=sum+rem*rem*rem;
   n=n/10;
 }
 if(sum==num)
 {
   printf("IT IS AN ARMSTRONG NUMBER \n");
 }
 else
 {
   printf("IT IS NOT AN ARMSTRONG NUMBER \n");	
 }
 return 0;
}



Output


ENTER THE NUMBER : 153
IT IS AN ARMSTRONG NUMBER


check a number a armstrong number or not

What we did - Our Approach


In this problem we have to check whether a given number is an armstrong number or not.An Armstrong number is an integer 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*3 + 7*7*7 + 1*1*1 = 371. To do so we declare a variable 'sum',which will store the result value(don't forget to initialize it with zero).Then,we update our 'sum' value by adding cube of remainder value(rem*rem*rem) to it in each iteration(until 'n' becomes 0).
After the last iteration,we compare the value of given number and our sum ,if both values are equal then given number is armstrong else it isn't.

Join us :





CODE OF GEEKS

A Platform to LEARN | CODE | ACHIEVE

Connect :



SHARE:



For any Queries,suggestions,feel free to Mail us at :
codeofgeeks@gmail.com

For reporting any kind of copyright voilation,mail us at : cogcopyclaim@gmail.com

© copyright CODE OF GEEKS