Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

lab task

SP22-BSE-104
DR WAQAS JADOON
QUESTION NO1:

public class CircleAreaCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the radius of the circle: ");

double radius = scanner.nextDouble();

double area = getArea(radius);

System.out.printf("The area of the circle with radius %.2f is: %.2f\n", radius, area);

int roundedArea = (int) area;

System.out.println("Rounded area: " + roundedArea);

public static double getArea(double radius) {

double pi = Math.PI;

double area = pi * Math.pow(radius, 2);

return area;

OUTPUT:
QUESTION NO2:

public class LargestNumberFinder {

public static void main(String[] args) {

int a = 5, b = 1, c = 10;

int largest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);

System.out.println("The largest number among " + a + ", " + b + ", and " + c + " is: " + largest);

}
QUESTION NO3: /*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license

*/

package com.mycompany.cgpacalculator;

import java.util.Scanner;

/**

* @author TECH LINHS

*/

public class CGPACalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


System.out.println("Enter your grades for the following assessments (out of 100):");

System.out.print("Quiz: ");

int quiz = scanner.nextInt();

System.out.print("Assignment: ");

int assignment = scanner.nextInt();

System.out.print("Mid-Term: ");

int midTerm = scanner.nextInt();

System.out.print("Final: ");

int finalExam = scanner.nextInt();

int total = quiz + assignment + midTerm + finalExam;

double cgpa;

if (total >= 85) {

cgpa = 4.0;

} else if (total >= 70) {

cgpa = 3.0;

} else if (total >= 50) {

cgpa = 2.0;

} else {

cgpa = 0.0; // Fail

System.out.println("Your total score: " + total);

System.out.println("Your CGPA: " + cgpa);

}
QUESTION NO4:

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license

*/

package com.mycompany.mathquiz;

import java.util.Scanner;

/**

* @author TECH LINHS

*/

public class MathQuiz {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


int number1 = (int) (Math.random() * 10); // Generating random number1

int number2 = (int) (Math.random() * (number1 + 1)); // Generating random number2 (<= number1)

int correctAnswer = number1 - number2; // Calculating correct answer

System.out.print("Task 1: What is " + number1 + " - " + number2 + "? ");

int userAnswer = scanner.nextInt();

if (userAnswer == correctAnswer) {

System.out.println("Correct!");

} else {

System.out.println("Incorrect. The correct answer is: " + correctAnswer);

int operation = (int) (Math.random() * 2); // 0 for subtraction, 1 for addition

switch (operation) {

case 0:

number1 = (int) (Math.random() * 10); // Generating random number1

number2 = (int) (Math.random() * (number1 + 1)); // Generating random number2 (<=


number1)

correctAnswer = number1 - number2; // Calculating correct answer

System.out.print("Task 2: What is " + number1 + " - " + number2 + "? ");

userAnswer = scanner.nextInt();

if (userAnswer == correctAnswer) {

System.out.println("Correct!");

} else {
System.out.println("Incorrect. The correct answer is: " + correctAnswer);

break;

case 1:

number1 = (int) (Math.random() * 10); // Generating random number1

number2 = (int) (Math.random() * 10); // Generating random number2

correctAnswer = number1 + number2; // Calculating correct answer

System.out.print("Task 2: What is " + number1 + " + " + number2 + "? ");

userAnswer = scanner.nextInt();

if (userAnswer == correctAnswer) {

System.out.println("Correct!");

} else {

System.out.println("Incorrect. The correct answer is: " + correctAnswer);

break;

default:

System.out.println("Invalid operation.");

scanner.close();

}
EXERCISE:

import java.util.Scanner;

public class SubtractionGame {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Welcome to Subtraction Game!");

// Loop for continuous play

while (true) {

// Generate random numbers

int number1 = (int) (Math.random() * 10); // Single-digit number


int number2 = (int) (Math.random() * (number1 + 1)); // Single-digit number less than or equal to
number1

// Display question

System.out.print("What is " + number1 + " - " + number2 + "? ");

int answer = scanner.nextInt();

// Check answer

if (answer == (number1 - number2)) {

System.out.println("Congratulations! That's correct.");

} else {

System.out.println("Oops! That's incorrect. The correct answer is: " + (number1 - number2));

// Ask if the user wants to play again

System.out.print("Do you want to play again? (yes/no): ");

String playAgain = scanner.next().toLowerCase();

if (!playAgain.equals("yes")) {

System.out.println("Thank you for playing. Goodbye!");

break; // Exit the loop if the user doesn't want to play again

scanner.close();

2:

import java.util.Scanner;
public class ArithmeticGame {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Welcome to Arithmetic Game!");

// Loop for continuous play

while (true) {

// Generate random numbers

int number1 = (int) (Math.random() * 10); // Single-digit number

int number2 = (int) (Math.random() * 10); // Single-digit number

// Generate random operation

char operation;

int result;

switch ((int) (Math.random() * 4)) {

case 0:

operation = '+';

result = number1 + number2;

break;

case 1:

operation = '-';

result = number1 - number2;

break;

case 2:

operation = '*';

result = number1 * number2;

break;

case 3:
operation = '%';

result = number1 % number2;

break;

default:

operation = '+'; // Default operation is addition

result = number1 + number2;

break;

// Display question

System.out.print("What is " + number1 + " " + operation + " " + number2 + "? ");

int answer = scanner.nextInt();

// Check answer

if (answer == result) {

System.out.println("Congratulations! That's correct.");

} else {

System.out.println("Oops! That's incorrect. The correct answer is: " + result);

// Ask if the user wants to play again

System.out.print("Do you want to play again? (yes/no): ");

String playAgain = scanner.next().toLowerCase();

if (!playAgain.equals("yes")) {

System.out.println("Thank you for playing. Goodbye!");

break; // Exit the loop if the user doesn't want to play again

}
scanner.close();

Q1

Q2
Q3

You might also like