Perfect 👍 I’ll give you the solutions (full Java code) for all 20 programs in a clean way.
🔹 Java 20 Programs with Solutions
// 1. Hello World
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
// 2. Even or Odd
import java.util.Scanner;
class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n % 2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
}
}
// 3. Largest of Two Numbers
class LargestTwo {
public static void main(String[] args) {
int a = 10, b = 20;
if(a > b)
System.out.println("Largest: " + a);
else
System.out.println("Largest: " + b);
}
}
// 4. Sum of N Natural Numbers
class SumNatural {
public static void main(String[] args) {
int n = 10, sum = 0;
for(int i=1; i<=n; i++) sum += i;
System.out.println("Sum = " + sum);
}
}
// 5. Factorial
class Factorial {
public static void main(String[] args) {
int n = 5, fact = 1;
for(int i=1; i<=n; i++) fact *= i;
System.out.println("Factorial = " + fact);
}
}
// 6. Multiplication Table
class MultiplicationTable {
public static void main(String[] args) {
int n = 5;
for(int i=1; i<=10; i++)
System.out.println(n + " x " + i + " = " + (n*i));
}
}
// 7. Fibonacci Series
class Fibonacci {
public static void main(String[] args) {
int n=10, a=0, b=1, c;
System.out.print(a + " " + b);
for(int i=2; i<n; i++) {
c = a + b;
System.out.print(" " + c);
a = b; b = c;
}
}
}
// 8. Palindrome Number
class Palindrome {
public static void main(String[] args) {
int num = 121, rev = 0, temp = num;
while(num > 0) {
rev = rev*10 + num%10;
num /= 10;
}
if(temp == rev) System.out.println("Palindrome");
else System.out.println("Not Palindrome");
}
}
// 9. Reverse a String
class ReverseString {
public static void main(String[] args) {
String str = "hello";
String rev = "";
for(int i=str.length()-1; i>=0; i--)
rev += str.charAt(i);
System.out.println("Reversed: " + rev);
}
}
// 10. Count Vowels & Consonants
class VowelsConsonants {
public static void main(String[] args) {
String str = "hello world";
int v=0, c=0;
str = str.toLowerCase();
for(char ch : str.toCharArray()) {
if("aeiou".indexOf(ch) != -1) v++;
else if(ch >= 'a' && ch <= 'z') c++;
}
System.out.println("Vowels: " + v + " Consonants: " + c);
}
}
// 11. Prime Number Check
class PrimeCheck {
public static void main(String[] args) {
int n = 29, flag = 0;
for(int i=2; i<=n/2; i++) {
if(n % i == 0) { flag = 1; break; }
}
if(flag==0) System.out.println("Prime");
else System.out.println("Not Prime");
}
}
// 12. Armstrong Number
class Armstrong {
public static void main(String[] args) {
int num = 153, sum=0, temp=num;
while(num > 0) {
int d = num % 10;
sum += d*d*d;
num /= 10;
}
if(temp == sum) System.out.println("Armstrong");
else System.out.println("Not Armstrong");
}
}
// 13. Sum of Digits
class SumDigits {
public static void main(String[] args) {
int num = 1234, sum=0;
while(num > 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum of digits = " + sum);
}
}
// 14. Simple Calculator
import java.util.Scanner;
class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a=10, b=5;
char op = '+';
switch(op) {
case '+': System.out.println(a+b); break;
case '-': System.out.println(a-b); break;
case '*': System.out.println(a*b); break;
case '/': System.out.println(a/b); break;
default: System.out.println("Invalid");
}
}
}
// 15. Sort an Array
import java.util.Arrays;
class SortArray {
public static void main(String[] args) {
int arr[] = {5,3,8,1,2};
Arrays.sort(arr);
System.out.println("Sorted: " + Arrays.toString(arr));
}
}
// 16. Largest & Smallest in Array
class LargestSmallest {
public static void main(String[] args) {
int arr[] = {10, 5, 20, 8};
int max = arr[0], min = arr[0];
for(int n: arr) {
if(n>max) max=n;
if(n<min) min=n;
}
System.out.println("Max: " + max + " Min: " + min);
}
}
// 17. Matrix Addition
class MatrixAddition {
public static void main(String[] args) {
int a[][]={{1,2},{3,4}};
int b[][]={{5,6},{7,8}};
int c[][]=new int[2][2];
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) System.out.print(c[i][j]+" ");
System.out.println();
}
}
}
// 18. Matrix Multiplication
class MatrixMultiplication {
public static void main(String[] args) {
int a[][]={{1,2,3},{4,5,6}};
int b[][]={{7,8},{9,10},{11,12}};
int c[][]=new int[2][2];
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<3;k++)
c[i][j]+=a[i][k]*b[k][j];
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) System.out.print(c[i][j]+" ");
System.out.println();
}
}
}
// 19. Student Class (OOP)
class Student {
String name;
int marks;
Student(String n, int m) {
name = n; marks = m;
}
void display() {
System.out.println("Name: " + name + " Marks: " + marks);
}
public static void main(String[] args) {
Student s1 = new Student("Arun", 85);
Student s2 = new Student("Kumar", 92);
s1.display();
s2.display();
}
}
// 20. File Handling (Write & Read)
import java.io.*;
class FileExample {
public static void main(String[] args) throws Exception {
FileWriter fw = new FileWriter("test.txt");
fw.write("Hello File in Java");
fw.close();
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
String line;
while((line = br.readLine()) != null)
System.out.println(line);
br.close();
}
}
✅ These 20 programs cover loops, arrays, strings, functions, OOP, and file handling –
enough for practice and viva.
Do you want me to combine them into one Java file (with menu choice) so you can run all
programs together?