C Program to Check Armstrong Number

C Program to Check whether entered number is Armstrong Number or not

here is an Example to check whether an integer entered by the user  is an Armstrong number or not using while loop and if…else statement.

C Program to Check Armstrong Number
C Program to Check Armstrong Number

Before going to write the c program to check whether the number is Armstrong or not, let’s understand what is Armstrong number.

Armstrong number is a number that is equal to the sum of cubes of its digits. in other words An Armstrong number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself.  For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.

as it is often asked is exams to write a program to check weather the entered number is Armstrong or not

so let us understand with the help of  examples then we will check its code

Examples:

  • 7 = 7^1
  • 371 = 3^3 + 7^3 + 1^3 (27 + 343 +1)
  • 153 =  1^3 + 5^3 + 3^3 = 1 + 125 + 27 
  • 8208 = 8^4 + 2^4 +0^4 + 8^4 (4096 + 16 + 0 + 4096).
  • 1741725 = 1^7 + 7^7 + 4^7 + 1^7 + 7^7 + 2^7 +5^7 (1 + 823543 + 16384 + 1 + 823543 +128 + 78125)

Let’s check the c program to check Armstrong Number in C

#include <stdio.h>;
#include <math.h>;
void main()
{
 int number, sum = 0, rem = 0, cube = 0, temp;
 printf ("enter a number : - ");
 scanf("%d", &amp;amp;number);
 temp = number;
 while (number != 0)
 {
 rem = number % 10;
 cube = pow(rem, 3);
 sum = sum + cube;
 number = number / 10;
 }
 if (sum == temp)
 printf ("The given no is armstrong");
 else
 printf ("The given no is not a armstrong");
getch();
}

Output:

enter the number=153
The given no is armstrong
enter the number=5
The given no is not a armstrong

live demo :-    Download code : –

 

Check out these related examples:
check year is leap or not

 

3
Leave a Reply

avatar
2 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
anurag batraJenni Brunton Recent comment authors
  Subscribe  
Notify of
Jenni Brunton
Guest
Jenni Brunton

Hey, you used to write wonderful, but the last several posts have been kinda boring… I miss your tremendous writings. Past few posts are just a little bit out of track! come on!

trackback
Google

Google

Every as soon as inside a while we pick blogs that we read. Listed beneath are the latest web sites that we pick.