how i print 10 number sum

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bizzaro
    New Member
    • Mar 2012
    • 1

    how i print 10 number sum

    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.prin tln(a+b+c+d+e+f +q+w+r+t);
    }

    i must print 10 numbers sum and its correct?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Don't declare ten integers to take the input. Use a for loop and add the values into a previously initialized variable.

    Comment

    • rekedtechie
      New Member
      • Feb 2012
      • 51

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

      Comment

      Working...