Telegram Group & Telegram Channel
11. Program to Create a Simple Calculator.
import java.util.Scanner;
import java.io.*;
class Calculator
{
public static void main(String[] args)
{
int choice;
int x = 0;
int y = 0;
int sum;
PrintStream out;
Scanner input;
Calculator calc = new Calculator();
try
{
out = new PrintStream("calclog.txt");
do
{
System.out.println("Calculator Program"); System.out.println("--------------------\n");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Mod");
System.out.println("6. Power");
System.out.println("99. End Program\n");
System.out.println("Enter Choice: ");
input = new Scanner(System.in);
choice = input.nextInt();
while ((choice < 1 || choice > 6) && choice != 99)
{
System.out.println("Please enter 1, 2, 3, 4, 5, or 6: ");
choice = input.nextInt();
}
if (choice != 99)
{
System.out.println("Please enter 2 numbers only: ");
x = input.nextInt();
y = input.nextInt();
}
switch (choice)
{
case 1:
sum = calc.add(x, y);
System.out.printf("The sum is %d\n\n", sum);
out.println(x + "+" + y + "=" + sum);
break;
case 2:
sum = calc.sub(x, y);
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "-" + y + "=" + sum);
break;
case 3:
sum = calc.multi(x, y);
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "*" + y + "=" + sum);
break;
case 4:
try
{
sum = calc.div(x, y);
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "/" + y + "=" + sum);
}
catch (Exception e)
{
System.out.println("\nError: Cannot Divide by zero\n\n");
}
break;
case 5:
sum = calc.mod(x, y);
System.out.printf("The mod is %d\n\n", sum);
out.println(x + "%" + y + "=" + sum);
break;
case 6:
sum = calc.pow(x, y)
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "^" + y + "=" + sum);
break;
}
}
while (choice != 99);
input.close();
System.out.println("Ending program...");
}
catch (Exception e){
System.out.println("ERROR: Some error occured");
e.printStackTrace();
}
}
public int add(int num1, int num2)
{int sum;
sum = num1 + num2;
return sum;
}
public int sub(int num1, int num2)
{ int sum;
sum = num1 - num2;
return sum;
}
public int multi(int num1, int num2)
{ int sum;
sum = num1 * num2;
return sum;
}
public int div(int num1, int num2)
{int sum;
sum = num1 / num2;
return sum;
}
public int mod(int num1, int num2)
{int sum;
sum = num1 % num2;
return sum;
}
public int pow(int base, int exp)
{int sum = 1;
if (exp == 0)
{sum = 1;
}
while (exp > 0)
{sum = sum * base;
exp--;
}
return sum;
}
}
@java_codings



tg-me.com/java_codings/19
Create:
Last Update:

11. Program to Create a Simple Calculator.
import java.util.Scanner;
import java.io.*;
class Calculator
{
public static void main(String[] args)
{
int choice;
int x = 0;
int y = 0;
int sum;
PrintStream out;
Scanner input;
Calculator calc = new Calculator();
try
{
out = new PrintStream("calclog.txt");
do
{
System.out.println("Calculator Program"); System.out.println("--------------------\n");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Mod");
System.out.println("6. Power");
System.out.println("99. End Program\n");
System.out.println("Enter Choice: ");
input = new Scanner(System.in);
choice = input.nextInt();
while ((choice < 1 || choice > 6) && choice != 99)
{
System.out.println("Please enter 1, 2, 3, 4, 5, or 6: ");
choice = input.nextInt();
}
if (choice != 99)
{
System.out.println("Please enter 2 numbers only: ");
x = input.nextInt();
y = input.nextInt();
}
switch (choice)
{
case 1:
sum = calc.add(x, y);
System.out.printf("The sum is %d\n\n", sum);
out.println(x + "+" + y + "=" + sum);
break;
case 2:
sum = calc.sub(x, y);
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "-" + y + "=" + sum);
break;
case 3:
sum = calc.multi(x, y);
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "*" + y + "=" + sum);
break;
case 4:
try
{
sum = calc.div(x, y);
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "/" + y + "=" + sum);
}
catch (Exception e)
{
System.out.println("\nError: Cannot Divide by zero\n\n");
}
break;
case 5:
sum = calc.mod(x, y);
System.out.printf("The mod is %d\n\n", sum);
out.println(x + "%" + y + "=" + sum);
break;
case 6:
sum = calc.pow(x, y)
System.out.printf("The answer is %d\n\n", sum);
out.println(x + "^" + y + "=" + sum);
break;
}
}
while (choice != 99);
input.close();
System.out.println("Ending program...");
}
catch (Exception e){
System.out.println("ERROR: Some error occured");
e.printStackTrace();
}
}
public int add(int num1, int num2)
{int sum;
sum = num1 + num2;
return sum;
}
public int sub(int num1, int num2)
{ int sum;
sum = num1 - num2;
return sum;
}
public int multi(int num1, int num2)
{ int sum;
sum = num1 * num2;
return sum;
}
public int div(int num1, int num2)
{int sum;
sum = num1 / num2;
return sum;
}
public int mod(int num1, int num2)
{int sum;
sum = num1 % num2;
return sum;
}
public int pow(int base, int exp)
{int sum = 1;
if (exp == 0)
{sum = 1;
}
while (exp > 0)
{sum = sum * base;
exp--;
}
return sum;
}
}
@java_codings

BY Advance Java πŸ‘¨β€πŸ’»


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/java_codings/19

View MORE
Open in Telegram


Advance Java ‍ Telegram | DID YOU KNOW?

Date: |

Tata Power whose core business is to generate, transmit and distribute electricity has made no money to investors in the last one decade. That is a big blunder considering it is one of the largest power generation companies in the country. One of the reasons is the company's huge debt levels which stood at β‚Ή43,559 crore at the end of March 2021 compared to the company’s market capitalisation of β‚Ή44,447 crore.

Look for Channels Online

You guessed it – the internet is your friend. A good place to start looking for Telegram channels is Reddit. This is one of the biggest sites on the internet, with millions of communities, including those from Telegram.Then, you can search one of the many dedicated websites for Telegram channel searching. One of them is telegram-group.com. This website has many categories and a really simple user interface. Another great site is telegram channels.me. It has even more channels than the previous one, and an even better user experience.These are just some of the many available websites. You can look them up online if you’re not satisfied with these two. All of these sites list only public channels. If you want to join a private channel, you’ll have to ask one of its members to invite you.

Advance Java ‍ from ru


Telegram Advance Java πŸ‘¨β€πŸ’»
FROM USA