×

Java Programs

Java Practice

Java program to find sum and average of two integer numbers

This is a simple java program, in which we are taking input of two integer numbers and calculate their SUM and AVERAGE.

Given (input) two integer numbers and we have to calculate their SUM and AVERAGE.

We have already discussed how to Input and print an integer value? Through this program, we will learn following things:

  1. Taking integer inputs
  2. Performing mathematical operations
  3. Printing the values on output screen

Consider the program:

// Find sum and average of two numbers in Java

import java.util.*;

public class Numbers {
    public static void main(String args[]) {
        int a, b, sum;
        float avg;

        Scanner buf = new Scanner(System.in);

        System.out.print("Enter first number : ");
        a = buf.nextInt();

        System.out.print("Enter second number : ");
        b = buf.nextInt();

        /*Calculate sum and average*/
        sum = a + b;
        avg = (float)((a + b) / 2);

        System.out.print("Sum : " + sum + "\nAverage : " + avg);
    }
}

Output

Enter first number : 100 
Enter second number : 200 
Sum : 300 
Average : 150.0

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.