This is a C Program to print armstrong number from 1 to 1000.
Problem Description
This C Program print armstrong number from 1 to 1000.
Problem Solution
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. Hence 153 because `1^3 + 5/3 + 3^3 = 1 + 125 + 27 = 153.`
Program/Source Code
/*
* C Program to Print Armstrong Number from 1 to 1000
*/
#include
main()
{
int number, temp, digit1, digit2, digit3;
printf("Print all Armstrong numbers between 1 and 1000:\n");
number = 001;
while (number .... Show More
This is a C Program to print armstrong number from 1 to 1000.
Problem Description
This C Program print armstrong number from 1 to 1000.
Problem Solution
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. Hence 153 because `1^3 + 5/3 + 3^3 = 1 + 125 + 27 = 153.`
Program/Source Code
/*
* C Program to Print Armstrong Number from 1 to 1000
*/
#include
main()
{
int number, temp, digit1, digit2, digit3;
printf("Print all Armstrong numbers between 1 and 1000:\n");
number = 001;
while (number <= 900)
{
digit1 = number - ((number / 10) * 10);
digit2 = (number / 10) - ((number / 100) * 10);
digit3 = (number / 100) - ((number / 1000) * 10);
temp = (digit1 * digit1 * digit1) + (digit2 * digit2 * digit2) + (digit3 * digit3 * digit3);
if (temp == number)
{
printf("\n Armstrong no is:%d", temp);
}
number++;
}
}
Program Explanation
In this C program, we are printing the Armstrong number from 1 to 1000. 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. Hence, 153 because `1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`. We are initializing the number variable value as 001.
Using while loop checks the condition that the value of number variable is less than or equal to 900. If the condition is true then, execute the iteration of the loop. The digit1 variable is used to compute the division of the value of number variable by 10 and multiply the resulting value by 10 and subtract the multiplied value with the value of number variable.
The digit2 variable is used to compute the division of the value of number variable by 100, multiply the resulting value with 10, divide the value of number variable by 10 and subtract the multiplied value from the divided value.
The digit3 variable is used to compute the division of the value of number variable by 100 and also by 1000, then multiply the value divided by 100 with 10 and subtract both the values. The temp variable is used to multiply the value of digit1, digit2, digit3 variables to the power of 3 respectively. Compute the summation of all the three multiplied values. If condition statement is used to check the value of temp and number variables are equal, if the condition is true print the Armstrong number.
Output:
Print all Armstrong numbers between 1 and 1000:
Amstrong no is:1
Amstrong no is:153
Amstrong no is:370
Amstrong no is:371
Amstrong no is:407