# c++ program



## sai_cool (Aug 14, 2007)

Hey,

can u guys tell me how to create a cpp program to print all the armstrong numbers within a given limit.?


----------



## RCuber (Aug 14, 2007)

Is thats a assignment given by your school/college ?


----------



## Zeeshan Quireshi (Aug 14, 2007)

sai_cool said:
			
		

> Hey,
> 
> can u guys tell me how to create a cpp program to print all the armstrong numbers within a given limit.?


that is for you to do mate , or else you'll fail your computer exam(most probably) if you can't make ven these simple programs .

Well here's the main logic:
1. extract each digit , be taking modulus from 10(i.e. num % 10) or string operators if you're usin java n treatin as string.
2. then square that digit
3. add all squares
4. check if result is same as original number
5. if yes then number is armstrong , else not 

Hope it helped


----------



## Sykora (Aug 14, 2007)

^^^ Armstrong is sum of cubes, not squares.


----------



## Garbage (Aug 14, 2007)

and create a function which returns 1 if number is Armstrong & otherwise 0.
Use a for loop in main() to pass numbers one by one to that function & if it returns 1, then print that number as an Armstrong !!!
got it ??


----------



## utsav (Aug 14, 2007)

i can make it using while loop as it is for a given limit of numbers.

its a school assignment .ICSE or ISC .right


----------



## slugger (Aug 14, 2007)

d00ds let d thread starter show us wat hes done and den v cud go abt correctin it instead of just tellin him how 2 do it [code/alg/flow]

let us not encourage members who want 2 use d forum to get his/her hw done by some 1 else


----------



## The_Devil_Himself (Aug 14, 2007)

I think he is stuck somewhere.And dudes you know that programming skills increases most when you read and understands codes written by others.
I don't know c++(i have just started it) but I think following c code should work for you.Note that I haven't checked if its running or not but I think its ok(atleast the logic is ok).
ok here it is:

void main()
{
int n,i,p,r,sum=0;
printf("Enter the no.:\n");
scanf("%d",&n);
for(i=0;i<=n;i++)
{r=i;
 while(r!=0)
{p=r%10;
r=r/10;
sum=sum+p*p*p;
}
if(sum==i)
printf("%d\t",sum);
sum=0;
}
getch();
clrscr();
}





CHECKED,IT'S WORKING


----------



## abhishek_del (Aug 15, 2007)

he wanted it in c++....these days they dont teach c before teaching c++...so I think u shud explain the printf scanf commands or use cin cout....coz i too dont know the printf scanf commands


----------



## Garbage (Aug 15, 2007)

@ The_Devil_Himself

U should NOT told him the code. But u did !!! huh...


----------



## Zeeshan Quireshi (Aug 15, 2007)

@ The_Devil_Himself

Dud that's C not C++


----------



## The_Devil_Himself (Aug 15, 2007)

Zeeshan Quireshi said:
			
		

> Dud that's C not C++



Dude please read the full post first.I SAID I don't know c++ yet cos I have just started using it but I know c and I guess the logic is the same.


----------



## shady_inc (Aug 16, 2007)

Sykora said:
			
		

> ^^^ Armstrong is sum of cubes, not squares.



Nope armstrong nos. are the ones in which the sum of each individual digit in a no. raised to the no. of digits equals the original no.So for three digit no.,it will be sum of cubes.....For a two digit no.,it will be sum of squares.....

Every single digit no. except zero is armstorng no.Hava look here. for more info.


----------



## mehulved (Aug 16, 2007)

According to *mathworld.wolfram.com/NarcissisticNumber.html , shady_inc is right.


----------



## Sykora (Aug 16, 2007)

I guess I never got interested in them enough to go past 999


----------



## Zeeshan Quireshi (Aug 16, 2007)

The_Devil_Himself said:
			
		

> Dude please read the full post first.I SAID I don't know c++ yet cos I have just started using it but I know c and I guess the logic is the same.


That's why i informed you mate , so u know what is C++ and what is C , no harm intended .


----------



## RCuber (Aug 16, 2007)

That guy "sai_cool" posted this question and never came back  



> Time Posted :  14-08-2007   08:29 PM
> Last Activity:  14-08-2007   08:31 PM


----------



## Garbage (Aug 16, 2007)

^^ U should say "Not logged in" bcoz he may be in Guests


----------



## The_Devil_Himself (Aug 19, 2007)

shady_inc said:
			
		

> Nope armstrong nos. are the ones in which the sum of each individual digit in a no. raised to the no. of digits equals the original no.So for three digit no.,it will be sum of cubes.....For a two digit no.,it will be sum of squares.....
> 
> Every single digit no. except zero is armstorng no.Hava look here. for more info.



Thanks buddy I didn't know this.But still the code can be used with slight modifications.

Anyways I prefer  fibonacci series over armstrong series....lols


----------

