Q5 Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each digit of the number equal to the number itself, then the number is called an Armstrong number. For example, 153 = (1*1*1)+(5*5*5)+(3*3*3)

Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each digit of the number equal to the number itself, then the number is called an Armstrong number. For example, 153 = (1*1*1)+(5*5*5)+(3*3*3)

Program: 98

Write a c program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each digit of the number equal to the number itself, then the number is called an Armstrong number. For example, 153 = (1 * 1 * 1) + (5 * 5 * 5) + (3 * 3 * 3)

#include<stdio.h>
#include<conio.h>
#include<conio.h>
int main()
{
    int num, temp, rem=0, sum=0, i;
    float cube;

    num = 1;
    temp = 1;
    for(i=1;i<=500;i++)
    {
        while(num!=0)
        {
            rem = num%10;
            cube = pow(rem,3); //or we can write (rem*rem*rem)
            sum = sum + cube;
            num = num/10;
        }   //HERE THE VALUE OF num = 0
        if(sum == temp)
            printf("%d\n",temp);

        //set default values to the variables
        rem = 0;
        sum = 0;
        cube = 0;
        //increment temp and num value according to i
        //for first loop i = 1
        temp = i+1;
        num = i+1;
    }

}

Output:

 1
 153
 370
 371
 407

Lokesh Kumar: Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.

View Comments (2)

  • i think it is wrong beacuse as we can see other then one . 2,3,4,5,6,7,8,9 are also an armstrong number but in output it is only showing 1 as armstrong number in 1 number digit.

    • other numbers like 2, 3, 4, 5, etc. are not armstrong numbers because the rule of armstrong number is that the cube of each digit is equal to the number. Example: 153, 1 cube + 5 cube + 3 cube = 153 which is correct. But for 2, 2 cube is 8 which is not equal to 2 hence its not an armstrong number.

Related Post
Leave a Comment

This website uses cookies.