Program 1. Write a program to execute arithmetic operations in java.
import [Link];
public class ArithmeticOperations {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
double num1 = [Link]();
[Link]("Enter second number: ");
double num2 = [Link]();
double sum = num1 + num2;
double difference = num1 - num2;
double product = num1 * num2;
double quotient = num1 / num2;
[Link]("Sum: " + sum);
[Link]("Difference: " + difference);
[Link]("Product: " + product);
[Link]("Quotient: " + quotient);
}
}
OUTPUT:
Program 2. Write a program to find the largest number.
import [Link];
public class LargestNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int num1 = [Link]();
[Link]("Enter second number: ");
int num2 = [Link]();
[Link]("Enter third number: ");
int num3 = [Link]();
int largest = (num1 > num2) ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 :
num3);
[Link]("Largest number: " + largest);
OUTPUT:
Program 3. Write a program to check number is prime or not.
import [Link];
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
[Link]();
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) {
isPrime = false;
break;
if (isPrime) {
[Link](num + " is a prime number.");
} else {
[Link](num + " is not a prime number.");
OUTPUT:
Program [Link] a program to check number is even or odd.
import [Link];
public class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
[Link]();
if (num % 2 == 0) {
[Link](num + " is even.");
} else {
[Link](num + " is odd.");
OUTPUT:
Program 5. Write a program to find the 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 num = [Link]();
[Link]();
long factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
[Link]("Factorial of " + num + " is " + factorial);
OUTPUT:
Program 6. Write a program to find the reverse of a number.
import [Link];
public class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
[Link]();
int reverse = 0;
while (num != 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num /= 10;
[Link]("Reversed number: " + reverse);
OUTPUT:
Program 7. Write a program to concatenate two strings.
import [Link];
public class StringConcatenation {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first string: ");
String str1 = [Link]();
[Link]("Enter second string: ");
String str2 = [Link]();
[Link]();
String result = str1 + str2;
[Link]("Concatenated string: " + result);
OUTPUT:
Program 8. Write a program to count number of char in a string.
import [Link];
public class CharacterCount {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
[Link]();
int count = 0;
for (int i = 0; i < [Link](); i++) {
if ([Link]([Link](i))) {
count++;
[Link]("Number of characters: " + count);
OUTPUT:
Program 9. Write a program to count number of vowels and consonants in a string.
import [Link];
public class VowelConsonantCount {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
[Link]();
int vowels = 0, consonants = 0;
str = [Link]();
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if (ch >= 'a' && ch <= 'z') {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
} else {
consonants++;
[Link]("Number of vowels: " + vowels);
[Link]("Number of consonants: " + consonants);
OUTPUT:
Program 10. Write a program to reverse a string.
public class Reverse{
public static void main(String[] args) {
String str = "Hello, World!";
String reversed = "";
for (int i = [Link]() - 1; i >= 0; i--) {
reversed += [Link](i);
[Link](reversed);
OUTPUT:
Program 11. Write a program to create an array.
public class ArrayExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < [Link]; i++) {
[Link](numbers[i]);
OUTPUT:
Program 12. Write a program to make a left triangle star pattern.
public class StarPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
[Link]("*");
[Link]();
OUTPUT:
Program 13. Write a program to print a number pattern in pyramid.
public class NumberPyramid {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = rows; j > i; j--) {
[Link](" ");
for (int k = 1; k <= i; k++) {
[Link](k + " ");
[Link]();
OUTPUT:
Program 14. Write a program to calculate the area of a rectangle using inner class.
public class Rectangle {
private double length;
private double width;
public Rectangle(double length, double width) {
[Link] = length;
[Link] = width;
public class Area {
public double calculateArea() {
return length * width;
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5.0, 3.0);
[Link] area = [Link] Area();
[Link]("Area of the rectangle: " + [Link]());
OUTPUT:
Program 15(a). Write a program to perform a multilevel inheritance.
class Animal {
void eat() {
[Link]("This animal eats food.");
class Dog extends Animal {
void bark() {
[Link]("The dog barks.");
class Puppy extends Dog {
void sleep() {
[Link]("The puppy sleeps.");
public class MultilevelInheritance {
public static void main(String[] args) {
Puppy puppy = new Puppy();
[Link]();
[Link]();
[Link]();
OUTPUT:
Program 15(b).Write a program to perform a hierarchical inheritance.
class Animal {
void eat() {
[Link]("This animal eats food.");
class Dog extends Animal {
void bark() {
[Link]("The dog barks.");
class Cat extends Animal {
void meow() {
[Link]("The cat meows.");
public class HierarchicalInheritance {
public static void main(String[] args) {
Dog dog = new Dog();
[Link]();
[Link]();
Cat cat = new Cat();
[Link]();
[Link]();
}
OUTPUT:
Program 15(c). Write a program to perform multiple inheritance using interface.
interface Animal {
void eat();
interface Vehicle {
void drive();
class Dog implements Animal, Vehicle {
public void eat() {
[Link]("The dog eats food.");
public void drive() {
[Link]("The dog drives a vehicle.");
public class MultipleInheritanceUsingInterface {
public static void main(String[] args) {
Dog dog = new Dog();
[Link]();
[Link]();
OUTPUT:
Program 16. Write a program to implement method overloading.
class Calculator {
int add(int a, int b) {
return a + b;
int add(int a, int b, int c) {
return a + b + c;
double add(double a, double b) {
return a + b;
public static void main(String[] args) {
Calculator calc = new Calculator();
[Link]([Link](5, 3));
[Link]([Link](5, 3, 2));
[Link]([Link](5.5, 3.3));
OUTPUT:
Program 17. Write a program to implement the concept of method overriding.
class Animal {
void sound() {
[Link]("Animal makes a sound");
class Dog extends Animal {
@Override
void sound() {
[Link]("Dog barks");
class Cat extends Animal {
@Override
void sound() {
[Link]("Cat meows");
public class Main {
public static void main(String[] args) {
Animal animal = new Animal();
Dog dog = new Dog();
Cat cat = new Cat();
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:
Program 18. Write a program to show abstraction using abstract class in java.
abstract class Animal {
abstract void sound();
abstract void eat();
class Dog extends Animal {
@Override
void sound() {
[Link]("Dog barks");
@Override
void eat() {
[Link]("Dog eats bones");
class Cat extends Animal {
@Override
void sound() {
[Link]("Cat meows");
@Override
void eat() {
[Link]("Cat eats fish");
public class Main1 {
public static void main(String[] args) {
Animal dog = new Dog();
Animal cat = new Cat();
[Link]();
[Link]();
[Link]();
[Link]();
OUTPUT:
Program 19. Write a program to show abstraction class using interfaces in java.
interface Animal {
void sound();
void eat();
class Dog implements Animal {
@Override
public void sound() {
[Link]("Dog barks");
@Override
public void eat() {
[Link]("Dog eats bones");
class Cat implements Animal {
@Override
public void sound() {
[Link]("Cat meows");
@Override
public void eat() {
[Link]("Cat eats fish");
public class Main2 {
public static void main(String[] args) {
Animal dog = new Dog();
Animal cat = new Cat();
[Link]();
[Link]();
[Link]();
[Link]();
OUTPUT:
Program 20. Write a program to implement multithreading in java.
class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
[Link](1000); // Pause for 1 second
} catch (InterruptedException e) {
[Link](e);
[Link]([Link]().getId() + " Value: " + i);
}
public class Main3 {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
[Link](); // Start thread t1
[Link](); // Start thread t2