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

Java Assignment1 Solutions

The document contains three JAVA programs: one for performing basic arithmetic operations, another for checking if a number is even or odd, and a third for calculating the perimeter and area of various geometrical figures. Each program includes user input prompts and sample output to demonstrate functionality. The programs utilize the Scanner class for input and perform calculations based on the provided values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Java Assignment1 Solutions

The document contains three JAVA programs: one for performing basic arithmetic operations, another for checking if a number is even or odd, and a third for calculating the perimeter and area of various geometrical figures. Each program includes user input prompts and sample output to demonstrate functionality. The programs utilize the Scanner class for input and perform calculations based on the provided values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy