 A problem using while loop | Sololearn: Learn to code for FREE!

+2

# A problem using while loop

Hey guys I try to resolve this problem Requirements: A natural number N is entered from the keyboard.The first N numbers that are odd are displayed in ascending order Restrictions and specifications : 0<N<1000000 Exemples: Input data: 5,3 Output data 1 3 5 7 9 1 3 5

+4

Benjamin Jürgens your code is really simple, good and exactly as you described above.

+3

> Ok and how to do that? Take the code you have written and change it according to what i wrote. I think my description is already a detailed instruction and straight forward to follow

+2

What Problem you have in this code it is working and printing values

+2

The idea is my input is 5 so I should have 5 numbers and my program show just 3

+2

A hint for you Andrei Macovei : as I undestood your imput are two integers separated with comma not one number. So have to receive two numbers. How, try to find out it.

+2

Not really it s for beginners this problem and it s request just a while loop

+2

I give an example Input :3 Output:1 3 5

+2

It says print first N odd numbers, not odd numbers up to N. So either let nr count 1 2 3 ... N and figure out the mathematical operations to apply to each nr to get output 1 3 5 ... or start at 1, increase in steps of 2 and figure out when you have to stop

+2

Syrah Algena no array is needed here

+2

+2

You can also use a for loop starting at 1 and a step of 2.

+1

Andrei Macovei for this problm u need to use array u can easily do it first define a number how much u want to enter then take user input how many values u want to print

+1

JaScript as I understood, example is really 2 examples: input => output 5 => 1 3 5 7 9 3 => 1 3 5

+1

Benjamin Jürgens its depend on your approach i thought array is much better

0

I tried that : #include <iostream> using namespace std; int main(){ int n,nr; cin>>n; while(nr<=n){ nr++; if(nr%2==1) cout<<nr; } return 0;}

0

Yes exactly but I don't know how to implement

0

Here i have used predefined element if u want u can take from user use cin with for loop #include <iostream> using namespace std; int main(){ int n,i=0; int array[]={1, 2, 3, 4, 5, 6 ,7 ,9 ,11, 13}; cout<<"how many numbers u want to print\n"; cin>>n; while(i<n){ if(array[i]%2==1) { cout<<array[i]<<" "; } i++; } return 0; }

0

Syrah Algena 0<N<1,000,000 Do you really want to write that array down? And if you fill it in a loop, then output in another loop, you can instead just output directly

0

Andrei Macovei you don't have to change much, just follow one of my suggestions. I'd prefer the first. Delete the if statement and instead output in every iteration, but not nr but the odd number calculated from nr 1=>1, 2=>3, 3=>5, 4=>7, 5=>9, ... 100=>199 It's just basic math

0