+ 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); } }

17th Dec 2016, 2:43 PM
Alaghusooriya EM
Alaghusooriya EM - avatar
11 Respostas
+ 2
output is 100
17th Dec 2016, 3:26 PM
Alaghusooriya EM
Alaghusooriya EM - avatar
+ 2
now this code is corrected and is comes output
18th Dec 2016, 5:58 AM
Alaghusooriya EM
Alaghusooriya EM - avatar
+ 1
Good
18th Dec 2016, 7:40 AM
ASWINI
+ 1
out put 100
18th Dec 2016, 7:42 AM
ASWINI
+ 1
100
18th Dec 2016, 7:43 AM
Alok TheGr8
Alok TheGr8 - avatar
+ 1
100
24th Jan 2017, 4:58 PM
Alok TheGr8
Alok TheGr8 - avatar
0
90
17th Dec 2016, 3:12 PM
Vijeth Belle
Vijeth Belle - avatar
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.
18th Dec 2016, 12:23 AM
Ousmane Diaw
0
100
6th Jan 2017, 12:17 AM
HARIBASKER S
0
correct
12th Jan 2017, 5:17 PM
Pramod Patil
Pramod Patil - avatar
0
100 is the answer dude
24th Jan 2017, 3:17 PM
Anand Chourasiya
Anand Chourasiya - avatar