C# calculator program 🖩

  Рет қаралды 65,958

Bro Code

Bro Code

Күн бұрын

Пікірлер
@BroCodez
@BroCodez 3 жыл бұрын
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(); } } }
@dreamboy1659
@dreamboy1659 9 ай бұрын
//Mine - which allow user to do operation with previous result //Thank you for the lecture^^! internal class Program { private static void Main(string[] args) { double a = 0, b = 0, result = 0; string operato; string cont1, cont2; bool true1 = true, true2 = true; while(true2==true) { Console.WriteLine("Enter a: "); a = double.Parse(Console.ReadLine()); true1 = true; while (true1 == true) { Console.WriteLine("Enter b: "); b = double.Parse(Console.ReadLine()); operato = ""; while(operato != "+" && operato != "-" && operato != "*" && operato != "/") { Console.WriteLine("What do you want to do now? (+; -; *; /)"); operato = Console.ReadLine(); } switch (operato) { case ("+"): result = a + b; break; case ("-"): result = a - b; break; case ("*"): result = a * b; break; case ("/"): result = a / b; break; } Console.WriteLine("Result is: " + result); cont1 = ""; while (cont1 != "Y" && cont1 != "N") { Console.WriteLine("Do you want to continue do calculating with the result? (Y/N)"); cont1 = Console.ReadLine(); } switch (cont1) { case ("Y"): a = result; true1 = true; break; case ("N"): cont2 = ""; while (cont2 != "Y" && cont2 != "N") { Console.WriteLine("Then do you want to do another operation or not? (Y/N)"); cont2 = Console.ReadLine(); } switch (cont2) { case ("Y"): true1 = false; true2 = true; break; case ("N"): true1 = false; true2 = false; break; } break; } } } Console.WriteLine("Ok, bye then!"); Console.ReadKey(); } }
@turkyt
@turkyt 7 ай бұрын
Give as ne video on this topic please with new method 😔
@engirckt5410
@engirckt5410 3 жыл бұрын
your lessons are better than many c# lessons out there! Thank you for all of the quality lessons you made!
@hamzasadouk4741
@hamzasadouk4741 8 ай бұрын
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 = Convert.ToDouble(Console.ReadLine()); }
@leg1187
@leg1187 2 жыл бұрын
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!
@Ken2K5
@Ken2K5 3 ай бұрын
This is better than my instructor's lesson, now he is wondering why I already tackled his lessons even we never met yet
@spartanuk1977
@spartanuk1977 3 ай бұрын
I am 14 and cannot believe I understand all of this code, thank you so much
@ivanpedro9151
@ivanpedro9151 2 жыл бұрын
currently learning how to code, thanks for the help
@alexmacmillan2690
@alexmacmillan2690 Жыл бұрын
Program is simple, but I’m add check on integer value, because enter params can be string
@shiroy2
@shiroy2 2 жыл бұрын
4:57 nice
@simik4830
@simik4830 3 ай бұрын
for the algorithm!
@MarbinCastro502
@MarbinCastro502 Жыл бұрын
NIce
@spartanranger
@spartanranger 3 жыл бұрын
Thanks for the video Bro.
@AzianPrincess
@AzianPrincess Жыл бұрын
Thank you so much, Sirr!❤️‍🔥
@AdrAtom_1
@AdrAtom_1 5 ай бұрын
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!
@user-gk9fn8pu1f
@user-gk9fn8pu1f Жыл бұрын
top
@Iamaqwerte
@Iamaqwerte Жыл бұрын
Thanks man! It's a very useful tutorial for beginners like me!
@whitedinamo
@whitedinamo 2 жыл бұрын
lesson check😇
@michalski9141
@michalski9141 3 жыл бұрын
what if I wanted to make sure the user typed in y or n when asked if they want to continue?
3 жыл бұрын
Thanks Bro!
@rivazmardani
@rivazmardani 2 жыл бұрын
makasih gan
@monn1815
@monn1815 3 жыл бұрын
Why don't you use github for uploading the code?
@BroCodez
@BroCodez 3 жыл бұрын
I'm lazy
@paavobjorkbacka7254
@paavobjorkbacka7254 2 жыл бұрын
Doesnt work, it les me put numbers like 2 but no 2.2
@jawadkassomeh3207
@jawadkassomeh3207 2 жыл бұрын
try putting in an "," instead of an "."
@Taril69
@Taril69 Жыл бұрын
Hey bro, How can i lock the "num1" and "num2" to only number/integers. because when add a letter it crashes
@Nikola95inYT
@Nikola95inYT Жыл бұрын
You basically you need to separate taking input and assigning variable. After taking input, assign it to a temporary variable and check if it contains only digits, if it is, convert variable to double in order to make operations. You can put it inside loop, so in case of invalid input user will be told to enter again.
@Taril69
@Taril69 Жыл бұрын
Alright! thanks for the reply :) @@Nikola95inYT
@Sasuke46525
@Sasuke46525 Ай бұрын
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(" Choose 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 = Console.ReadLine().Trim().ToLower(); 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 InvalidOperationException("Invalid operation."); } } } }
@iliyan1014
@iliyan1014 2 жыл бұрын
Work for fractions too?
@karolissad.4270
@karolissad.4270 Жыл бұрын
if you mean decimal fractions yes, but it won't understand if you enter something like 1/3
@giacomorinaldi
@giacomorinaldi 2 жыл бұрын
hello, why you use this code ? $"Your result: {num1} + {num2} = " + result ? it's not creare the $ and the num in the parenthesis
@AdityaSharma-qt1ig
@AdityaSharma-qt1ig Жыл бұрын
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.
@jorgematos863
@jorgematos863 3 жыл бұрын
bro why double numb = 0 and not only double numb;
@crazytheorist118
@crazytheorist118 Жыл бұрын
Chad coding
@denisdospel2975
@denisdospel2975 5 ай бұрын
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 System.Runtime.InteropServices; 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("------------------"); 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("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!!
@haroonrasheed1117
@haroonrasheed1117 Жыл бұрын
thanks
@RighteousBruce
@RighteousBruce 2 жыл бұрын
Mine wont even launch
@jawadkassomeh3207
@jawadkassomeh3207 2 жыл бұрын
i think you like 3,14
@definitelynotchris4776
@definitelynotchris4776 3 ай бұрын
thanks daddy
@memy4460
@memy4460 2 жыл бұрын
comment
@harisshafi01
@harisshafi01 Жыл бұрын
using System.Runtime.CompilerServices; using System.Threading.Channels; double firstNum = 0; double secondNum = 0; double result = 0; do { Console.WriteLine("----------------"); Console.WriteLine("-- Calculator --"); Console.WriteLine("----------------"); Console.Write("Enter the first Num : "); firstNum = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter the second Num : "); secondNum = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Choose Options"); Console.WriteLine("\t+ : ADD "); Console.WriteLine("\t- : SUBSTRACT "); Console.WriteLine("\t* : MULTIPLY"); Console.WriteLine("\t/ : DIVIDE"); Console.Write("Enter the Options : "); switch(Console.ReadLine()) { case "+": result = firstNum + secondNum; Console.WriteLine($"Result : {firstNum} + {secondNum} = "+result); break; case "-": result = firstNum - secondNum; Console.WriteLine($"Result : {firstNum} - {secondNum} = " + result); break; case "*": result = firstNum * secondNum; Console.WriteLine($"Result : {firstNum} * {secondNum} = " + result); break; case "/": result = firstNum / secondNum; Console.WriteLine($"Result : {firstNum} / {secondNum} = " + result); break; default: Console.WriteLine("That was not a valid option"); break; } Console.WriteLine("Would you like to continue (Y/N) :"); }while (Console.ReadLine().ToUpper() =="Y"); Console.WriteLine("Thanks"); Console.ReadKey();
@goroland6904
@goroland6904 Жыл бұрын
The Code is good: Console.Writeline(Source + "Trust me bro");
@Go_view
@Go_view 2 жыл бұрын
Console.WritLine("greate");
C# arrays 🚗
5:57
Bro Code
Рет қаралды 68 М.
C# rock-paper-scissors game 🗿
11:44
Bro Code
Рет қаралды 37 М.
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 45 МЛН
BAYGUYSTAN | 1 СЕРИЯ | bayGUYS
36:55
bayGUYS
Рет қаралды 1,9 МЛН
how Google writes gorgeous C++
7:40
Low Level
Рет қаралды 980 М.
How to create a calculator in C# windows forms application
19:22
Tutus Funny
Рет қаралды 131 М.
Learning C# In A Week... Otherwise I Fail University
9:04
Simple GUI Calculator in Python
22:51
NeuralNine
Рет қаралды 292 М.
Can You Beat Minecraft if the Whole World is Water?
16:07
EnderSkull
Рет қаралды 293 М.
How to Program in C# - BASICS (E01)
11:48
Brackeys
Рет қаралды 1,5 МЛН
How to Create a C# Calculator in 25 minutes
25:22
DJ Oamen
Рет қаралды 18 М.
31 nooby C++ habits you need to ditch
16:18
mCoding
Рет қаралды 846 М.
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН