-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

Saturday, May 9, 2020

Java Program to add two numbers using Scanner

  • May 09, 2020
  • by


Java Program to add two numbers using Scanner


Logic : int c = int a + int b


In this Java Program, we use the Scanner class to get the numbers from the user.  


Step 1 : Create 3 variables named as num1, num2 and sum. 


Step 2 : Insert Scanner Class for taking the input from the user.  


Step 3 : Use System.out.println method to take input of first number from the user.


Step 4 : Store the user's input in variable num1


Step 5 : Use System.out.println method to take input of second number from the user.


Step 6 : Store the user's input in variable num2


Step 7 : Close the Scanner.


Step 8 : Perform the addition of the two numbers entered by the user with the help of  "+" operator and store the result in the variable named as sum.


Step 9 : Display the result by using the System.out.println method. 


Program :

 

import java.util.Scanner;


public class AddTwoNumbers2

    {

        public static void main(String[] args)

        {

            int num1, num2, sum;

            Scanner sc = new Scanner(System.in);

            System.out.println("Enter First Number:");

            num1 = sc.nextInt();

         

            System.out.println("Enter Second Number:");

            num2 = sc.nextInt();

         

            sc.close();

            sum = num1 + num2;

            System.out.println("Sum of these numbers:"+sum);

         

        }

}


Output:


Enter First Number :

121

Enter Second Number:

19

Sum of these numbers: 140


For execution of the code do visit my YouTube channel:


https://youtu.be/OY7-fUy9bjo

A blog full of codes of various languages like Java, .net, C, C++ etc with their outputs and some other IT stuff.

0 comments:

Post a Comment

ITEngineer8995
India

SEND ME A MESSAGE

Search This Blog

Powered by Blogger.

Pages