|
|
|
#1 (permalink) |
|
New to Overclock.net
|
I have a basic program that asks the user to enter a number and then I need to add just the odd numbers of 1 - what ever number the user entered. How would I write the code for this? Just the adding of the odd numbers I have rest of the code.
|
|
|
|
|
|
#2 (permalink) | ||||||||||||
|
New to Overclock.net
|
what language are you writing in?
__________________if it is c++ i just might be able to help you
|
||||||||||||
|
|
|
|
|
#4 (permalink) | ||||||||||||
|
New to Overclock.net
|
#include <iostream>
__________________using namespace std; int main() { cout << "enter your number" << endl; int number = 0; cin >> number; int sum = 0; int i = 1; while(i <= number) { sum += i; i += 2; } cout << sum << endl; system("pause"); return 0; } Threw this toghether for ya, you could also use a for loop but a while loop works just as well
|
||||||||||||
|
|
|
|
|
#6 (permalink) | ||||||||||||||
|
Security Sleuth
![]() |
Id go with a loop and a modulus. Itll reassure whatever you add will be odd.
If you dont understand, a quick google of that will explain it. Assuming you have basic math skills, it shouldnt be a problem. Gluck.
__________________
Quote:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
|
|
#7 (permalink) | ||||||||||||||
|
I am Iron Man
![]() |
Quote:
Oh and make sure you put your "ints" at the beginning just for readability
|
||||||||||||||
|
|
|
|
#8 (permalink) | ||||||||||||||
|
4.0 GHz
![]() |
I really, have no clue about anything that you guys posted. You guys are beast. +REP
__________________
$$$Money Saved for next Upgrade!$$$ $680 Quote:
|
||||||||||||||
|
|
|
|
|
#9 (permalink) | |||||||||||||
|
I am Iron Man
![]() |
Modulii are pretty easy. Just think of the result of a modulus as the remainder of a division operation. So 3 divided by 2 would = 1 mod 1 since 2 goes into 3 once with a remainder of 1.
__________________
|
|||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|