Java Programs
Section A: Basic Programs
Program 1
Write a Java program that reads four integers and prints their sum, product, subtraction,
division, and average.
import [Link];
public class Program1 {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter first integer: ");
int a = [Link]();
[Link]("Enter second integer: ");
int b = [Link]();
[Link]("Enter third integer: ");
int c = [Link]();
[Link]("Enter fourth integer: ");
int d = [Link]();
int sum = a + b + c + d;
int product = a * b * c * d;
int sub = a - b - c - d;
double division = (double) a / b;
double average = sum / 4.0;
[Link]("Sum = " + sum);
[Link]("Product = " + product);
[Link]("Subtraction = " + sub);
[Link]("Division (a/b) = " + division);
[Link]("Average = " + average);
[Link]("--------------------");
[Link]("Group Members Roll Numbers:");
[Link]("bsf23005058");
[Link]("bsf23005054");
[Link]("bsf23005126")
Output:
Explanation:
This program takes four integers from the user. It calculates the sum, product, subtraction,
division of first two numbers, and average. Results are displayed using [Link]().
Program 2
Write a Java program that inputs numbers until the user enters a negative number and
calculates the average, maximum, and minimum of all positive numbers.
import [Link];
public class Program2 {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num, count = 0, sum = 0;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
while (true) {
[Link]("Enter number: ");
num = [Link]();
if (num < 0)
break;
sum += num;
count++;
if (num > max)
max = num;
if (num < min)
min = num;
}
if (count > 0) {
double avg = (double) sum / count;
[Link]("Average = " + avg);
[Link]("Maximum = " + max);
[Link]("Minimum = " + min);
} else {
[Link]("No positive numbers entered.");
}
[Link]("No positive numbers entered.");
[Link]("--------------------");
[Link]("Group Members Roll Numbers:");
[Link]("bsf23005058");
[Link]("bsf23005054");
[Link]("bsf23005126");
}
}
Output:
Explanation:
This program keeps taking input until a negative number is entered. It stores total count, sum,
maximum, and minimum values. After loop ends, it calculates average of positive numbers.
Program 3
Write a Java program that takes input from the user and checks whether a year is a leap
year or not.
import [Link];
public class Program3 {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter year: ");
int year = [Link]();
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
[Link](year + " is a Leap Year");
else
[Link](year + " is not a Leap Year");
[Link]("--------------------");
[Link]("Group Members Roll Numbers:");
[Link]("bsf23005058");
[Link]("bsf23005054");
[Link]("bsf23005126");
Output:
Explanation:
This program checks leap year conditions. A year is leap if divisible by 4 but not by 100, or
divisible by 400. Then it prints the result.
Program 4
Write a Java program that takes a character from the user and checks whether it is a
vowel or not.
import [Link];
public class Program4 {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter a character: ");
char ch = [Link]().charAt(0);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
[Link]("It is a vowel.");
else
[Link]("It is not a vowel.");
[Link]("--------------------");
[Link]("Group Members Roll Numbers:");
[Link]("bsf23005058");
[Link]("bsf23005054");
[Link]("bsf23005126");
Output:
Explanation:
This program takes one character as input. It compares the character with all vowels in uppercase
and lowercase. If matched, it prints vowel otherwise not vowel.
Program 5
Write a Java program that takes a number and the range (length) from the user and prints
its multiplication table up to that range.
import [Link];
public class Program5 {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter number: ");
int num = [Link]();
[Link]("Enter range: ");
int range = [Link]();
for (int i = 1; i <= range; i++) {
[Link](num + " x " + i + " = " + (num * i));
}
[Link]("--------------------");
[Link]("Group Members Roll Numbers:");
[Link]("bsf23005058");
[Link]("bsf23005054");
[Link]("bsf23005126");
Output:
Section B: Conditional Programs
Program 6
Write a Java program that allows the user to choose an operation (Mean, Median, or
Mode) and calculates the result based on the user’s input data.
import [Link];
import [Link];
public class Program6 {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("How many numbers? ");
int n = [Link]();
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
[Link]("Enter number " + (i + 1) + ": ");
arr[i] = [Link]();
[Link]("Choose Operation:");
[Link]("1. Mean");
[Link]("2. Median");
[Link]("3. Mode");
int choice = [Link]();
if (choice == 1) {
int sum = 0;
for (int num : arr)
sum += num;
double mean = (double) sum / n;
[Link]("Mean = " + mean);
}
else if (choice == 2) {
[Link](arr);
if (n % 2 == 0)
[Link]("Median = " + (arr[n/2 - 1] + arr[n/2]) / 2.0);
else
[Link]("Median = " + arr[n/2]);
else if (choice == 3) {
int mode = arr[0], maxCount = 0;
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[j] == arr[i])
count++;
if (count > maxCount) {
maxCount = count;
mode = arr[i];
[Link]("Mode = " + mode);
else {
[Link]("Invalid Choice");
[Link]("--------------------");
[Link]("Group Members Roll Numbers:");
[Link]("bsf23005058");
[Link]("bsf23005054");
[Link]("bsf23005126");
Output:
Mean Median Mode
Explanation:
This program stores user numbers in an array. It gives three choices: Mean, Median, or Mode.
According to user selection, it calculates and displays the result.
Program 7
Write a Java program that allows the user to enter input and find Range, Quartiles, and
Quartile Deviation based on the user choice.
import [Link];
import [Link];
public class Program7 {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("How many numbers? ");
int n = [Link]();
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
[Link]("Enter number " + (i + 1) + ": ");
arr[i] = [Link]();
[Link](arr);
int range = arr[n - 1] - arr[0];
int q1 = arr[n / 4];
int q2 = arr[n / 2];
int q3 = arr[(3 * n) / 4];
double qd = (q3 - q1) / 2.0;
[Link]("Range = " + range);
[Link]("Q1 = " + q1);
[Link]("Q2 = " + q2);
[Link]("Q3 = " + q3);
[Link]("Quartile Deviation = " + qd);
[Link]("--------------------");
[Link]("Group Members Roll Numbers:");
[Link]("bsf23005058");
[Link]("bsf23005054");
[Link]("bsf23005126");
Output:
Explanation:
This program takes numbers in an array and sorts them. Then it calculates range, quartiles (Q1,
Q2, Q3), and quartile deviation. Results are printed on screen.
Program 8
Write a Java program that calculates the volume of 3D shapes (cube, cylinder, sphere) and
the area of 2D shapes (circle, triangle, rectangle) based on the user’s choice.
import [Link];
public class Program8 {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Choose Shape:");
[Link]("1. Cube Volume");
[Link]("2. Cylinder Volume");
[Link]("3. Sphere Volume");
[Link]("4. Circle Area");
[Link]("5. Triangle Area");
[Link]("6. Rectangle Area");
int choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter side: ");
double side = [Link]();
[Link]("Volume = " + (side * side * side));
break;
case 2:
[Link]("Enter radius: ");
double r = [Link]();
[Link]("Enter height: ");
double h = [Link]();
[Link]("Volume = " + (3.1416 * r * r * h));
break;
case 3:
[Link]("Enter radius: ");
r = [Link]();
[Link]("Volume = " + ((4.0/3) * 3.1416 * r * r * r));
break;
case 4:
[Link]("Enter radius: ");
r = [Link]();
[Link]("Area = " + (3.1416 * r * r));
break;
case 5:
[Link]("Enter base: ");
double b = [Link]();
[Link]("Enter height: ");
h = [Link]();
[Link]("Area = " + (0.5 * b * h));
break;
case 6:
[Link]("Enter length: ");
double l = [Link]();
[Link]("Enter width: ");
double w = [Link]();
[Link]("Area = " + (l * w));
break;
default:
[Link]("Invalid Choice");
[Link]("--------------------");
[Link]("Group Members Roll Numbers:");
[Link]("bsf23005058");
[Link]("bsf23005054");
[Link]("bsf23005126");
}
Output:
Explanation:
This program uses switch statement for shape selection. It calculates volume for 3D shapes and
area for 2D shapes using standard formulas. User enters dimensions according to selected shape.