![]() |
|
#1
|
|||
|
|||
how do i find(list them) and sum them up?here is what i have so far...i need to list all the odd number and sum them up at the end....any help please.
CPP / C++ / C Code:
Last edited by LuciWiz : 03-Nov-2005 at 15:27.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|||
|
#2
|
||||
|
||||
Re: how do i find(list them) and sum them up?Hi jack,
Welcome to the GID Forums. Did you read the guidelines? Quote:
the sum is initialized to zero at the start. CPP / C++ / C Code:
Then, to check whether a number is odd or not, we have to use like this: CPP / C++ / C Code:
For example, consider that we have 5. 5 % 2 will be 1. whereas 4 % 2 will be 0. So, the remainder should be 1 for odd number. Now, how to calculate the sum? We can calculate the sum when we count the odd numbers itself. like this: CPP / C++ / C Code:
Here is what you should remove: 1. The extra int odd = 0; You can declare a variable only once. So, you can use int sum = 0; instead. 2. In the for loop: CPP / C++ / C Code:
3. In this line: CPP / C++ / C Code:
In the next line, CPP / C++ / C Code:
Summarising, here is the code CPP / C++ / C Code:
Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
|||
|
|||
Re: how do i find(list them) and sum them up?thanks Paramesh.
|
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The