0% found this document useful (0 votes)
2 views2 pages

Java Programs

The document contains Java programs that demonstrate various functionalities, including checking for prime numbers, determining leap years, printing an X pattern, creating a diamond pattern, and generating a Fibonacci series. Each program is structured with a main method and utilizes loops and conditional statements to achieve its purpose. The examples provide a practical illustration of basic programming concepts in Java.

Uploaded by

ashusharma268235
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Java Programs

The document contains Java programs that demonstrate various functionalities, including checking for prime numbers, determining leap years, printing an X pattern, creating a diamond pattern, and generating a Fibonacci series. Each program is structured with a main method and utilizes loops and conditional statements to achieve its purpose. The examples provide a practical illustration of basic programming concepts in Java.

Uploaded by

ashusharma268235
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Programs

Prime Number Check


public class PrimeCheck {
public static void main(String[] args) {
int num = 29;
boolean isPrime = true;

if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}

System.out.println(num + " is " + (isPrime ? "a Prime Number" : "not a Prime Number"));
}
}

Leap Year Check


public class LeapYearCheck {
public static void main(String[] args) {
int year = 2024;
boolean isLeap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
System.out.println(year + " is " + (isLeap ? "a Leap Year" : "not a Leap Year"));
}
}

X Pattern
public class XPattern {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j || i + j == n - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

Diamond Pattern
public class DiamondPattern {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i += 2) {
for (int j = 0; j < (n - i) / 2; j++) System.out.print(" ");
for (int j = 0; j < i; j++) System.out.print("*");
System.out.println();
}
for (int i = n - 2; i >= 1; i -= 2) {
for (int j = 0; j < (n - i) / 2; j++) System.out.print(" ");
for (int j = 0; j < i; j++) System.out.print("*");
System.out.println();
}
}
}

Fibonacci Series
public class FibonacciSeries {
public static void main(String[] args) {
int n = 10, first = 0, second = 1;
System.out.print("Fibonacci Series: " + first + ", " + second);

for (int i = 2; i < n; i++) {


int next = first + second;
System.out.print(", " + next);
first = second;
second = next;
}
}
}

You might also like