ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.