Armstrong Number in C
Q. Write a C program to check Armstrong Number or Not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0,t;
clrscr();//clear the screen
printf("Enter the number: ");
scanf("%d",&n);
t=n;
while(t>0)
{
r=t%10;
sum=sum+(r*r*r);
t=t/10;
}
if(n==sum)
{
printf("%d is armstrong number.",n);
}
else
{
printf("%d is not armstrong number.",n);
}
}
Q. Write an algorithm to check Armstrong Number or Not.
1. Start
2. Prompt the user to enter a number and read the number into variable `n`.
3. Initialize:
- `sum` to 0 (to store sum of cubes of digits)
- `t` to `n` (to preserve original number for comparison)
4. While `t` is greater than 0, repeat the following steps:
- Compute `r` = `t % 10` (extract the last digit)
- Compute `r^3` (cube of the digit)
- Add `r^3` to `sum`
- Update `t` to `t / 10` (remove the last digit)
5. After the loop ends, compare `sum` with the original number `n`.
6. If `sum` equals `n`, then
- Print that `n` is an Armstrong number.
Else
- Print that `n` is not an Armstrong number.
7. End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com