posted 1 month ago
Hello everyone,
I am learning Java basics and practicing simple programs.
Below is a basic Java program that takes two numbers from the user and prints their sum.
This example helps beginners understand:
User input using Scanner
Variable declaration
Basic arithmetic operation
Output using System.out.println()
Please review the code and let me know if there are better practices or improvements.
Thank you.
import java.util.Scanner;
public class SumOfTwoNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
int sum = num1 + num2;
System.out.println("Sum of two numbers: " + sum);
sc.close();
}
}