I am making a program to check if a number is a Armstrong number or not. A Armstrong number is a number in which the sum of the cube of the digits is equal to the number itself ( In 153 1*1*1+5*5*5+3*3*3=153). Please help. Thanks in advance.
#include<iostream.h>
#include<process.h>
void main()
{
int a,b,c,d,e,f;
cout<<"\t\tAMSTRONG NUMBER(0-999)";
cout<<"\n\nEnter the number=";
cin>>a;
b=a%10;
f=a/10;
c=a%10;
f=f/10;
d=c%10;
e=b*b*b+c*c*c+d*d*d;
if(a==e)
cout<<"\nThis is a Amrstrong number.";
else
cout<<"\nThis is not a Armstrong number.";
}
main should return int, it should be iostream.h also why do you have process included?
As far as your problem what exactly are these supposed to be?
1 2 3 4 5
b=a%10; //right digit
f=a/10; //left 2 digits
c=a%10; //right digit
f=f/10; //arbitrary number divided by 10
d=c%10 //right digit mod 10 = right digit still
Here is how I would do it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
int main()
{
int Armstrong = 0;
std::cout << "Please enter a number: ";
std::cin >> Armstrong;
int right = Armstrong % 10;
int middle = Armstrong / 10 % 10;
int left = Armstrong / 100;
int sum = right * right * right + middle * middle * middle + left * left * left;
std::cout << "The number " << Armstrong << " is " << (sum == Armstrong ? "" : "not") << " an Armstrong number." << std::endl;
return 0;
}
Please enter a number: 152
The number 152 is not an Armstrong number.
...
Please enter a number: 153
The number 153 is an Armstrong number.
Even if you fix your code, it will only work for up to three digits. I seem to recall that the largest Armstrong numbers are less than 1,000, but lets pretend we don't know that. So why not write a function that computes the sum of the cube of the digits:
1 2 3 4 5 6 7 8 9 10
int sumCubeDigits(int num)
{
int result = 0;
while (num) {
int digit = num %10; // get least significant digit
result += digit*digit*digit;
num %= 10; // shift num down one digit. e.g. 123 -> 12
}
return result;
}
Now a number is an Armstrong number if num == sumCubeDigits(num)