Ideas for c# calculator

Hey, I’m making a calculator in C# using dotnetfiddle terminals, and I was wondering about any additions that you guys would make to it.

It is capable of addition, subtraction, multiplication, division, exponents, logarithms, and whole random number generation (set by user limits).

Here’s the code. If you can find of a way to streamline it, it would also be appreciated.

using System;
public class Program{
	public static void Main(){
		Console.WriteLine("Welcome to the basic calculator\n-------------------------------\n");
		while (true){
			Console.WriteLine("Here are the available functions: \n\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Exponent\n6. Logarithm\n7. Random number generator\n\nSelect a function by its number: ");
			string pemdas = Console.ReadLine();
			if (pemdas == "1"){
				Console.WriteLine("insert first number: ");
				double firstNum = (double)Convert.ToDouble(Console.ReadLine());
				Console.WriteLine("insert second number: ");
				double secondNum = (double)Convert.ToDouble(Console.ReadLine());
				Console.WriteLine("\nYour result is: " + (firstNum + secondNum) + "\n\n\n");
			}else if (pemdas == "2"){
				Console.WriteLine("insert starting value: ");
				double firstNum = (double)Convert.ToDouble(Console.ReadLine());
				Console.WriteLine("insert value removed: ");
				double secondNum = (double)Convert.ToDouble(Console.ReadLine());
				Console.WriteLine("\n your resulting value is: " + (firstNum - secondNum) + "\n\n\n");
			}else if (pemdas == "3"){
				Console.WriteLine("Enter your starting value: ");
				double firstNum = (double)Convert.ToDouble(Console.ReadLine());
				Console.WriteLine("How much are you multiplying this number by: ");
				double secondNum = (double)Convert.ToDouble(Console.ReadLine());
				Console.WriteLine("\nYour result is: " + (firstNum * secondNum) + "\n\n\n");
			}else if (pemdas == "4"){
				Console.WriteLine("Enter your starting value: ");
				double firstNum = (double)Convert.ToDouble(Console.ReadLine());
				Console.WriteLine("How much are you dividing this number by? ");
				double secondNum = (double)Convert.ToDouble(Console.ReadLine());
				Console.WriteLine("\nYour resulting value is: " + (firstNum / secondNum) + "\n\n\n");
			}else if (pemdas == "5"){
				Console.WriteLine("Enter your base: ");
				if (Console.ReadLine() == "e"){
					double baseNum = 2.71828;
					Console.WriteLine("Enter the power: ");
					double exponent = (double)Convert.ToDouble(Console.ReadLine());
					Console.WriteLine("\nYour result is: " + Math.Pow(baseNum, exponent) + "\n\n\n");
				}else{
					double baseNum = (double)Convert.ToDouble(Console.ReadLine());
					Console.WriteLine("Enter the power: ");
					double exponent = (double)Convert.ToDouble(Console.ReadLine());
					Console.WriteLine("\nYour result is: " + Math.Pow(baseNum, exponent) + "\n\n\n");
				}
			}else if (pemdas == "6"){
				Console.WriteLine("Enter your log base: ");
				if(Console.ReadLine() == "e"){
					double logBase = 2.71828;
					Console.WriteLine("What is the other thing: ");
					double thingValue = (double)Convert.ToDouble(Console.ReadLine());
					Console.WriteLine("The result is: " + Math.Log(thingValue, logBase));
				}else{
					double logBase = (double)Convert.ToDouble(Console.ReadLine());
					Console.WriteLine("What is the other thing: ");
					double thingValue = (double)Convert.ToDouble(Console.ReadLine());
					Console.WriteLine("The result is: " + Math.Log(thingValue, logBase));
				}
			}else if(pemdas=="7"){
				Random rnd = new Random();
				Console.WriteLine("WHOLE NUMBERS ONLY");
				Console.WriteLine("Lower bound: ");
				int lowerB = (int)Convert.ToInt32(Console.ReadLine());
				Console.WriteLine("Upper bound: ");
				int upperB = (int)Convert.ToInt32(Console.ReadLine());
				int compChoice = rnd.Next(lowerB, upperB);
			}
		}
	}
}
2 Likes

Kewl

1 Like

Why don’t you add parenthesis?

1 Like

Because

1 Like

Yeesh, that is going to be something difficult to do, as I’d have to do an open form calculator which are much harder to make.

2 Likes

I’m trying to make one in Scratch :]

Thats why I suggested it :slight_smile: \

You forgot base converting, such as decimal to binary :] you could look at my TypeScript code on Github for ideas of how to do it

Is your base converter fast? I didn’t read all the code (yet).

It’s pretty much instant

Since computers are really fast lol

1 Like

I also tried to make a calculator on C++ but not in C# using dotnetfiddle terminals.

I tried your code but every time the result of a function comes out it always appears above and not below in the foreground. When the result of a function comes out you could make it more visible.

Maybe when the result of a function comes out for example addition etc., you could put like “Press a key on the keyboard to go back”, and when you press it it brings you back to the available functions “here are the available functions”.

Here is:
Code

Thank you, I will put this into consideration

1 Like