Sunday, 4 March 2018

Arrays in java User input,Display Array using foreach loop & sum of array





package javaapplication9;



import java.util.Scanner;





public class JavaApplication9 {



 

    public static void main(String[] args)

    {

        Scanner obj=new Scanner(System.in);

            int sum=0,a=0;   

        int []x=new int[5];

       

        for (int i = 0; i < x.length; i++)

        {

            System.out.println("Enter the number at index "+i);

            x[i]=obj.nextInt();

           

           

        }

         System.out.println("-------Printing the values--------");     

        for(int c:x)

        {

            System.out.println(c);

        }

  while(a<x.length)

  {

  sum=sum+x[a];

  a++;

 

  }

     System.out.println("-------Sum of the Array--------");     

        System.out.println("sum = "+sum);

       

       

    }

   

}


No comments:

Post a Comment

Pass Dynamically Added Html Table Records List To Controller In Asp.net MVC

Controller Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ...