C Program to Check whether a given Number is Armstrong:
This is a C Program to check whether a given number is armstrong.
Problem Description
This C Program checks whether a given number is armstrong number.
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 Check whether a given Number is Armstrong
*/
#include
#include
void main()
{
int number, sum = 0, rem = 0, cube = 0, temp;
printf ("enter a number");
scanf("%d", &number);
temp = number;
while (number != 0)
{
rem = number % .... Show More
This is a C Program to check whether a given number is armstrong.
Problem Description
This C Program checks whether a given number is armstrong number.
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 Check whether a given Number is Armstrong
*/
#include
#include
void main()
{
int number, sum = 0, rem = 0, cube = 0, temp;
printf ("enter a number");
scanf("%d", &number);
temp = number;
while (number != 0)
{
rem = number % 10;
cube = pow(rem, 3);
sum = sum + cube;
number = number / 10;
}
if (sum == temp)
printf ("The given no is armstrong no");
else
printf ("The given no is not a armstrong no");
}
Program Explanation
In this C program, we are reading the integer value using number variable. An Armstrong number is an n-digit base b number, such that the sum of its digits raised to the power n is the number itself. Hence, 153 because `1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.`
Using while loop checks the value of number variable is not equal to 0. If the condition is true, execute the iteration of the loop. The rem variable is used to compute the modulus of the value of number variable by 10 and cube variable is used to compute the cube of the value of rem variable using pow().
Then sum variable is used to compute the summation of the value of sum variable with the value of cube variable. The If-else condition statement is used to check both the value of sum variable and the value of temp variable are equal. If the condition is true, then it will print Armstrong number. Otherwise, it will execute the else condition statement and print not Armstrong number.
Output:
enter a number370
The given no is armstrong no
enter a number1500
The given no is not a armstrong no
show less