SAMPLE PROGRAMS
1. Write a Java program to reverse a given string without using any built-in reverse
methods.
Example:
Input: Programming
Output: gnimmargorP
2. Write a Java program to check whether a given string is a palindrome or not.
A palindrome reads the same forward and backward.
Example:
Input: level
Output: level is a Palindrome
3. Write a Java program to find the factorial of a given number.
Example:
Input: 5
Output: Factorial of 5 is 120
4. Write a Java program to generate the Fibonacci series up to n terms.
The first two terms are 0 and 1, and each next term is the sum of the previous two.
Example:
Input: 10
Output: 0 1 1 2 3 5 8 13 21 34
5. Write a Java program to check whether a given number is prime or not.
A prime number is divisible only by 1 and itself.
Example:
Input: 29
Output: 29 is a Prime number.
6. Write a Java program to check whether a given number is an Armstrong number
or not.
An Armstrong number of 3 digits is equal to the sum of the cubes of its digits.
Example:
Input: 153
Output: 153 is an Armstrong number.
7. Write a Java program to find the maximum and minimum elements in an array.
Example:
Input: {34, 12, 9, 76, 29}
Output: Maximum: 76 Minimum: 9
8. Write a Java program to sort an array of integers in ascending order using built-in
functions or logic.
Example:
Input: {5, 2, 8, 1, 3}
Output: {1, 2, 3, 5, 8}
9. Write a Java program to count and display the frequency of each character in a
given string.
Example:
Input: hello
Output: {e=1, h=1, l=2, o=1}
10. Write a Java program to check whether a given number is even or odd using the
conditional (ternary) operator.
Example:
Input: 7
Output: 7 is Odd
11. Write a Java program to demonstrate the concept of inheritance.
Create a base class Animal with a method sound(), and a derived class Dog that
overrides the method to display “Dog barks”.
12. Write a Java program to demonstrate the use of abstract classes.
Create an abstract class Shape with an abstract method draw().
Create a subclass Circle that implements the draw() method and prints “Drawing
Circle”.
13. Write a Java program to implement an interface.
Create an interface Vehicle with a method start().
Implement this interface in a class Car that prints “Car started”.
14. Write a Java program to demonstrate exception handling using try, catch, and
finally blocks.
Example scenario: handle an ArithmeticException when dividing a number by zero.
15. Write a Java program to read input from the user using the Scanner class and
display a personalized message.
Example:
Input: Enter your name: AMIT
Output: Hello, AMIT!
SOLUTIONS :
1. Reverse a String (without built-in reverse methods)
import [Link];
public class ReverseString {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String rev = "";
for (int i = [Link]() - 1; i >= 0; i--) {
rev += [Link](i);
[Link]("Reversed String: " + rev);
2. Check Palindrome
import [Link];
public class PalindromeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String rev = "";
for (int i = [Link]() - 1; i >= 0; i--) {
rev += [Link](i);
}
if ([Link](rev))
[Link](str + " is a Palindrome");
else
[Link](str + " is not a Palindrome");
3. Factorial of a Number
import [Link];
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
[Link]("Factorial of " + n + " is " + fact);
}
4. Fibonacci Series
import [Link];
public class FibonacciSeries {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of terms: ");
int n = [Link]();
int a = 0, b = 1;
[Link]("Fibonacci Series: " + a + " " + b);
for (int i = 2; i < n; i++) {
int c = a + b;
[Link](" " + c);
a = b;
b = c;
5. Prime Number Check
import [Link];
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
boolean prime = true;
if (num <= 1) prime = false;
else {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
prime = false;
break;
if (prime)
[Link](num + " is a Prime number.");
else
[Link](num + " is not a Prime number.");
6. Armstrong Number
import [Link];
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int original = num, sum = 0;
while (num != 0) {
int digit = num % 10;
sum += [Link](digit, 3);
num /= 10;
if (sum == original)
[Link](original + " is an Armstrong number.");
else
[Link](original + " is not an Armstrong number.");
7. Maximum and Minimum in an Array
public class MaxMinArray {
public static void main(String[] args) {
int[] arr = {34, 12, 9, 76, 29};
int max = arr[0];
int min = arr[0];
for (int num : arr) {
if (num > max)
max = num;
if (num < min)
min = num;
[Link]("Maximum: " + max);
[Link]("Minimum: " + min);
}
8. Sort Array (Ascending)
import [Link];
public class SortArray {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 1, 3};
[Link](arr);
[Link]("Sorted Array: ");
for (int num : arr)
[Link](num + " ");
9. Count Frequency of Each Character
import [Link];
import [Link];
import [Link];
public class CharacterCount {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
Map<Character, Integer> map = new HashMap<>();
for (char ch : [Link]()) {
[Link](ch, [Link](ch, 0) + 1);
}
[Link](map);
10. Even or Odd (Ternary Operator)
import [Link];
public class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
String result = (num % 2 == 0) ? "Even" : "Odd";
[Link](num + " is " + result);
11. Demonstrate Inheritance
class Animal {
void sound() {
[Link]("Animal makes a sound");
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
public class InheritanceExample {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
12. Abstract Class Example
abstract class Shape {
abstract void draw();
class Circle extends Shape {
void draw() {
[Link]("Drawing Circle");
public class AbstractExample {
public static void main(String[] args) {
Shape s = new Circle();
[Link]();
13. Interface Implementation
interface Vehicle {
void start();
}
class Car implements Vehicle {
public void start() {
[Link]("Car started");
public class InterfaceExample {
public static void main(String[] args) {
Vehicle v = new Car();
[Link]();
14. Exception Handling Example
public class ExceptionHandling {
public static void main(String[] args) {
try {
int a = 10, b = 0;
int c = a / b; // This will cause an exception
[Link]("Result: " + c);
} catch (ArithmeticException e) {
[Link]("Error: Division by zero is not allowed.");
} finally {
[Link]("Execution completed.");
}
}
15. Read Input from User
import [Link];
public class UserInput {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Hello, " + name + "!");