1.Fibonacci Series
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
int n1 = 0, n2 = 1, n3;
System.out.print(n1 + " " + n2);
for (int i = 2; i < n; i++) {
n3 = n1 + n2;
System.out.print(" " + n3);
n1 = n2;
n2 = n3;
}
}
}
Alternative MethodÂ
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
// Get input from user
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of terms to print in the Fibonacci series: ");
int n = input.nextInt();
// Initialize the first two terms of the series
int a = 0;
int b = 1;
// Print the first n terms of the series
System.out.println("Fibonacci series up to " + n + " terms:");
for (int i = 1; i <= n; i++) {
System.out.print(a + " ");
int sum = a + b;
a = b;
b = sum;
}
}
}
2.Palindrome string
Â
import java.util.Scanner;
public class PalindromeString {
public static void main(String[] args) {
// Get input from user
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = input.nextLine();
// Convert the string to lowercase and remove spaces
str = str.toLowerCase().replaceAll("\\s", "");
// Check if the string is a palindrome
boolean isPalindrome = true;
for (int i = 0; i < str.length() / 2; i++) {
if (str.charAt(i) != str.charAt(str.length() - i - 1)) {
isPalindrome = false;
break;
}
}
// Print the result
if (isPalindrome) {
System.out.println("The string is a palindrome.");
} else {
System.out.println("The string is not a palindrome.");
}
}
}
3.Reverse a String
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
// Get input from user
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = input.nextLine();
// Reverse the string
String reversedStr = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversedStr += str.charAt(i);
}
// Print the reversed string
System.out.println("The reversed string is: " + reversedStr);
}
}
4.Prime Number
import java.util.Scanner;
public class PrimeNumber {
public static void main(String[] args) {
// Get input from user
Scanner input = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int n = input.nextInt();
// Check if the number is prime
boolean isPrime = true;
if (n < 2) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
// Print the result
if (isPrime) {
System.out.println(n + " is a prime number.");
} else {
System.out.println(n + " is not a prime number.");
}
}
}
5.Factorial of a number.
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
// Get input from user
Scanner input = new Scanner(System.in);
System.out.print("Enter a non-negative integer: ");
int n = input.nextInt();
// Calculate the factorial
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
// Print the result
System.out.println("The factorial of " + n + " is " + factorial);
}
}