0% found this document useful (0 votes)
4 views

Java Programs Collection

The document provides a collection of Java programs for various tasks including generating even and odd numbers, prime numbers, calculating factorials, generating Fibonacci series, checking leap years, Armstrong numbers, Pascal's triangle, and identifying palindromes for strings and numbers. Each program is presented with its code and a brief description of its functionality. The programs are designed to work as expected for the specified tasks.

Uploaded by

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

Java Programs Collection

The document provides a collection of Java programs for various tasks including generating even and odd numbers, prime numbers, calculating factorials, generating Fibonacci series, checking leap years, Armstrong numbers, Pascal's triangle, and identifying palindromes for strings and numbers. Each program is presented with its code and a brief description of its functionality. The programs are designed to work as expected for the specified tasks.

Uploaded by

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

Java Programs Collection

Give answer programs in java :

1. Even number generation


2. Odd number generation
3. Prime number generation
4. Factorial Program 5! = 120
5. Fibonacci series
6. Leap year 2000 - 3000
7. Amstrong Number
8. Pascal Triangle
9. String Palindrome
10. Number Palindrome

Here are Java programs for each of the requested problems:

1. Even Number Generation

java

public class EvenNumbers {


public static void main(String[] args) {
for (int i = 2; i <= 100; i += 2) {
System.out.print(i + " ");
}
}
}

2. Odd Number Generation

java

public class OddNumbers {


public static void main(String[] args) {
for (int i = 1; i <= 100; i += 2) {
System.out.print(i + " ");
}
}
}

3. Prime Number Generation

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/4
java

public class PrimeNumbers {


public static void main(String[] args) {
for (int i = 2; i <= 100; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
}

public static boolean isPrime(int num) {


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

4. Factorial Program (e.g., 5! = 120)

java

public class Factorial {


public static void main(String[] args) {
int number = 5;
System.out.println("Factorial of " + number + " is " + factorial(number));
}

public static int factorial(int n) {


if (n == 0) return 1;
return n * factorial(n - 1);
}
}

5. Fibonacci Series

java

public class FibonacciSeries {


public static void main(String[] args) {
int n = 10;
int a = 0, b = 1;

System.out.print("Fibonacci Series: " + a + " " + b + " ");

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


int next = a + b;
System.out.print(next + " ");
a = b;
b = next;
}
}
}

6. Leap Year (2000 - 3000)

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/4
java

public class LeapYear {


public static void main(String[] args) {
for (int year = 2000; year <= 3000; year++) {
if (isLeapYear(year)) {
System.out.print(year + " ");
}
}
}

public static boolean isLeapYear(int year) {


return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}

7. Armstrong Number

java

public class ArmstrongNumber {


public static void main(String[] args) {
int number = 153;
if (isArmstrong(number)) {
System.out.println(number + " is an Armstrong number.");
} else {
System.out.println(number + " is not an Armstrong number.");
}
}

public static boolean isArmstrong(int num) {


int original = num, sum = 0;
while (num != 0) {
int digit = num % 10;
sum += Math.pow(digit, 3);
num /= 10;
}
return sum == original;
}
}

8. Pascal Triangle

java

public class PascalTriangle {


public static void main(String[] args) {
int rows = 5;
for (int i = 0; i < rows; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(pascal(i, j) + " ");
}
System.out.println();
}
}

public static int pascal(int row, int col) {


if (col == 0 || col == row) return 1;
return pascal(row - 1, col - 1) + pascal(row - 1, col);
}
}

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/4
9. String Palindrome

java

public class StringPalindrome {


public static void main(String[] args) {
String str = "madam";
if (isPalindrome(str)) {
System.out.println(str + " is a palindrome.");
} else {
System.out.println(str + " is not a palindrome.");
}
}

public static boolean isPalindrome(String str) {


int start = 0, end = str.length() - 1;
while (start < end) {
if (str.charAt(start) != str.charAt(end)) {
return false;
}
start++;
end--;
}
return true;
}
}

10. Number Palindrome

java

public class NumberPalindrome {


public static void main(String[] args) {
int number = 121;
if (isPalindrome(number)) {
System.out.println(number + " is a palindrome.");
} else {
System.out.println(number + " is not a palindrome.");
}
}

public static boolean isPalindrome(int num) {


int original = num, reverse = 0;
while (num != 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num /= 10;
}
return reverse == original;
}
}

These programs should work as expected for the given tasks. Let me know if you have further
questions!
ChatGPT can make mistakes. Check important info.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/4

You might also like