ALGORITHM TO CHECK ARMSTRONG NUMBER
CHECK ARMSTRONG (N)Let N be a three digit Integer. This algorithm checks that whether N is an Armstrong Number or not.
Step 1: Start
Step 2: Take Input N as Integer
Step 3: [ initializing SUM ] Set: SUM = 0
Step 4: Set: Number = N
Step 5: Repeat While Number ≠ 0
SUM = (Number%10)**3 + SUM
Number = Number/10
[ End of While Loop ]
Step 6: [ Checking ? ]
If SUM == N then
Print: N is an Armstrong Number.
Else
Print: N is not an Armstrong Number.
[End of If-Else Structure]
Step 7: Exit
FLOWCHART TO CHECK ARMSTRONG NUMBER
Source Code to Check Armstrong Number
/* C++ program to check whether a number entered by user is Armstrong or not. */
#include <iostream>
using namespace std;
int main()
{
int n, n1, rem, num=0;
cout << "Enter a positive integer: ";
cin >> n;
n1=n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(num==n)
cout << n << " is an Armstrong number.";
else
cout << n << " is not an Armstrong number.";
return 0;
}
No comments:
Post a Comment