Simple calculator in java
- Program to implement a simple calculator in java.
Program
import
java.io.IOException;
import
java.util.Scanner;
public class
SimpleCalculator {
static float num1,num2,ans;
static int choice;
static char proceed;
private static
Scanner sc;
public static void
main(String[] args) throws IOException {
sc=new
Scanner(System.in);
do {
System.out.println("Enter
the choice:\n 1.Addition\n2.Subtraction\n3.Multiplication\n4.Division");
choice = sc.nextInt();
System.out.println("Enter
two numbers:");
num1=sc.nextFloat();
num2=sc.nextFloat();
switch(choice)
{
case 1: ans=num1+num2;
System.out.println("Sum
of "+num1+" and "+num2+"
is "+ans);
break;
case 2: ans=num1-num2;
System.out.println("Difference
between "+num1+"
and "+num2+"
is "+ans);
break;
case 3: ans=num1*num2;
System.out.println("Product
of "+num1+" and "+num2+"
is "+ans);
break;
case 4: ans=num1/num2;
System.out.println("Quotient
of "+num1+" and "+num2+"
is "+ans);
break;
default:System.out.println("Invalid
Choice");
break;
}
System.out.println("Do
you want to continue:(y/n)");
proceed=(char)
System.in.read();
}while(proceed=='Y'||proceed=='y');
}
}
Output
Enter the choice:
1.Addition
2.Subtraction
3.Multiplication
4.Division
1
Enter two numbers:
23
45
Sum of 23.0 and 45.0
is 68.0
Do you want to
continue:(y/n)
y
Enter the choice:
1.Addition
2.Subtraction
3.Multiplication
4.Division
4
Enter two numbers:
50
4
Quotient of 50.0 and
4.0 is 12.5
Do you want to
continue:(y/n)
n
Generate Prime numbers between two numbers
- Program to find whether the number entered by the user is a prime number or not. Extend this program to list all the prime numbers between two given numbers.
Program
import
java.io.IOException;
import
java.util.Scanner;
public class
PrimeNumber {
static int choice,num,min,max;
static char proceed;
private static
Scanner sc;
static int flag=0,i,j,num1,f=0;
//Function that checks
whether a number is prime or not
public void
findPrime(int num) {
for(i=1;i<=num;i++)
{
if(num%i==0)
{
flag++;
}
}
if(flag==2)
{
System.out.println("The
number is prime");
}
else {
System.out.println("The
number is not prime");
}
}
//Function that generates
prime numbers between two numbers
public void
primeSeries(int min,int max) {
int
temp=min;
min=max;
max=temp;
}
for(j=min+1;j<max;j++)
{
flag=0;
num1=j;
for(i=1;i<=num1;i++)
{//loop checks whether a number is prime
if(num1%i==0)
{
flag++;
}
}
if(flag==2)
{
if(f==0)
{
System.out.println("The
prime numbers between "+min+" and "+max+"
are:");
f=1;
}
System.out.println(+num1);
}
}
if(f!=1||max-min==1)
{
System.out.println("No
prime numbers between "+min+" and "+max);
}
}
public static void
main(String[] args) throws IOException {
sc=new
Scanner(System.in);
PrimeNumber ob=new
PrimeNumber();
do {
System.out.println("Enter
your choice:\n1.Find whether a number is prime\n2.Generate prime numbers
between two numbers");
choice=sc.nextInt();
switch(choice) {
case 1: System.out.println("Enter
the number to be checked for prime:");
num=sc.nextInt();
ob.findPrime(num);
break;
case 2: System.out.println("Enter
two numbers to generate prime numbers between those numbers:");
min=sc.nextInt();
max=sc.nextInt();
ob.primeSeries(min,max);
break;
default:System.out.println("Invalid
Choice");
break;
}
System.out.println("Do
you want to continue:(y/n)?");
proceed=(char)
System.in.read();
}while(proceed=='Y'||proceed=='y');
}
}
Output
Enter
your choice:
1.Find
whether a number is prime
2.Generate prime numbers between two numbers
1
Enter
the number to be checked for prime:
7
The
number is prime
Do
you want to continue:(y/n)?
y
Enter
your choice:
1.Find
whether a number is prime
2.Generate
prime numbers between two numbers
2
Enter
two numbers to generate prime numbers between those numbers:
2
9
The
prime numbers between 2 and 9 are:
3
5
7
Do
you want to continue:(y/n)?
n
No comments:
Post a Comment