Ques:-15) Write a program to print out all Armstrong numbers between 1 to 500. If sum of cubes of each digit of the number is 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).

here is a C program that prints all Armstrong numbers between 1 and 500:

				
					#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int op,temp,last,i;
printf("Armstrog number is:\n");
for(i=1;i<=500;i++)
{
op=0;
temp=i;
while(temp>0)
	{
	last=temp%10;
	temp=temp/10;
	op=op+last*last*last;
	}
if(op==i)
	{
	printf("%d\n",i);
	}
}
getch();
}

				
			

Out Put

				
					Armstrong numbers
1
153
370
371
470
				
			

Another Example

				
					#include <stdio.h>
#include <math.h>

// Function to check if a number is an Armstrong number
int isArmstrong(int num) {
    int originalNum, remainder, result = 0;

    originalNum = num;

    while (originalNum != 0) {
        remainder = originalNum % 10;
        result += pow(remainder, 3);
        originalNum /= 10;
    }

    // If the result is equal to the number, it is an Armstrong number
    if(result == num) {
        return 1;
    }
    return 0;
}

int main() {
    printf("Armstrong numbers between 1 and 500 are:\n");
    for(int i = 1; i <= 500; i++) {
        if(isArmstrong(i)) {
            printf("%d\n", i);
        }
    }
    return 0;
}

				
			

Explanation:

  1. isArmstrong Function:

    • This function takes an integer as input and checks if it is an Armstrong number.
    • It calculates the sum of the cubes of its digits and checks if this sum is equal to the original number.
  2. Main Function:

    • The main function iterates through all numbers from 1 to 500.
    • For each number, it calls the isArmstrong function to check if it is an Armstrong number.
    • If the number is an Armstrong number, it prints the number.

Output:

When you run this program, it will print all Armstrong numbers between 1 and 500. The expected output is:

				
					Armstrong numbers between 1 and 500 are:
1
153
370
371
407

				
			

You can compile and run this program using a C compiler to see the results.

Scroll to Top