English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

My friend gave me a problem about C++ for loop statement...and it goes like this:

Output:

Enter Number n: 2

1 3

Basically, if I enter number 2, the first two odd numbers will appear. If I enter 5, the first five odd numbers will appear. As i Have no background about this, I searched the net but I found myself confused. Will you guys help me out? He told me that there is some sort of a code?? Hope you can help me...

2007-02-18 19:20:11 · 3 answers · asked by rebomb 1 in Computers & Internet Programming & Design

I just wanted to know the source code that my friend is talking about...I already know what is odd and even numbers...IT's just i wanna work this problem out....I repeat, i want the source and kindly explain how its done as I am new to this...and it happened that I have no background about programming..this is C++ by the way

2007-02-18 20:12:11 · update #1

3 answers

I have almost forgot C++, but I will try:

Here n is the number that is input.

int oddNum = 1;
while (n > 0) {
cout< n--;
oddNum += 2;
}

2007-02-18 23:47:36 · answer #1 · answered by vin 2 · 0 0

Simple logical thinking is what you need. Stop wasting your time and start investing some time in improving ur logical thinking abilities.

Tip - a number is odd when it can not be divided with 2

2007-02-19 03:49:08 · answer #2 · answered by kvasani 2 · 0 0

I concur. Solve the math problem first, then solve the c++ problem. If you can figure out how to do it atomically by hand, you'll be able to do it in c++.

2007-02-19 04:38:13 · answer #3 · answered by smokingun 4 · 0 0

fedest.com, questions and answers