Skip to main content

Sum of Three Numbers Example in Java

2 min read Updated May 29, 2026
Share:
On this page (8sections)

Introduction

Sum Of Three Numbers is a classic Java console program that demonstrates the concept with complete source code and sample output. These programs cover your first Java class, constructors, methods and simple OOP building blocks.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Sum Of Three Numbers Example Program

import java.util.Scanner;

class SumOfThreeNumbers{
	public static void main(String args[]){
		int num1,num2,num3,result;
		System.out.println("Enter any 3 integers ");
		Scanner in = new Scanner(System.in);
		num1 = in.nextInt();
		num2 = in.nextInt();
		num3 = in.nextInt();
		result=num1+num2+num3;
		System.out.println("The result after addition is "+result);
	}
}

Sample Output

Enter any 3 integers
99
132
56
The result after addition is 287

When to use

Use this sum of three numbers example when learning or revising core Java syntax.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. import java.util.Scanner; imports a class used later in the program.

  3. A println / print call writes text to the console — part of the sample output below.

  4. A Scanner reads typed input from the keyboard (System.in).

  5. num1 = in.nextInt(); updates a variable used in the calculation or output.

  6. num2 = in.nextInt(); updates a variable used in the calculation or output.

  7. num3 = in.nextInt(); updates a variable used in the calculation or output.

  8. A println / print call writes text to the console — part of the sample output below.

Best Practices

  • Name classes in PascalCase and follow one public class per file when starting out.
  • Keep main short — delegate work to other methods as programs grow.

Common Mistakes

  • Copying code without understanding each line — practice by changing one statement at a time.
  • Mismatching the public class name and the .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Sum Of Three Numbers program demonstrate?
It shows how to implement sum of three numbers in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related Tutorials

Search tutorials