Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views10 pages

Coding Exercise (Individual)

Uploaded by

chochomocho11
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)
6 views10 pages

Coding Exercise (Individual)

Uploaded by

chochomocho11
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/ 10

CODING EXERCISE (INDIVIDUAL)

1. Sum of Two Numbers Ask the user for two integers,


then display their sum.
Example Output:
import java.util.Scanner;

public class SumOfTwoNumbers {


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 sum = num1 + num2;


System.out.println("\nThe sum is: " + sum);
}
}
Enter first number: 10
Enter second number: 20
The sum is: 30
2. Area of a Rectangle
Ask the user for the length and width, then compute and
display the area.
Example Output:
import java.util.Scanner;

public class RectangleArea {


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

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


int length = scanner.nextInt();

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


int width = scanner.nextInt();

int area = length * width;


System.out.println("\nArea of the rectangle is: " + area);
}
}
Enter length: 5
Enter width: 3
Area of the rectangle is: 15
3. Circle Area Calculator
Ask the user for the radius, then calculate the area using
π × r × r.
Example Output:
import java.util.Scanner;

public class CircleArea {


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

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


double radius = scanner.nextDouble();

double area = Math.PI * radius * radius;


System.out.printf("\nArea of the circle is: %.2f\n", area);
}
}
Enter radius: 4
Area of the circle is: 50.24
4. Age Calculator
Ask the user for their birth year, then calculate their
current age.
Example Output:
import java.util.Scanner;
import java.time.Year;

public class AgeCalculator {


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

System.out.print("Enter your birth year: ");


int birthYear = scanner.nextInt();

int currentYear = Year.now().getValue(); // gets current year dynamically


int age = currentYear - birthYear;

System.out.println("You are " + age + " years old.");

scanner.close();
}
}
Enter your birth year: 2005
You are 20 years old.
5. Simple Interest Calculator
Ask the user for principal amount, rate, and time (in
years), then calculate:
Simple Interest = (P × R × T) / 100
Example Output:
import java.util.Scanner;

public class SimpleInterestCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter principal: ");
double principal = scanner.nextDouble();

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


double rate = scanner.nextDouble();

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


double time = scanner.nextDouble();

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

System.out.println("\nSimple interest is: " + simpleInterest);

scanner.close();
}
}
Enter principal: 1000
Enter rate: 5
Enter time (in years): 2
Simple interest is: 100.0
6. Temperature Converter Ask the user for temperature
in Celsius, convert it to Fahrenheit. Formula: F = (C × 9/5)
+ 32
Example Output:
import java.util.Scanner;

public class TemperatureConverter {


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

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


double celsius = scanner.nextDouble();

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

System.out.println("Temperature in Fahrenheit: " + fahrenheit);

scanner.close();
}
}
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.0
7. Perimeter of a Square
Ask the user for the side length, then compute and
display the perimeter.
Example Output:
import java.util.Scanner;

public class SquarePerimeter {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter side length: ");
int side = scanner.nextInt();

int perimeter = 4 * side;


System.out.println("Perimeter of square: " + perimeter);

scanner.close();
}
}
Enter side length: 6
Perimeter of square: 24
8. Total and Average
Ask the user for three grades, then compute and display
the total and average.
Example Output:
import java.util.Scanner;

public class TotalAndAverage {


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

System.out.print("Enter first grade: ");


int grade1 = scanner.nextInt();

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


int grade2 = scanner.nextInt();

System.out.print("Enter third grade: ");


int grade3 = scanner.nextInt();

int total = grade1 + grade2 + grade3;


double average = total / 3.0;

System.out.println("Total: " + total);


System.out.println("\nAverage: " + average);

scanner.close();
}
}
Enter first grade: 85
Enter second grade: 90
Enter third grade: 95
Total: 270
Average: 90.0
9. Kilometers to Meters Converter
Ask the user for kilometers, then convert it to meters
(meters = km × 1000)
Example Output:
import java.util.Scanner;
public class KmToMetersConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter distance in kilometers: ");


double km = scanner.nextDouble();

double meters = km * 1000;


System.out.println("\nDistance in meters: " + (int)meters);

scanner.close();
}
}
Enter distance in kilometers: 5
Distance in meters: 5000
10. Salary After Bonus
Ask the user for monthly salary and bonus percentage,
then display the total salary.
Example Output:
import java.util.Scanner;

public class SalaryAfterBonus {


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

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


double salary = scanner.nextDouble();

System.out.print("Enter bonus percentage: ");


double bonusPercent = scanner.nextDouble();

double totalSalary = salary + (salary * bonusPercent / 100);


System.out.println("\nTotal salary after bonus: " + totalSalary);

scanner.close();
}
}
Enter salary: 20000
Enter bonus percentage: 10
Total salary after bonus: 22000.0
11. Rectangle Perimeter
Ask the user for length and width, then compute and
display the perimeter.
Formula: Perimeter = 2 x (length + width)
Example Output:
import java.util.Scanner;

public class RectanglePerimeter {


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

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


int width = scanner.nextInt();

int perimeter = 2 * (length + width);


System.out.println("\nPerimeter of rectangle: " + perimeter);

scanner.close();
}
}
Enter length: 8
Enter width: 5
Perimeter of rectangle: 26
12. Square Area & Perimeter Ask the user for the side,
then compute both the area and the perimeter.
Example Output:
import java.util.Scanner;

public class SquareAreaPerimeter {


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

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


int side = scanner.nextInt();

int area = side * side;


int perimeter = 4 * side;

System.out.println("Area: " + area);


System.out.println("Perimeter: " + perimeter);

scanner.close();
}
}
Enter side: 4
Area: 16
Perimeter: 16
13. Minutes to Hours Converter Ask the user for minutes,
then convert to hours (as a decimal). Formula: hours =
minutes / 60
Example Output:
import java.util.Scanner;

public class MinutesToHoursConverter {


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

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


double minutes = scanner.nextDouble();

double hours = minutes / 60;


System.out.println("Hours: " + hours);

scanner.close();
}
}
Enter minutes: 150
Hours: 2.5
15. BMI Calculator
Ask the user for weight (kg) and height (m), then
compute BMI. Formula: BMI = weight / (height × height)
Example Output:
import java.util.Scanner;

public class BMICalculator {


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

System.out.print("Enter weight (kg): ");


double weight = scanner.nextDouble();

System.out.print("Enter height (m): ");


double height = scanner.nextDouble();

double bmi = weight / (height * height);


System.out.printf("\nYour BMI is: %.2f\n", bmi);

scanner.close();
}
}
Enter weight (kg): 60
Enter height (m): 1.65
Your BMI is: 22.04
16. Days to Weeks Converter
Ask the user for days, then convert to weeks (as a
decimal). Formula: weeks = days / 7
Example Output:
import java.util.Scanner;

public class DaysToWeeksConverter {


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

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


double days = scanner.nextDouble();

double weeks = days / 7;


System.out.println("\nWeeks: " + weeks);
scanner.close();
}
}
Enter days: 21
Weeks: 3.0
17. Cylinder Volume
Ask the user for radius and height, then compute volume.
Formula: Volume = × r2 × h
Example Output:
import java.util.Scanner;

public class CylinderVolume {


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

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


double radius = scanner.nextDouble();

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


double height = scanner.nextDouble();

double volume = Math.PI * radius * radius * height;


System.out.printf("\nVolume of cylinder: %.1f\n", volume);

scanner.close();
}
}
Enter radius: 3
Enter height: 5
Volume of cylinder: 141.3
18. Average of Four Numbers
Ask the user for four numbers, then compute and display
the average.
Example Output:
import java.util.Scanner;

public class AverageOfFourNumbers {


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

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


double num1 = scanner.nextDouble();

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


double num2 = scanner.nextDouble();

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


double num3 = scanner.nextDouble();
System.out.print("Enter fourth number: ");
double num4 = scanner.nextDouble();

double average = (num1 + num2 + num3 + num4) / 4;


System.out.println("\nAverage: " + average);

scanner.close();
}
}
Enter first number: 5
Enter second number: 7
Enter third number: 9
Enter fourth number: 11
Average: 8.0
19. Convert Seconds to Minutes and Seconds
Ask the user for total seconds, then display as minutes
and seconds.
Example Output:
import java.util.Scanner;

public class SecondsToMinutesSeconds {


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

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


int totalSeconds = scanner.nextInt();

int minutes = totalSeconds / 60;


int seconds = totalSeconds % 60;

System.out.println("Minutes: " + minutes);


System.out.println("\nSeconds: " + seconds);

scanner.close();
}
}
Enter seconds: 130
Minutes: 2
Seconds: 10
20. Grocery Total Calculator
Ask the user for the prices of three items, then compute
and display total.
Example Output:
import java.util.Scanner;

public class GroceryTotalCalculator {


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

System.out.print("Enter price of item 1: ");


double price1 = scanner.nextDouble();

System.out.print("Enter price of item 2: ");


double price2 = scanner.nextDouble();

System.out.print("Enter price of item 3: ");


double price3 = scanner.nextDouble();

double total = price1 + price2 + price3;


System.out.println("\nTotal price: " + total);

scanner.close();
}
}
Enter price of item 1: 45.5
Enter price of item 2: 20
Enter price of item 3: 34.5
Total price: 100.0

You might also like