Hi guys,
This is a simple cpp program to check weather a number is amstrong or not.
A number is amstrong if the sum of the cubes of the digits of the number is the same number. Thus as a common example we use the number 153.
Then,
1^3=1
5^3=125
3^3=27
the total will be 1+125+27=153 ,the same number.
In the program, we:
This is a simple cpp program to check weather a number is amstrong or not.
A number is amstrong if the sum of the cubes of the digits of the number is the same number. Thus as a common example we use the number 153.
Then,
1^3=1
5^3=125
3^3=27
the total will be 1+125+27=153 ,the same number.
In the program, we:
- input the number to be checked.
- extract each digits from the number.
- take cubes.
- add up.
- check the result with the given number.
- display the result.
Now try the program given below:
//amstrong
#include<conio.h>
#include<iostream.h>
class dom
{
int dgt,dum,flag;
public:
void get()
{
cout<<"Enter the digit:";
cin>>dgt;
cout<<"done!";
dum=dgt;
}
void check();
void disp()
{
if(flag==1) cout<<"\n Awesome! Amstrong";
else cout<<"\noops! not *_* TRY AGAIN";
}
};
void dom::check()
{
int q,sum=0;
while (dum!=0)
{
q=dum%10;
sum=sum+q*q*q;
dum=dum/10;
}
cout<<"\n\ncubed:"<<sum;
if (sum==dgt) flag=1;
else flag=0;
};
void main()
{
clrscr();
dom obj;
obj.get();
obj.check();
obj.disp();
getch();
}
Good luck.
Comments
Post a Comment