C Program To Check Armstrong number or Not
Armstrong number(after Michael F. Armstrong) or a plus perfect number)
Armstrong is a number that is the sum of its own digits each raised to the power of
the number of digits.
Example :
Given Number : 153 (It is a Three Digit Number)
=> (1*1*1) + (5*5*5) + (3*3*3) -> Sum of Cubes of its digits
=> 1 + 125 + 27
=> 153
153 is an Armstrong Number
Example :
Given Number : 371 (It is a Three Digit Number)
=> (3*3*3) + (7*7*7) + (1*1*1) -> Sum of Cubes of its digits
=> 27 + 343 + 1
=> 371
371 is an Armstrong Number
Example :
Given Number : 1634 (It is a Three Digit Number)
=> (1*1*1*1) + (6*6*6*6) + (3*3*3*3)+(4*4*4*4) -> sum of digits each raised to the power of
the number of digits.
=> 1 + 81 + 1296 + 256
=> 1634
1634 is an Armstrong Number
you can use the following algorithm:
- Read the input number.
- Create a copy of the input number.
- Initialize a variable sum to 0.
- While the number is not equal to 0, perform the following steps:
- Get the last digit of the number by using the modulo operator (%).
- Raise the last digit to the power three (3).
- Add the result to the sum.
- Divide the number by 10 to remove the last digit.
- Check if the sum is equal to copy of the original number.
- If they are equal, the number is an Armstrong Number.
Otherwise, it is not. - Print the result.
In this algorithm raises each digit to the power of three(3) and adds them to the sum. Finally, it checks if the sum is equal to the copy of the original number to determine if it is an Armstrong Number.
Please note that: this implementation assumes that the input number is a positive integer. You may need to add additional checks for negative numbers or other cases depending on your requirements.
Source Code
// C Program To Check Armstrong number or Not (Three Digit Number Only)?
#include<stdio.h>
#include<conio.h>
void main()
{
int num , Rd , sum = 0 , ncopy;
printf("Enter a Number To Check : ");
scanf("%d",&num);
ncopy = num ;
while ( num!=0 )
{
Rd = num % 10 ;
sum = sum + Rd * Rd * Rd ;
num = num / 10 ;
}
if ( sum==ncopy )
printf("It is an Armstrong Number . ");
else
printf("It is not an Armstrong Number . ");
getch();
}
Output
Sample Input :
Enter a Number To Check : 153
Sample Output:
It is an Armstrong Number
Sample Input :
Enter a Number To Check : 125
Sample Output:
It is Not an Armstrong Number
To Check Any Digit Number Whether it is an Armstrong number or Not ?
To check if a number is an Armstrong or Not in Cyou can use the following algorithm:
- Read the input number.
- Create a copy of the input number.
- Initialize a variable sum and count to 0.
- Calculate the number of digits in the input number.
While the number is not equal to 0, perform the following steps: - Iterate through the given number, dividing it by 10 until it becomes 0.
- Increment a counter variable for each iteration.
- Reset the number to the copy of original value.
- While the number is not equal to 0, perform the following steps:
- Get the last digit of the number by using the modulo operator (%).
- Raise the last digit to the power of the number of digits.
- Add the result to the sum.
- Divide the number by 10 to remove the last digit.
- Check if the sum is equal to copy of the original number.
- If they are equal, the number is an Armstrong Number.
Otherwise, it is not. - Print the result.
In this algorithm, the input number is processed to calculate the number of digits using a loop that divides the number by 10 until it becomes 0. Then, the algorithm raises each digit to the power of the number of digits and adds them to the sum. Finally, it checks if the sum is equal to the copy of the original number to determine if it is an Armstrong Number.
Please note that: this implementation assumes that the input number is a positive integer. You may need to add additional checks for negative numbers or other cases depending on your requirements.
Source Code
// To Check Any Digit Number Whether it is Armstrong number or Not ?
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num , count=0;
int Rd , sum = 0 , ncopy;
printf("Enter a Number To Check : ");
scanf("%d",&num);
ncopy = num ;
// To Count Number of Digit of a Given Number
while( num!=0 )
{
num = num / 10;
count++;
}
num=ncopy;
// To Find Sum of Its own digits each raised to the power of
// The number of digits.
while ( num!=0 )
{
Rd = num % 10 ;
sum = sum + pow(Rd,count);
num = num / 10 ;
}
if ( sum==ncopy )
printf("It is an Armstrong Number . ");
else
printf("It is not an Armstrong Number . ");
getch();
}
Output
Sample Input :
Enter a Number To Check : 1634
Sample Output:
It is an Armstrong Number
Sample Input :
Enter a Number To Check : 123
Sample Output:
It is Not an Armstrong Number
Looping Related Programs
- C Program to Check Perfect Number or Not
- C Program to Check Prime Number or Not
- C Program to Check Twin Prime Number or Not
- C Program to Check Duck Number or Not
- C Program to Check Niven Or Harshad Number or Not
- C Program to Check Spy Number or Not
- C Program to Check Palindrome Number or Not
- C Program to Check Neon Number or Not
- C Program to Check Strong (Krishnamurthy)Number or Not
- C Program to Check Happy Number or Not
- C Program to Check Automorphic Number or Not