0

how do u write a program of first 10 odd numbers using only loops

13th Jul 2016, 7:07 PM
varshith
4 Antworten
+ 2
public class firstten{ public static void main(String args[]){ int b=0; int c=0; for(int i=1;i<=20;i++) { b++; if(b%2==1){ if(c<10){ c++; System.out.println(b); } } } }}
13th Jul 2016, 9:33 PM
Anurag Nigam
Anurag Nigam - avatar
0
Initialize integer oddCounter with 0. Start loop with 1 until let's say until 100. If odd number found, print one and add 1 to oddCounter. If oddCounter is more than 10, break the loop.
14th Jul 2016, 11:47 AM
Richi
0
for(int i = 0; i < 20; i += 2) System.out.println(i);
14th Jul 2016, 6:13 PM
Maxim Kvasnikov
Maxim Kvasnikov - avatar
0
for(int i = 1; i < 20; i += 2) System.out.println(i);
16th Jul 2016, 12:44 PM
Preeti.Bhushania
Preeti.Bhushania - avatar