Armstrong number In C

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




1. C program to check whether a number is Armstrong or not

#include<stdio.h>
#include<conio.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





2. C program for Armstrong number generation

#include<stdio.h>
#include<conio.h>

int main()
{
    int num,r,sum,temp;
    int min,max;

    printf("Enter the minimum range: ");
    scanf("%d",&min);

    printf("Enter the maximum range: ");
    scanf("%d",&max);

    printf("Armstrong numbers in given range are: ");
    for(num=min;num<=max;num++)
    {
         temp=num;
         sum = 0;

         while(temp!=0)
         {
             r=temp%10;
             temp=temp/10;
             sum=sum+(r*r*r);
         }
         if(sum==num)
             printf("%d ",num);
    }
    return 0;
}

Sample output:
Enter the minimum range: 1
Enter the maximum range: 200
Armstrong numbers in given range are: 1 153




3. Armstrong number in c using for loop

#include<stdio.h>
#include<conio.h>

int main()
{
    int num,r,sum=0,temp;

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

    for(temp=num;num!=0;num=num/10)
    {
         r=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: 370
370 is an Armstrong number
Logic of Armstrong number in c




4. C program to print Armstrong numbers from 1 to 500

#include<stdio.h>
#include<conio.h>

int main()
{
    int num,r,sum,temp;

    for(num=1;num<=500;num++)
    {
         temp=num;
         sum = 0;

         while(temp!=0)
         {
             r=temp%10;
             temp=temp/10;
             sum=sum+(r*r*r);
         }
         if(sum==num)
             printf("%d ",num);
    }

    return 0;
}

Output:
1 153 370 371 407