Simple Java Program: Adding Two Numbers from User Input

In this post, I’ll walk you through a simple Java program that takes two integer inputs from the user and computes their sum. This exercise is perfect for beginners to practice variables, user input, and basic arithmetic in Java.

Pre-requisites

  • Apache NetBeans IDE 23
  • Java 22.0.2 (Java HotSpot™ 64-Bit Server VM 22.0.2+9-70)
  • System: Windows 11 version 10.0 (amd64), UTF-8, en_US

This ensures the program compiles and runs as shown.

Code Overview

package com.mycompany.ieeextreme001;
import java.util.Scanner;

public class IEEEXtreme001 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Hello World-IEEEXtreme!");

        // Asking for the first integer
        System.out.println("Supply a");
        int a = input.nextInt();

        // Asking for the second integer
        System.out.println("Supply b");
        int b = input.nextInt();

        // Calculating the sum
        int c = a + b;
        System.out.println("The sum of " + a + "\t and " + b + "\t is\t" + c);
    }
}

How It Works:

  • Scanner class reads user input.
  • System.out.println displays messages asking for values.
  • input.nextInt() reads integer inputs.
  • Adds a and b and stores in c.
  • Prints the sum in a formatted string.

Sample Run

Input:

Supply a
12
Supply b
34

Output:

The sum of 12 and 34 is 46

Screenshot (for Beginners)

Screenshot of running IEEEXtreme001.java in NetBeans IDE showing code and output

Caption: Running IEEEXtreme001.java in NetBeans IDE with code and output visible.

Caption: Running IEEEXtreme001.java in NetBeans IDE with code and output visible.

Takeaways

  • Reinforces basic input/output operations in Java.
  • Small programs like this build confidence for coding competitions (like IEEEXtreme).
  • You can expand it to handle subtraction, multiplication, or division easily.

Comments

Popular posts from this blog

From DSRC to 5G NR-V2X: The Road Ahead for Connected Vehicles

CTE 125 - COMPUTER WORKSHOP PRACTICE: WEEK ONE

Location_Privacy_for_IoV (Estimation of communication overhead incurred during IT request, ZKP validation, and blockchain logging)