0% found this document useful (0 votes)
10 views9 pages

Level 3 Paractice

The document contains a series of Java programs that demonstrate various functionalities, including temperature conversion between Celsius and Fahrenheit, calculating total income, swapping numbers, calculating rounds in a triangular park, distributing chocolates, calculating simple interest, weight conversion from pounds to kilograms, and determining the maximum number of handshakes among students. Each program includes code snippets, user input prompts, and sample outputs. The document serves as a practical guide for basic programming tasks in Java.

Uploaded by

Sharma Manish
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)
10 views9 pages

Level 3 Paractice

The document contains a series of Java programs that demonstrate various functionalities, including temperature conversion between Celsius and Fahrenheit, calculating total income, swapping numbers, calculating rounds in a triangular park, distributing chocolates, calculating simple interest, weight conversion from pounds to kilograms, and determining the maximum number of handshakes among students. Each program includes code snippets, user input prompts, and sample outputs. The document serves as a practical guide for basic programming tasks in Java.

Uploaded by

Sharma Manish
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/ 9

LEVEL _3

Name – MANISH KUMAR SHARMA


Registration number – RA2411026010285
Section – AH1
Question 1: Write a program that takes the temperature in Celsius as input and outputs the
temperature in Fahrenheit.

Code:

import java.util.Scanner;

public class CelsiusToFahrenheit {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter temperature in Celsius: ");

double celsius = sc.nextDouble();

double fahrenheitResult = (celsius * 9/5) + 32;

System.out.println("The " + celsius + " Celsius is " + fahrenheitResult + " Fahrenheit");

Output:

Enter temperature in Celsius: 100

The 100.0 Celsius is 212.0 Fahrenheit

Question 2: Write a program that takes the temperature in Fahrenheit as input and outputs the
temperature in Celsius.

Code:

import java.util.Scanner;

public class FahrenheitToCelsius {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.print("Enter temperature in Fahrenheit: ");

double fahrenheit = sc.nextDouble();

double celsiusResult = (fahrenheit - 32) * 5/9;

System.out.println("The " + fahrenheit + " Fahrenheit is " + celsiusResult + " Celsius");

Output:

Enter temperature in Fahrenheit: 212

The 212.0 Fahrenheit is 100.0 Celsius

Question 3: Write a program to find the total income of a person by taking salary and bonus as user
input.

Code:

import java.util.Scanner;

public class TotalIncome {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter Salary: ");

double salary = sc.nextDouble();

System.out.print("Enter Bonus: ");

double bonus = sc.nextDouble();

double totalIncome = salary + bonus;

System.out.println("The salary is INR " + salary + " and bonus is INR " + bonus + ". Hence Total
Income is INR " + totalIncome);

Output:

Enter Salary: 50000

Enter Bonus: 5000

The salary is INR 50000.0 and bonus is INR 5000.0. Hence Total Income is INR 55000.0
Question 4: Rewrite the Sample Program 2 with user inputs

Code:

import java.util.Scanner;

public class EricTravels {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter distance from Chennai to Vellore (in km): ");

double chennaiToVellore = scanner.nextDouble();

System.out.print("Enter time taken from Chennai to Vellore (hours): ");

int hours1 = scanner.nextInt();

System.out.print("Enter time taken from Chennai to Vellore (minutes): ");

int minutes1 = scanner.nextInt();

System.out.print("Enter distance from Vellore to Bangalore (in km): ");

double velloreToBangalore = scanner.nextDouble();

System.out.print("Enter time taken from Vellore to Bangalore (hours): ");

int hours2 = scanner.nextInt();

System.out.print("Enter time taken from Vellore to Bangalore (minutes): ");

int minutes2 = scanner.nextInt();

double totalDistance = chennaiToVellore + velloreToBangalore;

int totalMinutes = (hours1 * 60 + minutes1) + (hours2 * 60 + minutes2);

int totalHours = totalMinutes / 60;

int remainingMinutes = totalMinutes % 60;

System.out.println("\nTotal Distance from Chennai to Bangalore: " + totalDistance + " km");

System.out.println("Total Time from Chennai to Bangalore: " + totalHours + " hours " +
remainingMinutes + " minutes");

scanner.close();

Output:

Enter distance from Chennai to Vellore (in km): 156.6

Enter time taken from Chennai to Vellore (hours): 4


Enter time taken from Chennai to Vellore (minutes): 4

Enter distance from Vellore to Bangalore (in km): 211.8

Enter time taken from Vellore to Bangalore (hours): 4

Enter time taken from Vellore to Bangalore (minutes): 25

Total Distance from Chennai to Bangalore: 368.4 km

Total Time from Chennai to Bangalore: 8 hours 29 minutes

Question 5: Write a program to swap two numbers.

Code:

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: ");

int number1 = sc.nextInt();

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

int number2 = sc.nextInt();

int temp = number1;

number1 = number2;

number2 = temp;

System.out.println("The swapped numbers are " + number1 + " and " + number2);

Output:

Enter first number: 5

Enter second number: 10

The swapped numbers are 10 and 5


Question 6: Write a program to calculate how many rounds an athlete must complete to run 5 km
given the sides of a triangular park.

Code:

import java.util.Scanner;

public class AthleteRun {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter side1 of the park: ");

double side1 = sc.nextDouble();

System.out.print("Enter side2 of the park: ");

double side2 = sc.nextDouble();

System.out.print("Enter side3 of the park: ");

double side3 = sc.nextDouble();

double perimeter = side1 + side2 + side3;

int rounds = (int) Math.ceil(5000 / perimeter);

System.out.println("The total number of rounds the athlete will run is " + rounds + " to complete
5 km");

Output:

Enter side1 of the park: 300

Enter side2 of the park: 400

Enter side3 of the park: 500

The total number of rounds the athlete will run is 5 to complete 5 km

Question 7: Write a program to divide N chocolates among M children and display how many
chocolates each child gets and how many remain.

Code:

import java.util.Scanner;

public class ChocolateDistribution {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter number of chocolates: ");

int numberOfChocolates = sc.nextInt();

System.out.print("Enter number of children: ");

int numberOfChildren = sc.nextInt();

int chocolatesEach = numberOfChocolates / numberOfChildren;

int remainingChocolates = numberOfChocolates % numberOfChildren;

System.out.println("The number of chocolates each child gets is " + chocolatesEach + " and the
number of remaining chocolates are " + remainingChocolates);

Output:

Enter number of chocolates: 20

Enter number of children: 3

The number of chocolates each child gets is 6 and the number of remaining chocolates are 2

Question 8: Write a program to input the Principal, Rate, and Time values and calculate Simple
Interest.

Code:

import java.util.Scanner;

public class SimpleInterest {

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: ");

double rate = sc.nextDouble();

System.out.print("Enter Time in years: ");

double time = sc.nextDouble();

double simpleInterest = (principal * rate * time) / 100;


System.out.println("The Simple Interest is " + simpleInterest + " for Principal " + principal + ",
Rate of Interest " + rate + " and Time " + time);

Output:

Enter Principal amount: 10000

Enter Rate of interest: 5

Enter Time in years: 2

The Simple Interest is 1000.0 for Principal 10000.0, Rate of Interest 5.0 and Time 2.0

Weight Conversion: Pounds to Kilograms

Question 9: Write a program to convert weight from pounds to kilograms.

Code:

import java.util.Scanner;

public class WeightConversion {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter weight in pounds: ");

double pounds = sc.nextDouble();

double kilograms = pounds / 2.2;

System.out.println("The weight of the person in pounds is " + pounds + " and in kg is " +
kilograms);

Output:

Enter weight in pounds: 150

The weight of the person in pounds is 150.0 and in kg is 68.18

Maximum Handshakes Calculation


Question 10: Write a program to find the maximum number of handshakes among N students.

Code:

import java.util.Scanner;

public class HandshakeCalculation {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter number of students: ");

int numberOfStudents = sc.nextInt();

int handshakes = (numberOfStudents * (numberOfStudents - 1)) / 2;

System.out.println("The maximum number of handshakes possible is " + handshakes);

Output:

Enter number of students: 5

The maximum number of handshakes possible is 10

Weight Conversion: Pounds to Kilograms

Question 11: Write a program to convert weight from pounds to kilograms.

Code:

import java.util.Scanner;

public class WeightConversion {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter weight in pounds: ");

double pounds = sc.nextDouble();

double kilograms = pounds / 2.2;

System.out.println("The weight of the person in pounds is " + pounds + " and in kg is " +
kilograms);

}
}

Output:

Enter weight in pounds: 150

The weight of the person in pounds is 150.0 and in kg is 68.18

You might also like