Infolet





Java Code to Calculate the Sum of Many Numbers



Java code to calculate Sum of Many Numbers

This is a simple java console program to find the sum of set of numbers. Using this program, we can find the sum of unlimited numbers. At first, this program will accept the total number of numbers. After this, program will accept all numbers from user.
Example: If number are 1, 2, 3 and 4.
Then sum is = 1+2+3+4 = 10

Here class name is ‘sum’, so you should save your file with name ‘sum.java’.
Lean here how to create, compile and run a java application program here.

Program code:


import java.io.*;
class sum
{
 int num; 
 double sum = 0;
 double result;
void findsum() throws IOException
{
  BufferedReader Br = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("\n Mean of how many numbers : ");
    num = Integer.parseInt(Br.readLine());
int nums[] = new int[num+1];
  for(int i = 1;i<=num;i++)
  {
   System.out.print("\n Enter " + i + " number : ");
   nums[i]= Integer.parseInt(Br.readLine());
   sum = sum + nums[i];
  }
  
  System.out.print("\n Sum  is : "+ sum );
}

 public static void main(String s[]) throws IOException
  {
   sum sm = new sum();
   sm.findsum();
  }
}

Sample Output:

Java code to calculate Sum of Many Numbers





Responses

0 Respones to "Java Code to Calculate the Sum of Many Numbers"

Post a Comment

 
Return to top of page Copyright © 2010 | Platinum Theme Converted into Blogger Template by HackTutors GooglePrivacy Policy