+ 2
what is output of this code ? //code for sum of first 10 odd numbers using //for loop .. verify and it is correct or //incorr
public class oddnumbers { public static void main(String[] args) { int sum=0; for(int i=1;i<20;i=i+2) { sum=sum+i; } System.out.println(sum); } }
11 Respostas
+ 2
output is 100
+ 2
now this code is corrected and is comes output
+ 1
Good
+ 1
out put 100
+ 1
100
+ 1
100
0
90
0
Your code can be improved by using the modulo operator (%) for deciding whether a number is odd or not. The way you did it now seems its doing what you want but its not the way to approach it because it can be error prone.
0
100
0
correct
0
100 is the answer dude