0% found this document useful (0 votes)
3 views11 pages

Assignment Work Codes

The document contains multiple Java programs that perform various calculations including digital camera price calculation, simple pendulum time period calculation, employee salary calculation, compound interest calculation, and more. Each program includes variable descriptions and example outputs demonstrating their functionality. Additionally, there are exercises for mathematical operations, finding minimum values, counting positive and negative numbers, and checking for Armstrong numbers.

Uploaded by

ahansamanta14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

Assignment Work Codes

The document contains multiple Java programs that perform various calculations including digital camera price calculation, simple pendulum time period calculation, employee salary calculation, compound interest calculation, and more. Each program includes variable descriptions and example outputs demonstrating their functionality. Additionally, there are exercises for mathematical operations, finding minimum values, counting positive and negative numbers, and checking for Armstrong numbers.

Uploaded by

ahansamanta14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

Digital Camera Price Calculation


import java.util.Scanner;
public class CameraPriceCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter printed price of the digital camera: ");
double printedPrice = sc.nextDouble();
double discountedPrice = printedPrice * 0.90; // 10% discount
double finalPrice = discountedPrice * 1.06; // Adding 6% tax
System.out.println("Amount to be paid: " + finalPrice);
sc.close();
}
}

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 java.util.Scanner;
public class SimplePendulum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter length of pendulum (meters): ");
double length = sc.nextDouble();
System.out.print("Enter acceleration due to gravity (m/s^2): ");
double gravity = sc.nextDouble();
double timePeriod = 2 * Math.PI * Math.sqrt(length / gravity);
System.out.println("Time Period of the Pendulum: " + timePeriod + " seconds");
sc.close();
}
}

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 java.util.Scanner;
class Employee {
double basicPay, DA, HRA, CPF, grossPay, netPay;
void calculateSalary(double basicPay) {
this.basicPay = basicPay;
DA = basicPay * 0.30;
HRA = basicPay * 0.15;
CPF = basicPay * 0.125;
grossPay = basicPay + DA + HRA;
netPay = grossPay - CPF;
void displaySalary() {
System.out.println("Gross Pay: " + grossPay);
System.out.println("Net Pay: " + netPay);
}
}
public class EmployeeSalary {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Basic Pay: ");
double basicPay = sc.nextDouble();
Employee emp = new Employee();
emp.calculateSalary(basicPay);
emp.displaySalary();
sc.close();
}
}

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 java.util.Scanner;
public class CompoundInterest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter initial investment amount: ");
double principal = sc.nextDouble();
double rate = 5.0 / 100;
int years = 3;
double amountYear1 = principal * Math.pow((1 + rate), 1);
double amountYear2 = principal * Math.pow((1 + rate), 2);
double amountYear3 = principal * Math.pow((1 + rate), 3);
double interestYear1 = amountYear1 - principal;
double interestYear2 = amountYear2 - amountYear1;
double finalAmount = amountYear3;
System.out.println("Interest for first year: " + interestYear1);
System.out.println("Interest for second year: " + interestYear2);
System.out.println("Amount after three years: " + finalAmount);
sc.close();
}
}

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 java.util.Scanner;
public class CompoundInterest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Principal Amount: ");
double principal = sc.nextDouble();
System.out.print("Enter Rate of Interest (in %): ");
double rate = sc.nextDouble() / 100;
System.out.print("Enter Time (in years): ");
int time = sc.nextInt();
double amount = principal * Math.pow(1 + rate, time);
double interest = amount - principal;
System.out.println("Total Interest after " + time + " years: Rs " + interest);
sc.close();
}
}

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 java.util.Scanner;
public class SharesCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Annual Dividend Received: ");
double annualDividend = sc.nextDouble();
System.out.print("Enter Nominal Value of Each Share: ");
double nominalValue = sc.nextDouble();
System.out.print("Enter Dividend Percentage: ");
double dividendPercentage = sc.nextDouble();
int sharesOwned = (int) ((annualDividend * 100) / (nominalValue *
dividendPercentage));
System.out.println("Number of Shares Owned: " + sharesOwned);
sc.close();
}
}

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 java.util.Scanner;
public class SwapNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter first number (a): ");


int a = sc.nextInt();

System.out.print("Enter second number (b): ");


int b = sc.nextInt();

System.out.println("Before Swapping: a = " + a + ", b = " + b);

a = a + b;
b = a - b;
a = a - b;

System.out.println("After Swapping: a = " + a + ", b = " + b);

sc.close();
}
}

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 java.util.Scanner;

public class VolumeCalculator {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Choose solid type: \n1. Cuboid\n2. Cylinder\n3. Cone");


int choice = sc.nextInt();

switch (choice) {
case 1:
System.out.print("Enter length: ");
double length = sc.nextDouble();
System.out.print("Enter breadth: ");
double breadth = sc.nextDouble();
System.out.print("Enter height: ");
double height = sc.nextDouble();
double cuboidVolume = length * breadth * height;
System.out.println("Volume of Cuboid: " + cuboidVolume);
break;

case 2:
System.out.print("Enter radius: ");
double radius = sc.nextDouble();
System.out.print("Enter height: ");
height = sc.nextDouble();
double cylinderVolume = Math.PI * radius * radius * height;
System.out.println("Volume of Cylinder: " + cylinderVolume);
break;

case 3:
System.out.print("Enter radius: ");
radius = sc.nextDouble();
System.out.print("Enter height: ");
height = sc.nextDouble();
double coneVolume = (1.0/3) * Math.PI * radius * radius * height;
System.out.println("Volume of Cone: " + coneVolume);
break;

default:
System.out.println("Invalid choice!");
}

sc.close();
}
}

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 java.util.Scanner;

public class MathOperations {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter value of p: ");
double p = sc.nextDouble();
System.out.print("Enter value of q: ");
double q = sc.nextDouble();

System.out.println("p^q: " + Math.pow(p, q));


System.out.println("Square root of p: " + Math.sqrt(p));
System.out.println("Cube root of q: " + Math.cbrt(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 java.util.Scanner;

public class MinimumFinder {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter three numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();

int min = Math.min(a, Math.min(b, c));


System.out.println("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 java.util.Scanner;

public class NumberCounter {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of inputs: ");
int n = sc.nextInt();

int posCount = 0, negCount = 0, posSum = 0;

for (int i = 0; i < n; i++) {


System.out.print("Enter a number: ");
int num = sc.nextInt();

if (num > 0) {
posCount++;
posSum += num;
} else if (num < 0) {
negCount++;
}
}

System.out.println("Positive numbers count: " + posCount);


System.out.println("Negative numbers count: " + negCount);
System.out.println("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 java.util.Scanner;

public class SumOfDigits {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();

int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}

System.out.println("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 java.util.Scanner;

public class ArmstrongChecker {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();

int originalNum = num, sum = 0, digits = String.valueOf(num).length();

while (num > 0) {


int digit = num % 10;
sum += Math.pow(digit, digits);
num /= 10;
}

if (sum == originalNum)
System.out.println(originalNum + " is an Armstrong number.");
else
System.out.println(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 java.util.Scanner;

public class ReverseNumber {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();

int reversed = 0;
while (num > 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
System.out.println("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 java.util.Scanner;

public class MenuDrivenProgram {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Menu:");
System.out.println("1) Check if a number is composite");
System.out.println("2) Find the smallest digit in a number");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();

switch (choice) {
case 1:
System.out.print("Enter a number: ");
int num = sc.nextInt();
boolean isComposite = false;

for (int i = 2; i < num; i++) {


if (num % i == 0) {
isComposite = true;
break;
}
}

if (isComposite)
System.out.println(num + " is a composite number.");
else
System.out.println(num + " is not a composite number.");
break;

case 2:
System.out.print("Enter a number: ");
int number = sc.nextInt();
int smallest = 9;

while (number > 0) {


int digit = number % 10;
if (digit < smallest)
smallest = digit;
number /= 10;
}

System.out.println("Smallest digit is: " + smallest);


break;

default:
System.out.println("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++) {
System.out.print((j % 2 == 1) ? "*" : "#");
System.out.print(" ");
}
System.out.println();
}
}
}
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++) {
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
}
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

You might also like