1.
Digital Camera Price Calculation
import [Link];
public class CameraPriceCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter printed price of the digital camera: ");
double printedPrice = [Link]();
double discountedPrice = printedPrice * 0.90; // 10% discount
double finalPrice = discountedPrice * 1.06; // Adding 6% tax
[Link]("Amount to be paid: " + finalPrice);
[Link]();
}
}
Variable Description Table
Variable Type Description
printedPrice Printed price of the camera (input from user)
double
discountedPrice double Price after 10% discount
finalPrice double Price after adding 6% tax
Example Output
Enter printed price of the digital camera: 10000
Amount to be paid: 9540.0
2. Simple Pendulum Time Period Calculation
import [Link];
public class SimplePendulum {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter length of pendulum (meters): ");
double length = [Link]();
[Link]("Enter acceleration due to gravity (m/s^2): ");
double gravity = [Link]();
double timePeriod = 2 * [Link] * [Link](length / gravity);
[Link]("Time Period of the Pendulum: " + timePeriod + " seconds");
[Link]();
}
}
Variable Description Table
Variable Type Description
length doubleLength of the pendulum (input)
gravity double Acceleration due to gravity (input)
timePeriod double Time period of the pendulum
Example Output
Enter length of pendulum (meters): 1.0
Enter acceleration due to gravity (m/s^2): 9.8
Time Period of the Pendulum: 2.006 seconds
3. Employee Salary Calculation
import [Link];
class Employee {
double basicPay, DA, HRA, CPF, grossPay, netPay;
void calculateSalary(double basicPay) {
[Link] = basicPay;
DA = basicPay * 0.30;
HRA = basicPay * 0.15;
CPF = basicPay * 0.125;
grossPay = basicPay + DA + HRA;
netPay = grossPay - CPF;
void displaySalary() {
[Link]("Gross Pay: " + grossPay);
[Link]("Net Pay: " + netPay);
}
}
public class EmployeeSalary {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Basic Pay: ");
double basicPay = [Link]();
Employee emp = new Employee();
[Link](basicPay);
[Link]();
[Link]();
}
}
Variable Description Table
Variable Type Description
basicPay double Basic salary of the employee
DA double Dearness Allowance (30% of Basic Pay)
HRA double House Rent Allowance (15% of Basic Pay)
CPF double Provident Fund deduction (12.5% of Basic Pay)
grossPay double Total salary before deductions
netPay double Salary after CPF deduction
Example Output
Enter Basic Pay: 20000
Gross Pay: 29000.0
Net Pay: 26500.0
4. Compound Interest Calculation
import [Link];
public class CompoundInterest {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter initial investment amount: ");
double principal = [Link]();
double rate = 5.0 / 100;
int years = 3;
double amountYear1 = principal * [Link]((1 + rate), 1);
double amountYear2 = principal * [Link]((1 + rate), 2);
double amountYear3 = principal * [Link]((1 + rate), 3);
double interestYear1 = amountYear1 - principal;
double interestYear2 = amountYear2 - amountYear1;
double finalAmount = amountYear3;
[Link]("Interest for first year: " + interestYear1);
[Link]("Interest for second year: " + interestYear2);
[Link]("Amount after three years: " + finalAmount);
[Link]();
}
}
Variable Description Table
Variable Type Description
principal Initial investment amount
double
rate double Annual interest rate (5%)
years int Number of years (fixed at 3)
amountYear1 double Amount after year 1
amountYear2 double Amount after year 2
amountYear3 double Amount after year 3
interestYear1 double Interest earned in first year
interestYear2 double Interest earned in second year
finalAmount double Total amount after three years
Example Output
Enter initial investment amount: 1000
Interest for first year: 50.0
Interest for second year: 52.5
Amount after three years: 1157.625
4. Compound Interest Calculation
import [Link];
public class CompoundInterest {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Principal Amount: ");
double principal = [Link]();
[Link]("Enter Rate of Interest (in %): ");
double rate = [Link]() / 100;
[Link]("Enter Time (in years): ");
int time = [Link]();
double amount = principal * [Link](1 + rate, time);
double interest = amount - principal;
[Link]("Total Interest after " + time + " years: Rs " + interest);
[Link]();
}
}
Variable Description Table
Variable Type Description
principal doubleInitial investment amount
rate double Annual interest rate (converted to decimal)
time int Number of years
amount double Total amount after time period
interest double Total interest accrued
Example Output
Enter Principal Amount: 5000
Enter Rate of Interest (in %): 10
Enter Time (in years): 3
Total Interest after 3 years: Rs 1655.0
2. Number of Shares Calculation
import [Link];
public class SharesCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Annual Dividend Received: ");
double annualDividend = [Link]();
[Link]("Enter Nominal Value of Each Share: ");
double nominalValue = [Link]();
[Link]("Enter Dividend Percentage: ");
double dividendPercentage = [Link]();
int sharesOwned = (int) ((annualDividend * 100) / (nominalValue *
dividendPercentage));
[Link]("Number of Shares Owned: " + sharesOwned);
[Link]();
}
}
Variable Description Table
Variable Type Description
annualDividend Total dividend earned per year
double
nominalValue double Value of each share
dividendPercentage double Percentage of dividend per share
sharesOwned int Number of shares currently owned
Example Output
Enter Annual Dividend Received: 2000
Enter Nominal Value of Each Share: 10
Enter Dividend Percentage: 10
Number of Shares Owned: 2000
3. Swapping Two Variables Without a Third Variable
import [Link];
public class SwapNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number (a): ");
int a = [Link]();
[Link]("Enter second number (b): ");
int b = [Link]();
[Link]("Before Swapping: a = " + a + ", b = " + b);
a = a + b;
b = a - b;
a = a - b;
[Link]("After Swapping: a = " + a + ", b = " + b);
[Link]();
}
}
Variable Description Table
Variable Type Description
a int First number input
b int Second number input
Example Output
Enter first number (a): 23
Enter second number (b): 56
Before Swapping: a = 23, b = 56
After Swapping: a = 56, b = 23
4. Volume Calculation Using Switch Case
java
import [Link];
public class VolumeCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Choose solid type: \n1. Cuboid\n2. Cylinder\n3. Cone");
int choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter length: ");
double length = [Link]();
[Link]("Enter breadth: ");
double breadth = [Link]();
[Link]("Enter height: ");
double height = [Link]();
double cuboidVolume = length * breadth * height;
[Link]("Volume of Cuboid: " + cuboidVolume);
break;
case 2:
[Link]("Enter radius: ");
double radius = [Link]();
[Link]("Enter height: ");
height = [Link]();
double cylinderVolume = [Link] * radius * radius * height;
[Link]("Volume of Cylinder: " + cylinderVolume);
break;
case 3:
[Link]("Enter radius: ");
radius = [Link]();
[Link]("Enter height: ");
height = [Link]();
double coneVolume = (1.0/3) * [Link] * radius * radius * height;
[Link]("Volume of Cone: " + coneVolume);
break;
default:
[Link]("Invalid choice!");
}
[Link]();
}
}
Variable Description Table
Variable Type Description
choice int User selection of solid type
length, breadth, height double Dimensions for cuboid
radius double Radius for cylinder or cone
cuboidVolume, cylinderVolume, coneVolume double Calculated volume of respective solid
Example Output
Choose solid type:
1. Cuboid
2. Cylinder
3. Cone
Enter choice: 2
Enter radius: 3
Enter height: 7
Volume of Cylinder: 197.92
1. Power, Square Root, and Cube Root (Exercise 15)
java
import [Link];
public class MathOperations {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter value of p: ");
double p = [Link]();
[Link]("Enter value of q: ");
double q = [Link]();
[Link]("p^q: " + [Link](p, q));
[Link]("Square root of p: " + [Link](p));
[Link]("Cube root of q: " + [Link](q));
}
}
Variable Type Description
p double First input number
q double Second input number
sc Scanner Scanner object for input handling
Example Output:
Enter value of p: 4
Enter value of q: 2
p^q: 16.0
Square root of p: 2.0
Cube root of q: 1.2599210498948732
2. Finding the Minimum of Three Numbers (Exercise 16)
java
import [Link];
public class MinimumFinder {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter three numbers: ");
int a = [Link]();
int b = [Link]();
int c = [Link]();
int min = [Link](a, [Link](b, c));
[Link]("Minimum number: " + min);
}
}
Variable Type Description
a, b, c int Three input numbers
min int Minimum of the three numbers
Example Output:
Enter three numbers: 4 7 2
Minimum number: 2
3. Counting Positive & Negative Numbers (Exercise 17)
java
import [Link];
public class NumberCounter {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of inputs: ");
int n = [Link]();
int posCount = 0, negCount = 0, posSum = 0;
for (int i = 0; i < n; i++) {
[Link]("Enter a number: ");
int num = [Link]();
if (num > 0) {
posCount++;
posSum += num;
} else if (num < 0) {
negCount++;
}
}
[Link]("Positive numbers count: " + posCount);
[Link]("Negative numbers count: " + negCount);
[Link]("Sum of positive numbers: " + posSum);
}
}
Variable Type Description
n Number of input values
int
posCount int Count of positive numbers
negCount int Count of negative numbers
posSum int Sum of positive numbers
Example Output:
Enter number of inputs: 5
Enter a number: -3
Enter a number: 2
Enter a number: 8
Enter a number: -1
Enter a number: 0
Positive numbers count: 2
Negative numbers count: 2
Sum of positive numbers: 10
1. Sum of Digits (Exercise 18)
java
import [Link];
public class SumOfDigits {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
[Link]("Sum of digits: " + sum);
}
}
Variable Type Description
num int Input number
sum int Sum of digits of the number
Example Output:
Enter a number: 1234
Sum of digits: 10
2. Checking Armstrong Number (Exercise 19)
java
import [Link];
public class ArmstrongChecker {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int originalNum = num, sum = 0, digits = [Link](num).length();
while (num > 0) {
int digit = num % 10;
sum += [Link](digit, digits);
num /= 10;
}
if (sum == originalNum)
[Link](originalNum + " is an Armstrong number.");
else
[Link](originalNum + " is not an Armstrong number.");
}
}
Variable Type Description
num int Input number
originalNum int Copy of the input number
sum int Sum of powered digits
digits int Number of digits in the input
digit int Extracted digit from input
Example Output:
Enter a number: 153
153 is an Armstrong number.
3. Reverse of a Number (Exercise 20)
java
import [Link];
public class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int reversed = 0;
while (num > 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
[Link]("Reversed number: " + reversed);
}
}
Variable Type Description
num Input number
int
reversed int Reversed number
Example Output:
Enter a number: 9876
Reversed number: 6789
1. Menu-Driven Program (Composite Check & Smallest Digit)
java
import [Link];
public class MenuDrivenProgram {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Menu:");
[Link]("1) Check if a number is composite");
[Link]("2) Find the smallest digit in a number");
[Link]("Enter your choice: ");
int choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter a number: ");
int num = [Link]();
boolean isComposite = false;
for (int i = 2; i < num; i++) {
if (num % i == 0) {
isComposite = true;
break;
}
}
if (isComposite)
[Link](num + " is a composite number.");
else
[Link](num + " is not a composite number.");
break;
case 2:
[Link]("Enter a number: ");
int number = [Link]();
int smallest = 9;
while (number > 0) {
int digit = number % 10;
if (digit < smallest)
smallest = digit;
number /= 10;
}
[Link]("Smallest digit is: " + smallest);
break;
default:
[Link]("Invalid choice. Please try again.");
}
}
}
Variable Type Description
choice int User's selected menu option
Variable Type Description
num int Input number for composite check
isComposite boolean Flag indicating if the number is composite
number int Input number for smallest digit check
smallest int Stores smallest digit found
Example Output:
Menu:
1) Check if a number is composite
2) Find the smallest digit in a number
Enter your choice: 1
Enter a number: 9
9 is a composite number.
Menu:
1) Check if a number is composite
2) Find the smallest digit in a number
Enter your choice: 2
Enter a number: 6524
Smallest digit is: 2
2. Generating Pattern with * and #
java
public class PatternGenerator {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
[Link]((j % 2 == 1) ? "*" : "#");
[Link](" ");
}
[Link]();
}
}
}
Variable Type Description
rows int Number of rows in the pattern
i int Loop for rows
j int Loop for alternating characters
Example Output:
*
* #
* # *
* # * #
* # * # *
3. Generating Number Sequence Pattern
java
public class NumberSequencePattern {
public static void main(String[] args) {
int num = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
[Link](num + " ");
num++;
}
[Link]();
}
}
}
Variable Type Description
num int Number sequence counter
i int Loop for rows
j int Loop for numbers in each row
Example Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15