Java Programs Examples with Output

Share

Java Programs Examples with Output

I can provide you with a few Java program examples along with their outputs. Please note that you can run these examples in any Java development environment, such as Eclipse, IntelliJ IDEA, or even through the command line using javac and java commands.

Example 1: Hello World

javaCopy code

public class HelloWorld { public static void main(String[] args) { System.out.println(“Hello, World!”); } }

Output:

Copy code

Hello, World!

Example 2: Adding Two Numbers

javaCopy code

import java.util.Scanner; public class AddTwoNumbers { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter the first number: “); int num1 = scanner.nextInt(); System.out.print(“Enter the second number: “); int num2 = scanner.nextInt(); int sum = num1 + num2; System.out.println(“Sum: ” + sum); scanner.close(); } }

Output:

mathematicaCopy code

Enter the first number: 5 Enter the second number: 7 Sum: 12 

Example 3: Factorial Calculation

javaCopy code

import java.util.Scanner; public class Factorial { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter a number: “); int n = scanner.nextInt(); int factorial = 1; for (int i = 1; i <= n; i++) { factorial *= i; } System.out.println(“Factorial of ” + n + ” is: ” + factorial); scanner.close(); } }

Output:

mathematicaCopy code

Enter a number: 5 Factorial of 5 is: 120 

Example 4: Check if a Number is Prime

javaCopy code

import java.util.Scanner; public class PrimeCheck { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter a number: “); int num = scanner.nextInt(); boolean isPrime = true; if (num <= 1) { isPrime = false; } else { for (int i = 2; i <= num / 2; i++) { if (num % i == 0) { isPrime = false; break; } } } if (isPrime) { System.out.println(num + ” is a prime number.”); } else { System.out.println(num + ” is not a prime number.”); } scanner.close(); } }

Output:

lessCopy code

Enter a number: 17 17 is a prime number.

These are just a few basic examples to get you started with Java programming. You can run these examples to see the outputs and experiment with them to learn more about Java programming concepts.

Demo Day 1 Video:

 
You can find more information about Java in this Java Docs Link

 

Conclusion:

Unogeeks is the No.1 Training Institute for Java Training. Anyone Disagree? Please drop in a comment

You can check out our other latest blogs on Java Training here – Java Blogs

You can check out our Best in Class Java Training details here – Java Training

💬 Follow & Connect with us:

———————————-

For Training inquiries:

Call/Whatsapp: +91 73960 33555

Mail us at: info@unogeeks.com

Our Website ➜ https://unogeeks.com

Follow us:

Instagram: https://www.instagram.com/unogeeks

Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute

Twitter: https://twitter.com/unogeeks


Share

Leave a Reply

Your email address will not be published. Required fields are marked *