By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,653 Members | 1,483 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,653 IT Pros & Developers. It's quick & easy.

how i print 10 number sum

P: 1
public static void main(String[] args) {
Scanner toko = new Scanner(System.in);
int a =toko.nextInt();
int b =toko.nextInt();
int c =toko.nextInt();
int d =toko.nextInt();
int e =toko.nextInt();
int f =toko.nextInt();
int q =toko.nextInt();
int w =toko.nextInt();
int r =toko.nextInt();
int t =toko.nextInt();
System.out.println(a+b+c+d+e+f+q+w+r+t);
}

i must print 10 numbers sum and its correct?
Mar 13 '12 #1
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
Don't declare ten integers to take the input. Use a for loop and add the values into a previously initialized variable.
Mar 13 '12 #2

P: 51
int x;
int sum = 0;
for(i=0;i<=10;i++) {
System.out.println("enter a number:";
x = toko.nextInt();
sum = sum + x;
System.out.println("total sum is:"+sum;
}
//if you want to put the total sum at the end of your 10 inputs..
//put System.out.println("total sum is:"+sum;
outside the loop statement.
//it makes the printing method of the total sum will execute after the loop.
Mar 17 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.