D
Deleted member 613
Guest
I recently posted a thread on how to multiply numbers in C#, so I thought i would take it one step further with this, the C# calculator!
Heres the code:
All explanation is included in the code
Heres the code:
C#:
using System;
class MainClass {
public static void Main (string[] args) {
bool repeat = true;
while(repeat == true){
Console.WriteLine ("Booting System...");
Console.WriteLine ("System Booted");
Console.WriteLine ("Press any key to continue");
Console.ReadLine();
Console.Clear();
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine ("Welcome to the C# Calculator.");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine ("Please state the numbers to be used:");
//Takes the numbers to be used and stores them in variables.
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
Console.Clear();
Console.WriteLine("The numbers recieved are: " + a + " " + b);
Console.WriteLine("The commands are as follow:");
Console.WriteLine("Multiply = *");
Console.WriteLine("Divide = /");
Console.WriteLine("Minus = -");
Console.WriteLine("Add = +");
Console.WriteLine("State your commands");
//Takes in the users input and checks it(see next for loop)
string k = Console.ReadLine();
if(k == "*")
{
//If they want to multiply the numbers:
int new1 = 0;
new1 = a * b;
Console.Clear();
Console.WriteLine("The answer is: " + new1);
}
else if(k == "/")
{
//If they want to divide the numbers:
int new2 = 0;
new2 = a / b;
Console.Clear();
Console.WriteLine("The answer is: " + new2);
}
else if(k == "-")
{
//If they want to minus the numbers:
int new3 = 0;
new3 = a - b;
Console.Clear();
Console.WriteLine("The answer is: " + new3);
}
else if(k == "+")
{
////If they want to add the numbers:
int new4 = 0;
new4 = a + b;
Console.Clear();
Console.WriteLine("The answer is: " + new4);
}
Console.WriteLine("Would you like to restart the calculator? Y/N");
string repeatanswer = Console.ReadLine();
if(repeatanswer == "Y")
{
repeat = true;
Console.Clear();
}
else
{
repeat = false;
Console.Clear();
}
}
}
}
Last edited by a moderator: