Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,080 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,599 people online right now. Registration is fast and FREE... Join Now!




c++ armstrong number

 
Reply to this topicStart new topic

c++ armstrong number, tell me urgently

Rating  5
fahad g
12 Apr, 2008 - 12:50 PM
Post #1

New D.I.C Head
*

Joined: 12 Apr, 2008
Posts: 1

how kan i make the c++ program in which user kan enter any number (of any digits) and then the program tells that this is armstrong number or not...
i have made the program for 3 digits..pls tell me how kan i modify this to infinite number of digits???its important for me pl tell me now...
the program which i made for 3 digit number is:
CODE
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c,d,e,f;
cout<<" AMSTRONG NUMBER \n";
cout<<"\n";
cout<<"enter a number=";
cin>>a;
b=a%10;
c=a/10;
d=c/10;
e=c%10;
b==b*b*b;
d==d*d*d;
e==e*e*e;
f=b+d+e;
if (a=f)
{
cout<<"AMSTRONG NUMBER";
}
else
{
cout<<"wrong numbeR!try aGain!";
}
getch();
}

waiting...

*Mod Edit: added code tags: code.gif
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: C++ Armstrong Number
12 Apr, 2008 - 08:25 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
let me see... Armstrong Number - the sum of the cube of its digits equals the number...

To get a number's bottom digit you use:

digit1 = number % 10;
then number = number / 10; to remove that digit
digit2 = number % 10;
number = number / 10;
digit3 = number % 10;

Ok... now we just have to check:

cube1 = digit1 * digit1 *digit1; or we could use pow(digit1, 3)
cube2 = digit2 * digit2 *digit2;
cube3 = digit3 * digit3 *digit3;

if (cube1 + cube2 + cube3 == originalNumber) then smile.gif else sad.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: C++ Armstrong Number
12 Apr, 2008 - 08:26 PM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8525
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
QUOTE(fahad g @ 12 Apr, 2008 - 01:50 PM) *

its important for me pl tell me now...


A little demanding aren't we?
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: C++ Armstrong Number
12 Apr, 2008 - 08:30 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
oopse my bad... you had the three digit version working

Well to modify this to a general case you just need to add the above logic to a loop:

total = 0
BEGIN:
digit = number % 10;
number = number / 10;
cube = digit * digit * digit;
total += cube;
LOOP if number > 0
if total = original number then smile.gif else sad.gif

QUOTE(PsychoCoder @ 12 Apr, 2008 - 09:26 PM) *

QUOTE(fahad g @ 12 Apr, 2008 - 01:50 PM) *

its important for me pl tell me now...


A little demanding aren't we?


yea... I noticed that too...
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:34PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month