Help with program for odd numbers

Hello, i searched the forums but couldn't find my answer.
I have a code already made that finds the cube of all numbers from 0-30.
I was given the code and told to make it only odd numbers instead of all numbers from 0-30.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int numb; 
for(numb=0; numb<=30; numb++) 
{
cout << setw(4) << numb; 
int cube = numb*numb*numb; 
cout << setw(6) << cube << endl; 
}
system("PAUSE");
return 0;
}
Last edited on
I was given the code and told to make it only odd numbers instead of all numbers from 0-30.

Just so I'm clear, you mean that this new program is to only find the cube of odd numbers? Change your for loop to iterate over odd numbers only (ie, starting at one and incrementing by two for each iteration of the loop).
@Danny Toledo is right!
or you can do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int numb; 
for(numb=0; numb<=30; numb++) 
{

if(numb%2!=0){
cout << setw(4) << numb; 
int cube = numb*numb*numb; 
cout << setw(6) << cube << endl;
}//end if 
}
system("PAUSE");
return 0;
}
I believe you would change the numb=0 to numb=1, and change the numb++ meaning keep adding 1 to numb+2 or numb++2 but as I am relatively new to programming there's a good chance I am wrong and apologize if so.
I should have mentioned this, my bad. But i did that.
The real problem for me is getting to start at 0 and doing all odd numbers till 30 (Or 29 in this case)
@Danny Toledo is right!

Psh, I'm always right.

numb+2 or numb++2

It would be numb += 2

@OP
Start at 0? Then you are probably best off using @eyenrique's solution.
Got it, thanks guys!
Also my internet lagged so i apologies that i replied after everyone posted!
Thank you very much
Topic archived. No new replies allowed.