JAVA: (Feel free to test me on concepts shown below even during my sleep)
1. Reverse a String
This program takes a string input from the user and reverses it:
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = scanner.nextInt();
System.out.println("Factorial of " + num + " = " + factorial(num));
}
static int factorial(int n) {
if (n == 0 || n == 1) return 1;
else return n * factorial(n - 1);
}
}
2. Factorial of a Number
This program calculates the factorial of a given number using recursion.
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = scanner.nextInt();
System.out.println("Factorial of " + num + " = " + factorial(num));
}
static int factorial(int n) {
if (n == 0 || n == 1) return 1;
else return n * factorial(n - 1);
}
}
3. Fibonacci Sequence
This program generates the Fibonacci sequence up to a specified number of terms.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of terms: ");
int terms = scanner.nextInt();
int a = 0, b = 1;
System.out.print("Fibonacci Series: ");
for (int i = 0; i < terms; i++) {
System.out.print(a + " ");
int nextTerm = a + b;
a = b;
b = nextTerm;
}
}
}
4. Palindrome Check
This program checks whether a given string is a palindrome.
import java.util.Scanner;
public class PalindromeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string: ");
String str = scanner.nextLine();
String reversed = new StringBuilder(str).reverse().toString();
if (str.equals(reversed)) {
System.out.println(str + " is a palindrome");
} else {
System.out.println(str + " is not a palindrome");
}
}
}
5. Prime Number Check
This program checks whether a given number is prime.
import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = scanner.nextInt();
if (isPrime(num)) {
System.out.println(num + " is a prime number");
} else {
System.out.println(num + " is not a prime number");
}
}
static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
}
This program takes a string input from the user and reverses it:
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = scanner.nextInt();
System.out.println("Factorial of " + num + " = " + factorial(num));
}
static int factorial(int n) {
if (n == 0 || n == 1) return 1;
else return n * factorial(n - 1);
}
}
2. Factorial of a Number
This program calculates the factorial of a given number using recursion.
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = scanner.nextInt();
System.out.println("Factorial of " + num + " = " + factorial(num));
}
static int factorial(int n) {
if (n == 0 || n == 1) return 1;
else return n * factorial(n - 1);
}
}
3. Fibonacci Sequence
This program generates the Fibonacci sequence up to a specified number of terms.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of terms: ");
int terms = scanner.nextInt();
int a = 0, b = 1;
System.out.print("Fibonacci Series: ");
for (int i = 0; i < terms; i++) {
System.out.print(a + " ");
int nextTerm = a + b;
a = b;
b = nextTerm;
}
}
}
4. Palindrome Check
This program checks whether a given string is a palindrome.
import java.util.Scanner;
public class PalindromeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string: ");
String str = scanner.nextLine();
String reversed = new StringBuilder(str).reverse().toString();
if (str.equals(reversed)) {
System.out.println(str + " is a palindrome");
} else {
System.out.println(str + " is not a palindrome");
}
}
}
5. Prime Number Check
This program checks whether a given number is prime.
import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = scanner.nextInt();
if (isPrime(num)) {
System.out.println(num + " is a prime number");
} else {
System.out.println(num + " is not a prime number");
}
}
static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
}