C++ program to find the Armstrong number between the given range
This is a program that finds the Armstrong number between the given range input from user.
Enter first number of range:
Enter second number of range:
Armstrong numbers are like 371=3^3+7^3+1^3
Steps
User side:
1: User asked to enter starting number of interval
2: User asked to enter last number of interval
3: User displayed Armstrong number between that interval .
Developer side
1: Declare variables that are going to be used in program.
2: Input starting number of interval from user (input in an int variable)
3: Input last number of interval from user (input in an int variable)
3: Start a loop from first number to last number.
4: In the loop initialize a variable 'o' to zero and initialize a variable 'n' with the counter of the loop 'i' .
5: Start another while loop until the variable n is greater than zero.
6: In the inner loop place the following statements m=n%10;
o=o+m*m*m;
n=n/10;
7: Inner loop ends here.
8: Check if the counter is equal to the variable 'o'
9: If condition is true display the counter (these are the Armstrong numbers).
10: increase the counter.
11: Program ends here.
8: Check if the counter is equal to the variable 'o'
9: If condition is true display the counter (these are the Armstrong numbers).
10: increase the counter.
11: Program ends here.
Code:
#include <iostream>
using namespace std;
void main()
{ //variable
declaration
int n, m, o,
firstnumber, secondnumber, i;
//heading
cout<<"\t\t\tArmstrong
numbers"<<endl;
//entring of range
cout << "Enter
starting number of interval = ";
cin >> firstnumber;
cout << "Enter
last number of interval. = ";
cin >> secondnumber;
cout<<"The
following numbers:"<<endl;
i=firstnumber;
while(
i<=secondnumber)
{
o=0;
n=i;
while(n>0)
{
m=n%10;
o=o+m*m*m;
n=n/10;
}
if(o==i)
{
cout
<< i << endl;
}
i++;
}
cout
<< "are armstrong numbers between "<<firstnumber<<" and "<< secondnumber<<
endl;
system("pause");
}
Output:
Comments
Post a Comment