RA2411003010306- Salil Vaidya
Java Programs with Output
Assignment 1
1. Find Harry's Age in 2024
public class HarryAgeCalculator {
public static void main(String[] args) {
int birthYear = 2000;
int currentYear = 2024;
int age = currentYear - birthYear;
System.out.println("Harry's age in " + currentYear + " is " + age);
}
}
Output:
Harry's age in 2024 is 24
2. Find Sam’s Average Mark in PCM
public class SamsAverageMark {
public static void main(String[] args) {
int maths = 94;
int physics = 95;
int chemistry = 96;
double average = (maths + physics + chemistry) / 3.0;
System.out.println("Sam’s average mark in PCM is " + average);
}
}
Output:
Sam’s average mark in PCM is 95.0
3. Convert 10.8 km to Miles
public class KmToMilesConverter {
public static void main(String[] args) {
double km = 10.8;
double miles = km * 1.6;
System.out.println("The distance " + km + " km in miles is " + miles);
}
}
Output:
The distance 10.8 km in miles is 17.28
4. Calculate Profit and Loss
public class ProfitCalculator {
public static void main(String[] args) {
int costPrice = 129;
int sellingPrice = 191;
int profit = sellingPrice - costPrice;
double profitPercentage = (profit / (double) costPrice) * 100;
System.out.println("The Cost Price is INR " + costPrice + " and
Selling Price is INR " + sellingPrice);
System.out.println("The Profit is INR " + profit + " and the Profit
Percentage is " + profitPercentage + "%");
}
}
Output:
The Cost Price is INR 129 and Selling Price is INR 191
The Profit is INR 62 and the Profit Percentage is 48.06%
5. Divide 14 Pens Among 3 Students
public class PenDistribution {
public static void main(String[] args) {
int totalPens = 14;
int students = 3;
int pensPerStudent = totalPens / students;
int remainingPens = totalPens % students;
System.out.println("The Pen Per Student is " + pensPerStudent + " and
the remaining pen not distributed is " + remainingPens);
}
}
Output:
The Pen Per Student is 4 and the remaining pen not distributed is 2
6. Calculate Discounted University Fee
public class UniversityFeeCalculator {
public static void main(String[] args) {
double fee = 125000;
double discountPercent = 10;
double discountAmount = (discountPercent / 100) * fee;
double finalFee = fee - discountAmount;
System.out.println("The discount amount is INR " + discountAmount + "
and final discounted fee is INR " + finalFee);
}
}
Output:
The discount amount is INR 12500.0 and final discounted fee is INR 112500.0
7. Compute Volume of Earth
public class EarthVolumeCalculator {
public static void main(String[] args) {
double radius = 6378;
double volumeKm3 = (4.0/3) * Math.PI * Math.pow(radius, 3);
double volumeMiles3 = volumeKm3 / Math.pow(1.609, 3);
System.out.println("The volume of Earth in cubic kilometers is " +
volumeKm3 + " and cubic miles is " + volumeMiles3);
}
}
Output:
The volume of Earth in cubic kilometers is 1.0868324119371486E12 and cubic
miles is 2.6032655435476804E11
8. Convert Kilometers to Miles (User Input)
import java.util.Scanner;
public class KilometerToMile {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter distance in km: ");
double km = input.nextDouble();
double miles = km * 1.6;
System.out.println("The total miles is " + miles + " mile for the
given " + km + " km");
}
}
Output Example:
Enter distance in km: 10
The total miles is 16.0 mile for the given 10.0 km
9. Calculate University Discount (User Input)
import java.util.Scanner;
public class DiscountCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter fee amount: ");
double fee = input.nextDouble();
System.out.print("Enter discount percentage: ");
double discountPercent = input.nextDouble();
double discountAmount = (discountPercent / 100) * fee;
double finalFee = fee - discountAmount;
System.out.println("The discount amount is INR " + discountAmount + "
and final discounted fee is INR " + finalFee);
}
}
Output Example:
Enter fee amount: 100000
Enter discount percentage: 15
The discount amount is INR 15000.0 and final discounted fee is INR 85000.0
10. Convert Height from cm to Feet and Inches
public class HeightConverter {
public static void main(String[] args) {
double heightCm = 170;
double heightInches = heightCm / 2.54;
int feet = (int) (heightInches / 12);
double inches = heightInches % 12;
System.out.println("Your Height in cm is " + heightCm + " while in
feet is " + feet + " and inches is " + inches);
}
}
Output:
Your Height in cm is 170.0 while in feet is 5 and inches is 6.929133858267717
Java Programs with Output
Assignment 2
1. Basic Calculator
import java.util.Scanner;
public class BasicCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
double number1 = scanner.nextDouble();
System.out.print("Enter second number: ");
double number2 = scanner.nextDouble();
double addition = number1 + number2;
double subtraction = number1 - number2;
double multiplication = number1 * number2;
double division = number1 / number2;
System.out.println("Addition: " + addition);
System.out.println("Subtraction: " + subtraction);
System.out.println("Multiplication: " + multiplication);
System.out.println("Division: " + division);
}
}
Output:
Enter first number: 10
Enter second number: 5
Addition: 15.0
Subtraction: 5.0
Multiplication: 50.0
Division: 2.0
2. Find Area of a Triangle
import java.util.Scanner;
public class TriangleArea {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter base: ");
double base = scanner.nextDouble();
System.out.print("Enter height: ");
double height = scanner.nextDouble();
double area = 0.5 * base * height;
System.out.println("The area of the triangle is " + area + " square
units");
}
}
Output:
Enter base: 10
Enter height: 5
The area of the triangle is 25.0 square units
3. Find Side of Square
import java.util.Scanner;
public class SquareSide {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter perimeter: ");
double perimeter = scanner.nextDouble();
double side = perimeter / 4;
System.out.println("The length of the side is " + side + " whose
perimeter is " + perimeter);
}
}
Output:
Enter perimeter: 16
The length of the side is 4.0 whose perimeter is 16.0
4. Convert Distance to Yards and Miles
import java.util.Scanner;
public class DistanceConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter distance in feet: ");
double feet = scanner.nextDouble();
double yards = feet / 3;
double miles = feet / 5280;
System.out.println("The distance in yards is " + yards + " and in
miles is " + miles);
}
}
Output:
Enter distance in feet: 5280
The distance in yards is 1760.0 and in miles is 1.0
5. Calculate Total Price
import java.util.Scanner;
public class TotalPriceCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter unit price: ");
double unitPrice = scanner.nextDouble();
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
double totalPrice = unitPrice * quantity;
System.out.println("The total purchase price is INR " + totalPrice);
}
}
Output:
Enter unit price: 50
Enter quantity: 3
The total purchase price is INR 150.0
6. Find Quotient and Remainder
import java.util.Scanner;
public class QuotientRemainder {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();
int quotient = num1 / num2;
int remainder = num1 % num2;
System.out.println("The Quotient is " + quotient + " and the Remainder
is " + remainder);
}
}
Output:
Enter first number: 10
Enter second number: 3
The Quotient is 3 and the Remainder is 1
7. Integer Operations
import java.util.Scanner;
public class IntOperation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter value for a: ");
int a = scanner.nextInt();
System.out.print("Enter value for b: ");
int b = scanner.nextInt();
System.out.print("Enter value for c: ");
int c = scanner.nextInt();
int result1 = a + b * c;
int result2 = a * b + c;
int result3 = c + a / b;
int result4 = a % b + c;
System.out.println("The results of Int Operations are " + result1 + ",
" + result2 + ", and " + result3);
}
}
Output:
Enter value for a: 2
Enter value for b: 3
Enter value for c: 4
The results of Int Operations are 14, 10, and 5
8. Double Operations
import java.util.Scanner;
public class DoubleOperation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter value for a: ");
double a = scanner.nextDouble();
System.out.print("Enter value for b: ");
double b = scanner.nextDouble();
System.out.print("Enter value for c: ");
double c = scanner.nextDouble();
double result1 = a + b * c;
double result2 = a * b + c;
double result3 = c + a / b;
double result4 = a % b + c;
System.out.println("The results of Double Operations are " + result1 +
", " + result2 + ", and " + result3);
}
}
Output:
Enter value for a: 2.5
Enter value for b: 3.5
Enter value for c: 4.5
The results of Double Operations are 18.25, 13.25, and 5.214285714285714