Armstrong Number in C++
Q. Write a C++ program to check Armstrong Number or Not.
#include <iostream>
using namespace std;
int main()
{
int n,r,sum=0,t;
cout << "Enter the number: ";
cin >> n;
t=n;
while(t>0)
{
r=t%10;
sum=sum+(r*r*r);
t=t/10;
}
if(n==sum)
{
cout << n " is armstrong number.";
}
else
{
cout << n " is not armstrong number.";
}
return 0;
}
Q. Write an algorithm to check Armstrong Number or Not.
1. Start
2. Input: Read integer number `n`.
3. Initialize:
- `sum = 0`
- `t = n` (copy of the original number)
4. Process each digit of the number:
- While `t > 0`:
a. Extract the last digit: `r = t % 10`
b. Add the cube of the digit to `sum`: `sum = sum + (r * r * r)`
c. Remove the last digit from `t`: `t = t / 10` (integer division)
5. Check if the number is Armstrong:
- If `sum == n`, then
- Output: "`n` is an Armstrong number."
- Else
- Output: "`n` is not an Armstrong number."
6. 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