C# calculator program 🖩

preview_player
Показать описание
C# calculator program project tutorial example explained

#C# #calculator #program

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
do
{
double num1 = 0;
double num2 = 0;
double result = 0;

Console.WriteLine("------------------");
Console.WriteLine("Calculator Program");
Console.WriteLine("------------------");

Console.Write("Enter number 1: ");
num1 = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter number 2: ");
num2 = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter an option: ");
Console.WriteLine("\t+ : Add");
Console.WriteLine("\t- : Subtract");
Console.WriteLine("\t* : Multiply");
Console.WriteLine("\t/ : Divide");
Console.Write("Enter an option: ");

switch (Console.ReadLine())
{
case "+":
result = num1 + num2;
Console.WriteLine($"Your result: {num1} + {num2} = " + result);
break;
case "-":
result = num1 - num2;
Console.WriteLine($"Your result: {num1} - {num2} = " + result);
break;
case "*":
result = num1 * num2;
Console.WriteLine($"Your result: {num1} * {num2} = " + result);
break;
case "/":
result = num1 / num2;
Console.WriteLine($"Your result: {num1} / {num2} = " + result);
break;
default:
Console.WriteLine("That was not a valid option");
break;
}
Console.Write("Would you like to continue? (Y = yes, N = No): ");
} while (Console.ReadLine().ToUpper() == "Y");

Console.WriteLine("Bye!");
Console.ReadKey();
}
}
}
Рекомендации по теме
Комментарии
Автор

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
do
{
double num1 = 0;
double num2 = 0;
double result = 0;


Console.WriteLine("Calculator Program");


Console.Write("Enter number 1: ");
num1 =

Console.Write("Enter number 2: ");
num2 =

Console.WriteLine("Enter an option: ");
Console.WriteLine("\t+ : Add");
Console.WriteLine("\t- : Subtract");
Console.WriteLine("\t* : Multiply");
Console.WriteLine("\t/ : Divide");
Console.Write("Enter an option: ");


switch (Console.ReadLine())
{
case "+":
result = num1 + num2;
Console.WriteLine($"Your result: {num1} + {num2} = " + result);
break;
case "-":
result = num1 - num2;
Console.WriteLine($"Your result: {num1} - {num2} = " + result);
break;
case "*":
result = num1 * num2;
Console.WriteLine($"Your result: {num1} * {num2} = " + result);
break;
case "/":
result = num1 / num2;
Console.WriteLine($"Your result: {num1} / {num2} = " + result);
break;
default:
Console.WriteLine("That was not a valid option");
break;
}
Console.Write("Would you like to continue? (Y = yes, N = No): ");
} while (Console.ReadLine().ToUpper() == "Y");

Console.WriteLine("Bye!");
Console.ReadKey();
}
}
}

BroCodez
Автор

your lessons are better than many c# lessons out there! Thank you for all of the quality lessons you made!

engirckt
Автор

Fantastic video, well explained and easy to follow . the only thing i would like to add for people watching this later would be to make sure that num2 is not 0 to avoid division by 0

double num2=0;
.
.
.
. SAME CODE AS THE ONE IN THE VIDEO
.
.
while (num2 == 0)
{
Console.WriteLine(" Please enter number 2 diffrent from 0 :");
num2 =
}

hamzasadouk
Автор

Thank you so much for these videos. I'm telling multiple people in my class about you so we can try to pass our course. It's so ridiculous that with the outrageous tuition prices that schools charge that they cannot offer us the help you and others like you have. I wish they would smarten up and start working with YouTubers to improve their coursework.

maris
Автор

Thank you mate. Making a calculator In C# is easy but adding support for decimals and negative numbers is a bit tricky you helped me!

leg
Автор

This is better than my instructor's lesson, now he is wondering why I already tackled his lessons even we never met yet

KenK
Автор

I am 14 and cannot believe I understand all of this code, thank you so much

spartanuk
Автор

currently learning how to code, thanks for the help

ivanpedro
Автор

Program is simple, but I’m add check on integer value, because enter params can be string

alexmacmillan
Автор

Thanks man! It's a very useful tutorial for beginners like me!

Iamaqwerte
Автор

Not gonna lie Bro Code lessons are better than my proofs Bro Code explain it with example while our proof teaching us basics then leaving us with so many activities or problem that we dont know or he didn't even start teaching that lesson's yet

For me university is like a lead to me since before idk where would i start like how do i do this that not until i became a uni knowing the code from basics how it works what will you use for language what is ide and others data structure

Now i got the lead its all up to me if im gonna lit my spark or not

kyxzil
Автор

This is not really a calculator because you can just calculate 2 numbers . Making an array would help a lot but would need more cases. Anyways thanks!

AdrAtom_
Автор

doing this with no experience in IT before, when you typed switch i stopped the video and made the code myself and it ended up like this:

using System.Reflection;
using

namespace ConsoleApp3
{
internal class Program
{
static void Main(string[] args)
{
double num1 = 0;
double num2 = 0;
double result = 0;
bool again = true;

while (again)
{
String answer = "";

Console.WriteLine("Calculator Program");


Console.Write("Enter number 1: ");
num1 =

Console.Write("Enter number 2: ");
num2 =

Console.WriteLine("Select method");
Console.WriteLine("\t+ : Add");
Console.WriteLine("\t- : Substract");
Console.WriteLine("\t* : Multiply");
Console.WriteLine("\t/ : Divide");



switch (Console.ReadLine())
{
case "+":
result = num1 + num2;
Console.WriteLine("Your result: " + num1 + "+" + num2 + "=" + result);
break;
case "-":
result = num1 - num2;
Console.WriteLine("Your result: " + num1 + "-" + num2 + "=" + result);
break;
case "*":
result = num1 * num2;
Console.WriteLine("Your result: " + num1 + "*" + num2 + "=" + result);
break;
case "/":
result = num1 / num2;
Console.WriteLine("Your result: " + num1 + "/" + num2 + "=" + result);
break;



}
Console.WriteLine("Do you want to Start another calculation? Y/N: ");
answer = Console.ReadLine();
answer = answer.ToUpper();
if (answer == "Y")
{
again = true;
}
else
{
again = false;
}
}

Console.WriteLine("GG");

Console.ReadKey();
}
}
}


you are truly an amazing teacher. at the end is nice to see that do makes the whole while method way simpler. thank you for the videos!!

denisdospel
Автор

using System;

namespace EnumBasedCalculator
{
class Program
{
// Define operations as an enum
enum Operation
{
Addition = 1,
Subtraction = 2,
Multiplication = 3,
Division = 4
}

static void Main(string[] args)
{
Console.WriteLine("Welcome to the Enum-Based Calculator!");
Console.WriteLine("Supported operations:");
Console.WriteLine("1 - Addition (+)");
Console.WriteLine("2 - Subtraction (-)");
Console.WriteLine("3 - Multiplication (*)");
Console.WriteLine("4 - Division (/)");

while (true)
{
// Get user operation choice
Console.Write("\nChoose an operation (1-4): ");
if (!int.TryParse(Console.ReadLine(), out int operationInput) || !Enum.IsDefined(typeof(Operation), operationInput))
{
Console.WriteLine("Invalid choice. Please select a number between 1 and 4.");
continue;
}

Operation operation = (Operation)operationInput;

// Get the first number
Console.Write("Enter the first number: ");
if (!double.TryParse(Console.ReadLine(), out double firstNumber))
{
Console.WriteLine("Invalid input. Please enter a valid number.");
continue;
}

// Get the second number
Console.Write("Enter the second number: ");
if (!double.TryParse(Console.ReadLine(), out double secondNumber))
{
Console.WriteLine("Invalid input. Please enter a valid number.");
continue;
}

// Perform the calculation
double result = PerformOperation(operation, firstNumber, secondNumber);

// Check if the result is NaN (for division by zero)
if (double.IsNaN(result))
{
Console.WriteLine("Error: Division by zero is not allowed.");
}
else
{
Console.WriteLine($"The result is: {result}");
}

// Ask if the user wants to perform another calculation
Console.Write("Do you want to perform another calculation? (Y/N): ");
string response =
if (response != "y")
{
Console.WriteLine("Thank you for using the calculator. Goodbye!");
break;
}
}
}

static double PerformOperation(Operation operation, double num1, double num2)
{
switch (operation)
{
case Operation.Addition:
return num1 + num2;
case Operation.Subtraction:
return num1 - num2;
case Operation.Multiplication:
return num1 * num2;
case Operation.Division:
return num2 != 0 ? num1 / num2 : double.NaN;
default:
throw new operation.");
}
}
}
}

Sasuke
Автор

hey bro first of all thank you soo much for such a helpful content I live in Melbourne and regularly watch your videos
I need your help in a question I will be soo thank full to you if you can help me

Create a Visual C Sharp program that asks for the password three times. If the password is correct, it will welcome the user, otherwise, it will say Goodbye and END the program.
Run the above program and extend it to accept only alphanumeric input - for example, if a user enters digits only or characters only, it should not accept that as input, and ensures input is alphanumeric, and a mix of digits, numbers and one special character.

AdityaSharma-qtig
Автор

using System;
using System.Linq;
using System.Collections.Generic;

namespace HelloWold;

public static class Program
{
public static void Main()
{
int num = 23;

int num1 = 435;



Console.WriteLine(num);
Console.WriteLine(num1);
Console.WriteLine(num + num1);
Console.WriteLine(num * num1);
Console.WriteLine(num + num);
Console.WriteLine(num * num);
Console.WriteLine(num1 + num1);
Console.WriteLine(num1 * num1);
}
}

Пристаньрыбака-жш
Автор

what if I wanted to make sure the user typed in y or n when asked if they want to continue?

michalski
Автор

Hey bro,
How can i lock the "num1" and "num2" to only number/integers. because when add a letter it crashes

Taril
Автор

hello, why you use this code ? $"Your result: {num1} + {num2} = " + result ? it's not creare the $ and the num in the parenthesis

giacomorinaldi
Автор

Doesnt work, it les me put numbers like 2 but no 2.2

paavobjorkbacka