+ 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

26th Jan 2021, 3:44 PM
Andrei Macovei
Andrei Macovei - avatar
32 Respostas
+ 4
Benjamin Jürgens your code is really simple, good and exactly as you described above.
26th Jan 2021, 9:25 PM
JaScript
JaScript - avatar
+ 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
26th Jan 2021, 5:13 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 2
What Problem you have in this code it is working and printing values
26th Jan 2021, 4:20 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
The idea is my input is 5 so I should have 5 numbers and my program show just 3
26th Jan 2021, 4:21 PM
Andrei Macovei
Andrei Macovei - avatar
+ 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.
26th Jan 2021, 4:21 PM
JaScript
JaScript - avatar
+ 2
Not really it s for beginners this problem and it s request just a while loop
26th Jan 2021, 4:25 PM
Andrei Macovei
Andrei Macovei - avatar
+ 2
I give an example Input :3 Output:1 3 5
26th Jan 2021, 4:26 PM
Andrei Macovei
Andrei Macovei - avatar
+ 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
26th Jan 2021, 4:30 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 2
Syrah Algena no array is needed here
26th Jan 2021, 4:33 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
26th Jan 2021, 5:17 PM
JaScript
JaScript - avatar
+ 2
You can also use a for loop starting at 1 and a step of 2.
27th Jan 2021, 8:07 PM
Sonic
Sonic - avatar
+ 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
26th Jan 2021, 4:24 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
JaScript as I understood, example is really 2 examples: input => output 5 => 1 3 5 7 9 3 => 1 3 5
26th Jan 2021, 4:35 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
Benjamin Jürgens its depend on your approach i thought array is much better
26th Jan 2021, 4:36 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
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;}
26th Jan 2021, 3:48 PM
Andrei Macovei
Andrei Macovei - avatar
0
Yes exactly but I don't know how to implement
26th Jan 2021, 4:36 PM
Andrei Macovei
Andrei Macovei - avatar
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; }
26th Jan 2021, 4:37 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
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
26th Jan 2021, 4:51 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
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
26th Jan 2021, 4:59 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
0
Ok and how to do that?
26th Jan 2021, 5:04 PM
Andrei Macovei
Andrei Macovei - avatar