PDA

View Full Version : c++ program


sai_cool
14-08-2007, 08:29 PM
Hey,

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

Charan
14-08-2007, 08:36 PM
Is thats a assignment given by your school/college ? :p

Zeeshan Quireshi
14-08-2007, 09:01 PM
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
14-08-2007, 09:15 PM
^^^ Armstrong is sum of cubes, not squares.

Garbage
14-08-2007, 09:47 PM
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
14-08-2007, 09:58 PM
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
14-08-2007, 10:57 PM
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
14-08-2007, 11:31 PM
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
15-08-2007, 12:28 AM
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 :P

Garbage
15-08-2007, 06:50 AM
@ The_Devil_Himself

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

Zeeshan Quireshi
15-08-2007, 08:12 PM
@ The_Devil_Himself

Dud that's C not C++ ;)

The_Devil_Himself
15-08-2007, 08:40 PM
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
16-08-2007, 03:23 PM
^^^ 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. (http://illuminations.nctm.org/LessonDetail.aspx?id=L648) for more info.;)

mehulved
16-08-2007, 04:11 PM
According to http://mathworld.wolfram.com/NarcissisticNumber.html , shady_inc is right.

Sykora
16-08-2007, 07:01 PM
I guess I never got interested in them enough to go past 999 :)

Zeeshan Quireshi
16-08-2007, 07:45 PM
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 .

Charan
16-08-2007, 08:11 PM
That guy "sai_cool" posted this question and never came back :p


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

Garbage
16-08-2007, 09:03 PM
^^ U should say "Not logged in" bcoz he may be in Guests ;-)

The_Devil_Himself
19-08-2007, 01:03 AM
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. (http://illuminations.nctm.org/LessonDetail.aspx?id=L648) 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