0% found this document useful (0 votes)
2 views6 pages

Experiment 1

The document contains Java programs for three different tasks: a menu-driven simple calculator that performs basic mathematical operations, a program to find all prime numbers within a specified range, and solutions to two HackerRank problems that involve conditional statements and formatted output. Each program utilizes user input and loops to execute its respective functionality. The calculator and prime number finder continue to run until the user chooses to exit.

Uploaded by

tejasbhore24
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)
2 views6 pages

Experiment 1

The document contains Java programs for three different tasks: a menu-driven simple calculator that performs basic mathematical operations, a program to find all prime numbers within a specified range, and solutions to two HackerRank problems that involve conditional statements and formatted output. Each program utilizes user input and loops to execute its respective functionality. The calculator and prime number finder continue to run until the user chooses to exit.

Uploaded by

tejasbhore24
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/ 6

Q.

1 WAP to implement a menu-driven simple calculator that demonstrates all


basic mathematical operations.
import java.util.*;

public class Q1
{
public static void main(String[] args)
{
double x,y;
Scanner scanner = new Scanner(System.in);
while(true)
{
System.out.println("Enter the choice:\n1. Calculate\n2. Exit");
int n = scanner.nextInt();
if(n==2)
{
System.out.println("Thanks for using our calculator!!");
scanner.close();
System.exit(1);
}

System.out.println("Enter any operator: (+,-,*,/,%)");


String op = scanner.next();

System.out.println("Enter two numbers:");


x = scanner.nextDouble();
y = scanner.nextDouble();

double ans;
switch(op)
{
case "+":
ans = x + y;
System.out.println("Sum of "+x+" and "+y+" is "+ans);

break;

case "-":
ans = x - y;
System.out.println("Subtraction of "+x+" and "+y+" is "+ans);
break;

case "*":
ans = x * y;
System.out.println("Multiplication of "+x+" and "+y+" is "+ans);
break;
case "/":
ans = x / y;
System.out.println("Division of "+x+" and "+y+" is "+ans);
break;

case "%":
ans = x % y;
System.out.println("Remainder of "+x+" and "+y+" is "+ans);
break;

default:
System.out.println("You entered wrong input");
break;
}
System.out.println("\n\n");
}
}
}

Q.2 WAP to find all prime numbers between a given range of values as long as
the user wants.
import java.util.*;

public class Q2 {
public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

while(true)
{
System.out.println("Enter the choice:\n1. Calculate\n2. Exit");
int n = scan.nextInt();
if(n==2)
{
System.out.println("Thanks for using our calculator!!");
scan.close();
System.exit(1);
}
System.out.println("Enter the lower limit: ");
int lower = scan.nextInt();
System.out.println("Enter the upper limit: ");
int upper = scan.nextInt();
System.out.println("\nPrime numbers between the given limits are :");

for (int i = lower; i <= upper; i++)


if (Prime (i))
System.out.println(i);

System.out.println("\n\n");
}
}

static boolean Prime (int n)


{
if (n < 2)
return false;

for (int i = 2; i < n; i++)


{
if (n % i == 0)
return false;
}
return true;
}
}

}
}

Q.3 Hackerrank problem Java Solution


import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {


int n = scanner.nextInt();
if(n%2==0){
if(n>=2 && n<=5){
System.out.println("Not Weird");
}
else if(n>=6 && n<=20){
System.out.println("Weird");
}
else{
System.out.println("Not Weird");
}
}
else{
System.out.println("Weird");
}
}}
Q.3 Hackerrank problem Java Solution
import java.util.Scanner;

public class Solution {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
System.out.println("================================");
for(int i=0;i<3;i++){
String s1=sc.next();
int x=sc.nextInt();
//Complete this line
if(x>=100){
System.out.printf("%-15s%d\n",s1,x);
}else if(x>=10){
System.out.printf("%-15s0%d\n",s1,x);

}
else{
System.out.printf("%-15s00%d\n",s1,x);
}
}
System.out.println("================================");
}
}

You might also like