Level 2 Practice Programs
1. Write a program to create a basic calculator that can perform addition, subtraction,
multiplication, and division. The program should ask for two numbers (floating point)
and perform all the operations
Code
import [Link];
public class sCalculator {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int a,b;
[Link]("enter a number");
a = [Link]();
[Link]("enter another number");
b= [Link]();
int addition= a+b ;
int substraction = a-b;
int product=a*b;
float quotient= (float) a / b;
[Link]("The addition is" + addition );
[Link]("The substraction is" +substraction );
[Link]("The multiplication is" + product);
[Link]("The quotient is" + quotient );
[Link]();
}
}
2. Write a program that takes the base and height to find area of a triangle in square inches and
square centimeters
Code
import [Link];
public class TriangleArea {
public static void main(String[] args) {
final double INCH_TO_CM = 2.54;
Scanner scanner = new Scanner([Link]);
[Link]("Enter base of the triangle (in inches): ");
double base = [Link]();
[Link]("Enter height of the triangle (in inches): ");
double height = [Link]();
double areaInInches = 0.5 * base * height;
double areaInCm = areaInInches * (INCH_TO_CM * INCH_TO_CM);
[Link]("Area of the triangle in square inches: " + areaInInches);
[Link]("Area of the triangle in square centimeters: " + areaInCm);
[Link]();
}
3. Write a program to find the side of the square whose parameter you read
from user
Code
import [Link];
public class SquareSideCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the perimeter of the square: ");
double perimeter = [Link]();
double side = perimeter / 4;
[Link]("The side of the square is: " + side);
[Link]();
4. Write a program the find the distance in yards and miles for the distance
provided by user in feets
Code
import [Link];
public class DistanceConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the distance in feet: ");
double feet = [Link]();
double yards = feet / 3;
double miles = feet / 5280;
[Link]("Distance in yards: " + yards);
[Link]("Distance in miles: " + miles);
[Link]();
}
}
[Link] a program to input the unit price of an item and the quantity to be bought. Then,
calculate the total price.
Code
import [Link];
public class TotalPriceCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the unit price of the item: ");
double unitPrice = [Link]();
[Link]("Enter the quantity to be bought: ");
int quantity = [Link]();
double totalPrice = unitPrice * quantity;
[Link]("Total price: $" + totalPrice);
[Link]();
}
}
6. Write a program to take 2 numbers and print their quotient and reminder
Code
import [Link];
public class QuotientRemainderCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the dividend: ");
int dividend = [Link]();
[Link]("Enter the divisor: ");
int divisor = [Link]();
int quotient = dividend / divisor;
int remainder = dividend % divisor;
[Link]("Quotient: " + quotient);
[Link]("Remainder: " + remainder);
[Link]();
}
}
7. Write an IntOperation program by taking a, b, and c as input values and print the
following integer operations a + b *c, a * b + c, c + a / b, and a % b + c. Please also
understand the precedence of the operators.
Code
import [Link];
public class IntOperation {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter value for a: ");
int a = [Link]();
[Link]("Enter value for b: ");
int b = [Link]();
[Link]("Enter value for c: ");
int c = [Link]();
int result1 = a + b * c;
int result2 = a * b + c;
int result3 = c + a / b;
int result4 = a % b + c;
[Link]("a + b * c = " + result1);
[Link]("a * b + c = " + result2);
[Link]("c + a / b = " + result3);
[Link]("a % b + c = " + result4);
[Link]();
}
}
[Link], write the DoubleOpt program by taking double values and doing the same
operations.
Code
import [Link];
public class DoubleOpt {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the value of a: ");
double a = [Link]();
[Link]("Enter the value of b: ");
double b = [Link]();
[Link]("Enter the value of c: ");
double c = [Link]();
double result1 = a + b * c;
double result2 = a * b + c;
double result3 = c + a / b;
double result4 = a % b + c;
[Link]("a + b * c = " + result1);
[Link]("a * b + c = " + result2);
[Link]("c + a / b = " + result3);
[Link]("a % b + c = " + result4);
[Link]();
}
}