Sum of Two Integers in JAVA | Java Practice Question – 1

Write a program that takes two integer as an input from users  and, print their sum.

Firstly, We have to import Java’s predefined Scanner Class from package java.util

import java.util.scanner

Now, Begins the declaration of class SumofIntegers. and remember, the file name for this public class must be SumofInteger.java

public class SumofInteger

Note – Don’t forget an opening and closing braces.

Create the main method of java. that is available on every java program

public static void main(String[] args)

Note – Don’t forget an opening and closing braces.

Declaring and Creating a Scanner to Obtain User Input from the Keyboard.

Scanner input = new Scanner(System.in);

Declaring Variable to Store Integers

int number1;
int number2;
int sum;

Prompting the User for input and Obtaining an int as input from the user

System.out.print("Enter First Integer-");
number1 = input.nextInt();

System.out.print("Enter Second Integer -->");
number2 = input.nextInt();

Adding both integer and Store it in int sum

sum = number1 + number2

Display the Output

System.out.printf("Sum of %d + %d = %d",number1,number2,number3);

WATCH TUTORIAL

Leave a Comment

Your email address will not be published. Required fields are marked *