Java Program to Print Summation of Numbers


There are different ways of adding the numbers in Java. The numbers to be added can be given as input at runtime or can be put directly inside the program. However, it is difficult to add numbers in this way if the list of numbers is too big. Sometimes, the data to be added is given in an excel spreadsheet/ google sheet. So, the easy way is to save the excel spreadsheet /google sheet into a file with extension CSV (Comma Separated Values) and then CSV file’s column can be selected for adding up the values together using Java program. In this article, both the approaches of summations of numbers are specified.

Multiple Approaches

For summation of numbers by the java program, the following two approaches are used.

  • Approach 1: Getting the numbers from the user at runtime.

  • Approach 2: Adding the numbers through a csv file using Java.

Let’s see the programs along with the output one by one.

Approach-1: Getting the numbers from the user at runtime

In this approach, the scanner class will be used to take the user input and write the necessary code to print the computed result.

Algorithm

  • Step 1 − Ask the user how many numbers the user want to add.

  • Step 2 − Ask the user the numbers to be added.

  • Step 3 − Add the entered numbers.

  • Step 4 − Display the sum of numbers.

Example (Approach 1)

import java.util.Scanner;
public class newarr {
   public static void main(String[] args) {
      int nn;
      
      // Scanner is used to take input from the user
      Scanner scnn=new Scanner(System.in);
      System.out.print("Enter the number of items you want to add: ");
      
      //get the input
      nn=scnn.nextInt();
      int[] arrayofNum = new int[10];
      System.out.println("Enter the numbers to be added: ");
      for(int n=0; n<nn; n++){
         arrayofNum[n]=scnn.nextInt();
      }
      int summ = 0;
      
      // adding Numbers
      for (int m = 0; m < arrayofNum.length; m++)
      summ += arrayofNum[m];
      
      // printing the sum
      System.out.println("Sum of given array is "+ summ);
   }
}

Output

Enter the number of items you want to add: 10
Enter the numbers to be added:
200
320
56
67
6768768
5454
67667
7878
78
99
Sum of given array is 6850587

Approach-2: Adding the numbers through CSV file using Java

In this approach, Excel spreadsheet are used. Then as per the algorithm, the program code would fetch the numbers which are presented in the sheet and the addition of the numbers would be displayed in output.

Algorithm

  • Step 1 − Save Excel spreadsheet/ Google sheet in CSV format.

  • Step 2 − Read the CSV file line by line.

  • Step 3 − Skip the header lines.

  • Step 4 − Select a column number where data to be added is present.

  • Step 5 − Convert the String type data into numeric form.

  • Step 6 − Add the numbers of the specified column.

  • Step 7 − Print the result.

CSV file Content

Year,2022
Months,Icecream sales (Rs)
Jan,100034
Feb,245767
Mar,391500
Apr,537233
May,682966
Jun,1028699
Jul,974432
Aug,890165
Sep,265898
Oct,141163
Nov,135736
Dec,103097

Example (Approach 2)

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;
public class numbers {
   public static void main(String[] args) throws Exception {
      
      // specify the header lines in the csv file to skip
      int headerlines=2;
      int sum=0;
      
      // Read the csv file
      File file = new File("icecream_sales.csv");
      
      // Read all lines
      List<String> lines = Files.readAllLines(file.toPath(),StandardCharsets.UTF_8);
      
      // variable to hold int values after conversion
      int a;
      //current row number
      int rowcount=1;
      for (String line : lines) {
         
         //skip the number of header lines in csv file
         if (rowcount<=headerlines){
            rowcount=rowcount+1;
            continue;
         }
         String[] array = line.split(",", -1);
      
         //read the numbers from the second column after skipping the header lines
         a= Integer.parseInt(array[1]);
         System.out.println("Icecream Sales (in INR) for the month of "+ array[0] + " is "+ a);
        
         //Adding the numbers from the second column of CSV file
         sum =sum +a;
      }
      
      // printing the sum
      System.out.println("Total sum (in INR) for icecream sale in the given year is :" + sum);
   }
}

Output

Icecream Sales (in INR) for the month of Jan is 100034
Icecream Sales (in INR) for the month of Feb is 245767
Icecream Sales (in INR) for the month of Mar is 391500
Icecream Sales (in INR) for the month of Apr is 537233
Icecream Sales (in INR) for the month of May is 682966
Icecream Sales (in INR) for the month of Jun is 1028699
Icecream Sales (in INR) for the month of Jul is 974432
Icecream Sales (in INR) for the month of Aug is 890165
Icecream Sales (in INR) for the month of Sep is 265898
Icecream Sales (in INR) for the month of Oct is 141163
Icecream Sales (in INR) for the month of Nov is 135736
Icecream Sales (in INR) for the month of Dec is 103097
Total sum (in INR) for icecream sale in the given year is :5496690

Conclusion

In this article, two different programs are given to add the numbers using the java code. In the first approach, the numbers are given by the user at run time and in the second approach, the numbers are taken from a column of a CSV file for summation.

Updated on: 23-Mar-2023

514 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements