Programmator
Programmator
  • Home
  • Download
  • Social
    • YouTube
    • Instagram
    • Facebook
    • Telegram
  • Features
    • C/C++
    • Java
      • Category 1
      • Category 2
      • Category 3
      • Category 4
      • Category 5
    • Sub Menu 3
    • Sub Menu 4
  • Contact Us

Java program to add two Integers

In this program, two integers 11 and 12 are stored in integer variables first and second respectively.

Then, first and second are added using the + operator, and its result is stored in another variable sum.

Finally, sum is printed on the screen using println() function.

  •  Program:

class Main {

  public static void main(String[] args) {
    
    System.out.println("Enter two numbers");
    int first = 11;
    int second = 12;
    
    System.out.println(first + " " + second);

    // add two numbers
    int sum = first + second;
    System.out.println("The sum is: " + sum);
  }
}

  •  Output:

Enter two numbers
11 12
The sum is: 23

  •  Visit:
Write a Java program to print an Integer Here
How to print an integer entered by an user

In this program, an object of scanner class, reader is created to take input from standard input, Which is keyboard.

Then, Enter a number prompt is printed to give the user a visual cue as to what they should do next.

reader.nextInt() the reads all entered integers from the keyword unless it encounters a new line character \n Enter. The entered integer are then saved to the integer variable number.

If you enter any character which is not an integer, the compiler will throw an InputMismatchException.

Finally, number is printed onto the standard output (system.out) - computer screen using the function println().

  •  Program:

import java.util.Scanner;

public class HelloWorld {

    public static void main(String[] args) {

        // Creates a reader instance which takes
        // input from standard input - keyboard
        Scanner reader = new Scanner(System.in);
        System.out.print("Enter a number: ");

        // nextInt() reads the next integer from the keyboard
        int number = reader.nextInt();

        // println() prints the following line to the output screen
        System.out.println("You entered: " + number);
    }
}

  •  Output:

Enter a number: 10
You entered: 10

  •  Visit:

Write a Java program to add two integers Here
Newer Posts Home

ABOUT

I could look back at my life and get a good story out of it. It's a picture of somebody trying to figure things out.

SUBSCRIBE & FOLLOW

POPULAR POSTS

  • java program to calculate the sum of natural numbers

Advertisement

👆 Shopping 👆

Powered by Blogger

Archive

  • January 20231
  • December 20223
  • November 202215
  • August 20222

Report Abuse

Copyright

  • Home
  • About US
  • Privacy Policy
  • Disclaimer

About Me

Nilesh Patel
View my complete profile

Copyright © Programmator. Designed by OddThemes