Sunday, 1 September 2013

Write a c program to check given number is Armstrong number or not



Write a c program to check given number is Armstrong number or not.

Definition of Armstrong number or what is an Armstrong number:
THOSE NUMBERS WHICH SUM OF THE CUBE OF ITS DIGITS IS EQUAL TO THAT NUMBER ARE KNOWN AS ARMSTRONG NUMBERS. FOR EXAMPLE 153 SINCE 1^3 + 5^3 + 3^3 = 1+ 125 + 9 =153
OTHER ARMSTRONG NUMBERS: 370,371,407 ETC.

IN GENERAL DEFINITION:
THOSE NUMBERS WHICH SUM OF ITS DIGITS TO POWER OF NUMBER OF ITS DIGITS IS EQUAL TO THAT NUMBER ARE KNOWN AS ARMSTRONG NUMBERS.
EXAMPLE 1: 153
TOTAL DIGITS IN 153 IS 3
AND 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

Example 2: 1634
Total digits in 1634 is 4
And 1^4 + 6^4 + 3^4 +4^4 = 1 + 1296 + 81 + 64 =1634
Examples of Armstrong numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725

Code 1:
1. Warp to check a number is Armstrong
2. C program to check whether a number is Armstrong or not
3. Simple c program for Armstrong number
4. Armstrong number in c with output

#include<stdio.h>
int main(){
    int num,r,sum=0,temp;

    printf("Enter a number: ");
    scanf("%d",&num);

    temp=num;
    while(num!=0){
         r=num%10;
         num=num/10;
         sum=sum+(r*r*r);
    }
    if(sum==temp)
         printf("%d is an Armstrong number",temp);
    else
         printf("%d is not an Armstrong number",temp);

    return 0;
}

Sample output:
Enter a number: 153
153 is an Armstrong number

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved