Java Sample Programs

Share

Java Sample Programs

Sure, here are a few examples of simple Java programs.

  1. Hello World
java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
  1. Fibonacci series
java

public class Fibonacci {
public static void main(String[] args) {
int n = 10, a = 0, b = 1;
System.out.print("First " + n + " terms: ");

 

for (int i = 1; i <= n; ++i) {
System.out.print(a + " ");
int sum = a + b;
a = b;
b = sum;
}
}
}

  1. Factorial of a number
java
public class Factorial {
public static void main(String[] args) {
int num = 5;
long factorial = 1;
for(int i = 1; i <= num; ++i) {
factorial *= i;
}
System.out.printf("Factorial of %d = %d", num, factorial);
}
}
  1. Checking if a number is prime
java
public class PrimeCheck {
public static void main(String[] args) {
int num = 29;
boolean flag = false;
for(int i = 2; i <= num/2; ++i) {
if(num % i == 0) {
flag = true;
break;
}
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}

Remember to run these programs, you need to save the file with the same name as the public class. For instance, HelloWorld.java for the HelloWorld program.

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 *