Assignment 1
1. Write a JAVA program to perform arithmetic operations.
import java.util.Scanner;
public class ArithmeticOperations {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
System.out.println("Addition: " + (a + b));
System.out.println("Subtraction: " + (a - b));
System.out.println("Multiplication: " + (a * b));
System.out.println("Division: " + (a / b));
// Sample Output:
// Enter first number: 10
// Enter second number: 5
// Addition: 15
// Subtraction: 5
// Multiplication: 50
// Division: 2
2. Write a JAVA program to check whether a number is even or odd.
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num % 2 == 0)
System.out.println(num + " is Even.");
else
System.out.println(num + " is Odd.");
// Sample Output:
// Enter a number: 7
// 7 is Odd.
3. Write a JAVA program to find the perimeter and area of different geometrical figures.
import java.util.Scanner;
public class Geometry {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Rectangle
System.out.print("Enter length and breadth of rectangle: ");
double l = sc.nextDouble();
double b = sc.nextDouble();
System.out.println("Rectangle Area: " + (l * b));
System.out.println("Rectangle Perimeter: " + (2 * (l + b)));
// Circle
System.out.print("Enter radius of circle: ");
double r = sc.nextDouble();
System.out.println("Circle Area: " + (Math.PI * r * r));
System.out.println("Circle Perimeter: " + (2 * Math.PI * r));
// Triangle
System.out.print("Enter base and height of triangle: ");
double base = sc.nextDouble();
double height = sc.nextDouble();
System.out.println("Triangle Area: " + (0.5 * base * height));
System.out.print("Enter three sides of triangle: ");
double a = sc.nextDouble();
double b1 = sc.nextDouble();
double c = sc.nextDouble();
System.out.println("Triangle Perimeter: " + (a + b1 + c));
// Sample Output:
// Enter length and breadth of rectangle: 4 5
// Rectangle Area: 20.0
// Rectangle Perimeter: 18.0
// Enter radius of circle: 3
// Circle Area: 28.27
// Circle Perimeter: 18.85
// Enter base and height of triangle: 4 5
// Triangle Area: 10.0
// Enter three sides of triangle: 3 4 5
// Triangle Perimeter: 12.0