• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.
    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# How to make a calculator in C#

  • Thread starter Deleted member 613
  • Start date
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:
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();
     }
    }
  }
}
:) All explanation is included in the code
 
Last edited by a moderator:

Mathematical

Active Coder
Hey, @GameZee.

There is a better way of going about this, which involves not cramming everything into one function/method. You could've divided your program into smaller pieces using functions, like this:
C#:
public static void main(String args[]) {
     //...
}

public int Addition(int N1, int N2) {
     //...
}

public int Subtraction(int N1, int N2) {
     //...
}

public int Multiplication(int N1, int N2) {
     //...
}

public int Division(int N1, int N2) {
     //...
}
This way is in my opinion, a much neater way to do it, as you're splitting your operators up into different sections. I know that there is probably a way, that's way better than mine, but you can just use the one that I've shown you in the meantime.

Also, for the section containing your if-else if statements, I'd say it'd be much better to use a switch-statement. And instead of using strings or symbols, you could just use a single-character or an integer-value, as they'll be less error-prone(In my opinion anyway). Here:
C#:
int K = int.parse(Console.ReadLine());

switch(K) {
     case 1:
          Addition(A, B);
          break;
     case 2:
          //...
     // Continue doing the same thing for the rest of the statement.
     default:
          // Don't forget your default-case too.
}

This was just some feedback on what you could've done instead to make the code a bit better. Also, merge the changes in your "better" version into your original post, please.
 
Top