C# calculator program 🖩

Sdílet
Vložit
  • čas přidán 2. 07. 2021
  • 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();
    }
    }
    }
  • Věda a technologie

Komentáře • 47

  • @BroCodez
    @BroCodez  Před 3 lety +48

    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 Před 3 měsíci

      //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 Před měsícem

      Give as ne video on this topic please with new method 😔

  • @engirckt5410
    @engirckt5410 Před 3 lety +24

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

  • @leg1187
    @leg1187 Před 2 lety +1

    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!

  • @hamzasadouk4741
    @hamzasadouk4741 Před 2 měsíci +1

    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());
    }

  • @ivanpedro9151
    @ivanpedro9151 Před rokem +1

    currently learning how to code, thanks for the help

  • @AzianPrincess
    @AzianPrincess Před rokem

    Thank you so much, Sirr!❤️‍🔥

  • @spartanranger
    @spartanranger Před 2 lety +1

    Thanks for the video Bro.

  • @alexmacmillan2690
    @alexmacmillan2690 Před rokem +1

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

  •  Před 3 lety

    Thanks Bro!

  • @Iamaqwerte
    @Iamaqwerte Před rokem +1

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

  • @michalski9141
    @michalski9141 Před 2 lety

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

  • @rivazmardani
    @rivazmardani Před 2 lety

    makasih gan

  • @haroonrasheed1117
    @haroonrasheed1117 Před rokem

    thanks

  • @user-hm4xp6iv9u
    @user-hm4xp6iv9u Před 2 lety +1

    4:57 nice

  • @MarbinCastro502
    @MarbinCastro502 Před 8 měsíci

    NIce

  • @user-gk9fn8pu1f
    @user-gk9fn8pu1f Před 10 měsíci

    top

  • @acidpants97
    @acidpants97 Před 10 měsíci

    I encapsulated the code in a method instead and had it call the method instead of using the do while loop I didn't know anything about that loop or how it worked

  • @AdityaSharma-qt1ig
    @AdityaSharma-qt1ig Před rokem

    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.

  • @whitedinamo
    @whitedinamo Před 2 lety +1

    lesson check😇

  • @giacomorinaldi
    @giacomorinaldi Před rokem

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

  • @jorgematos863
    @jorgematos863 Před 2 lety

    bro why double numb = 0 and not only double numb;

  • @Taril69
    @Taril69 Před 10 měsíci +1

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

    • @Nikola95inYT
      @Nikola95inYT Před 8 měsíci +1

      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 Před 8 měsíci +1

      Alright! thanks for the reply :) @@Nikola95inYT

  • @crazytheorist118
    @crazytheorist118 Před 7 měsíci

    Chad coding

  • @user-bk1dy5wr1d
    @user-bk1dy5wr1d Před 16 dny

    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!

  • @monn1815
    @monn1815 Před 3 lety +1

    Why don't you use github for uploading the code?

  • @iliyan1014
    @iliyan1014 Před rokem

    Work for fractions too?

    • @karolissad.4270
      @karolissad.4270 Před rokem

      if you mean decimal fractions yes, but it won't understand if you enter something like 1/3

  • @zhinojabbar847
    @zhinojabbar847 Před 2 lety +1

    if number/0 say invaild in swich case ? help

    • @cyrusslobig518
      @cyrusslobig518 Před 2 lety

      case '/':
      if (num2 == 0.0)
      Console.WriteLine("Please select an integer other than 0");

    • @zhinojabbar847
      @zhinojabbar847 Před 2 lety

      @@cyrusslobig518 thanks

  • @RighteousBruce
    @RighteousBruce Před rokem

    Mine wont even launch

  • @paavobjorkbacka7254
    @paavobjorkbacka7254 Před 2 lety +2

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

  • @jawadkassomeh3207
    @jawadkassomeh3207 Před 2 lety

    i think you like 3,14

  • @memy4460
    @memy4460 Před 2 lety

    comment

  • @harisshafi01
    @harisshafi01 Před 8 měsíci

    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 Před rokem

    The Code is good:
    Console.Writeline(Source + "Trust me bro");

  • @Go_view
    @Go_view Před rokem

    Console.WritLine("greate");