C calculator program 🖩

Sdílet
Vložit
  • čas přidán 10. 09. 2024
  • C calculator program tutorial example explained
    #C #calculator #program
    int main(){
    char operator;
    double num1;
    double num2;
    double result;
    printf("
    Enter an operator (+ - * /): ");
    scanf("%c", &operator);
    printf("Enter number 1: ");
    scanf("%lf", &num1);
    printf("Enter number 2: ");
    scanf("%lf", &num2);
    switch(operator){
    case '+':
    result = num1 + num2;
    printf("
    result: %lf", result);
    break;
    case '-':
    result = num1 - num2;
    printf("
    result: %lf", result);
    break;
    case '*':
    result = num1 * num2;
    printf("
    result: %lf", result);
    break;
    case '/':
    result = num1 / num2;
    printf("
    result: %lf", result);
    break;
    default:
    printf("%c is not valid", operator);
    }
    return 0;
    }

Komentáře • 59

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

    #include
    int main(){
    char operator;
    double num1;
    double num2;
    double result;
    printf("
    Enter an operator (+ - * /): ");
    scanf("%c", &operator);
    printf("Enter number 1: ");
    scanf("%lf", &num1);
    printf("Enter number 2: ");
    scanf("%lf", &num2);
    switch(operator){
    case '+':
    result = num1 + num2;
    printf("
    result: %lf", result);
    break;
    case '-':
    result = num1 - num2;
    printf("
    result: %lf", result);
    break;
    case '*':
    result = num1 * num2;
    printf("
    result: %lf", result);
    break;
    case '/':
    result = num1 / num2;
    printf("
    result: %lf", result);
    break;
    default:
    printf("%c is not valid", operator);
    }
    return 0;
    }

  • @somethingwine
    @somethingwine Před 3 měsíci +8

    I love how each video is so short so that I keep getting the satisfaction of completion. Great job Bro!

  • @mr.sharingan2945
    @mr.sharingan2945 Před 2 lety +9

    Hello bro
    first I want to thank you for making such a quality tutorial videos these are easy to understand , quick , informative
    i am learning c easily thanks to you
    please make a video about what is difference between if statement and switch statement i am confused when to use if and when to use switch
    in which situation which one is better
    Again thank you , for this amazing playlist

    • @Noobatsecs69
      @Noobatsecs69 Před rokem

      I dont think theres any major difference between "if" and "switch" statements but as he said, use switch statements if you are gonna use a lot of "if" statements because its considered good practice and also in my opinion it looks nice and easy to understand.

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

      use switch statements whenever you can, use if statements when you cannot use a switch statement.

  • @SelloTape1651
    @SelloTape1651 Před rokem +7

    To anyone having this error error:
    expected type-specifier before ')' token
    printf("%c is not valid", operator);
    You just have to change the name of the char. I changed "operator" to "op" and it works fine

  • @jadeharvyabapo922
    @jadeharvyabapo922 Před rokem +8

    what if the printf("Enter your operator"); comes after the printf("Enter your num1"); .... why it won't work? why do i have to input the operator instead of the num1 first?

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

      Taking the operator as the first input allows the program to determine the course of action to perform and read the subsequent values accordingly, facilitating the execution of the desired mathematical operation in a structured and organized manner.

  • @Nuno97
    @Nuno97 Před 7 měsíci +3

    Why is it that if i want to put:
    printf("Enter an operator(+ - * /): ");
    scanf("%c", &operator);
    after:
    printf("
    Enter number 1: ");
    scanf("%lf", &num1);
    making the terminal ask me first the number and then the operator.
    i have to put scanf(" %c", &operator);
    A space before the %c? if not the code doesn't work
    Thank you!

    • @crystalfruit1193
      @crystalfruit1193 Před 6 měsíci +1

      I had the same aproach and it lead to me having the answer, when you press enter you basically give " " before your actual answer thats why

  • @kashhnotfound
    @kashhnotfound Před 8 měsíci +2

    Danke, bruder

  • @Amy-mo9ki
    @Amy-mo9ki Před rokem +2

    Very helpful! Thank you!

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

    4.20. Nice double, yup bro code indeed.

  • @tanjakilibarda9802
    @tanjakilibarda9802 Před 2 lety +12

    Why do u sometimes use main() and sometimes int main(). wat is the difference?

    • @jurescuiacob2237
      @jurescuiacob2237 Před 7 měsíci +5

      There is a big difference cuz in C u have to specify the return type of the function, so int main() would return 0. If u dont want to return anything u can say void as the return type

  • @jtjc7705
    @jtjc7705 Před rokem +1

    I asked myself "there has to be a better way than a bunch of switches... right?"
    short answer, nope.
    I went down a rabbit hole of "but wait, why can't I just recall the operator variable into an equation between num1 and num2, why all this extra stuff".
    It is so bizarre to me that you can't just inject an arithmetic operator into a function. Crazy right?
    I guess I get how the char is no longer an actual operator but a correlation "image" to something else. Still mind blowing to me that it isn't a matter of simple recall of math function. Good to know early on, I guess!

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

      Exactly, the character is just used to trigger a "true or false statement" and represents the input selection of the user. The actual equation takes place if the statement is "true". If the operation results in "false" it proceeds to the next switch case function.

  • @katnax3059
    @katnax3059 Před 10 měsíci +2

    When i tried to do it "chronologicaly" first number, then operator, then second number, i get wierd errors, Is it why you did this program with operator first?

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

    Hi , i would ask you what extention of visual studio code you have to make C run and compile. Because i've an issue with code, it simply dont allow me to do anything in c. Thanks for all your videos , they are helping me a lot, keep goin :D !!!

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

      extention or plugins

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

      Firstly a C/C++ extension and after that a extension named CODE RUNNER to run the code

  • @Partybauneparty
    @Partybauneparty Před 2 lety +4

    When I place scanf("%c", &operator) in between "Enter number 1: " and "Enter number 2: " the program skip scanf("%c", &operator) and goes straight to "Enter number 2: "
    Why is that?

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

      Hello Martin! Try using space before %c like this:
      scanf(" %c", &operator);

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

      @@slavapavlov9473 It works! Thanks :) but why does it work with the space before %c?

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

      It should work fine without the space, but many times the issue is with the compiler which creates the problem. From my personal experience, compilers can be somewhat glitchy. In that case entering a space can be an option to make the program compile and run. But yes the correct syntax should be ("%c", &operator); without the extra space, but unfortunately it sometimes glitches and the only way around it is to insert the space. Sometimes closing the code editor and restarting the computer might fix the issue.

  • @joshuaramos6866
    @joshuaramos6866 Před rokem

    thankyou beh you help so much po

  • @fernandoalencar3767
    @fernandoalencar3767 Před rokem

    Amazing man!

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

    I can't believe that all of this is free

  • @aryan_baka
    @aryan_baka Před rokem +1

    can i ask something? is this really basic or should i fear if i found this difficult? if i learned this calculator program, how much of my journey to learn c language is completed? can anyone please tell?

    • @vaniatheewlas9617
      @vaniatheewlas9617 Před rokem +1

      You're on 30% percent or so

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

      Just follow the lessons and take it step by step. By doing these little projects you can get a grasp of how the program functions and behaves. And dont worry about understanding everything, just practice and slowly things will become clearer. Every program has its peculiarities. Also dont forget to use google as a tool. Fear can only produce negativity which can sabotage our goals. Have fun and just code.

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

    can you make the same calculator program but with if statements please? thank you!

    • @yncor9756
      @yncor9756 Před 2 lety +3

      With the switch statment, the calculator is more efficient

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

      Yes, you can use "If /else" statements to perform the same operation. The advantage of using the switch statement is that the code looks more organized, but both methods work the same. If you have many "if/else" statements it can get a little difficult to read, in that case it would be better to use "switch", which is the recommended way. I use both methods depending on the length of the comparisons being made.

  • @naveenkumar1853
    @naveenkumar1853 Před 9 měsíci

    Good

  • @multicitygirl5527
    @multicitygirl5527 Před 2 lety

    thank uuuuuu

  • @Ben-uh5zc
    @Ben-uh5zc Před rokem +2

    I am getting this error with my code, I have tried to even just copy and paste your code but it is has not worked and still gives me this error message:
    Helloworld.C: In function 'int main()':
    Helloworld.C:6:17: error: expected type-specifier before ';' token
    char operator;
    ^
    Helloworld.C:12:26: error: expected type-specifier before ')' token
    scanf(" %c", &operator);
    ^
    Helloworld.C:20:19: error: expected type-specifier before ')' token
    switch(operator){
    ^
    Helloworld.C:38:44: error: expected type-specifier before ')' token
    printf("%c is not valid", operator);

    • @gab1xfn552
      @gab1xfn552 Před rokem +2

      I get that error aswell, did u fix it,if so please tell me how!

  • @pentasquare
    @pentasquare Před 6 měsíci +1

    What if I want to do more than two numbers

    • @futuredoctor7148
      @futuredoctor7148 Před 6 měsíci

      then add more variables and do something like this
      char operator;
      double num1;
      double num2;
      double num3;
      double num4;
      double result;
      result = num1+num2+num3+num4;
      Other code as it is and you are done

    • @pentasquare
      @pentasquare Před 6 měsíci

      @@futuredoctor7148 I wanted infinite amount of number. Not any selected amount. I tried it using while loop but I'm stuck on subtraction.
      Like I tried making so that you can only add subtract multiply or divide however many numbers you like. I made addition but subtraction is not working

    • @LAAL0_O
      @LAAL0_O Před 4 měsíci

      @@futuredoctor7148 but what if I don't know how many values the user is going to enter?
      wouldn't like this the user will HAVE to put 4 values?

  • @Buzzy_44
    @Buzzy_44 Před 6 měsíci

    The easiest way to make a calculator 😮

  • @abusufiansarker9572
    @abusufiansarker9572 Před rokem

    Hey Mr. Bro. I need your help in this
    If I to input wrong operator at the very start, how am I to end the code right there so that i dont have to input those number?

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

      use break statement

    • @luminouscat2117
      @luminouscat2117 Před 3 měsíci

      @@erenuzumaki2311 how?

    • @luminouscat2117
      @luminouscat2117 Před 3 měsíci

      @@erenuzumaki2311 Ok. i got that
      const char *valid_chars = "/*+-";
      printf("
      Enter an operator: ");
      scanf(" %c", &operator);
      if (!strchr(valid_chars, operator)) {
      printf("Wrong operator
      ");
      return 1; // Exit the program if the operator is invalid
      }

  • @Mad-Ramx_Dev
    @Mad-Ramx_Dev Před 5 měsíci

    hi

  • @AbuBakkarRaihan
    @AbuBakkarRaihan Před 6 měsíci +4

    My Calculator Code Is
    #include int main(){ printf("Welcome To Calculator"); float n1,n2; char s; char Sum = '+'; char Subtraction = '-'; char Product = '*'; char Quotient = '/'; int i; while (i=5) { printf("

    Enter 1st Number : "); scanf("%f",&n1); printf("
    Enter Operator (+,-,*,/) : "); scanf("%s",&s); printf("
    Enter 2nd Number : "); scanf("%f",&n2); if (s==Sum) { float Sum1 = n1 + n2; printf("
    Your Sum Is : "); printf("%f",Sum1); } else if (s==Subtraction) { float Subtraction1=n1 - n2; printf("
    Your Subtraction Is : "); printf("%f",Subtraction1); } else if (s==Product) { float Product1= n1 * n2; printf("
    Your Product Is : "); printf("%f",Product1); } else if (s==Quotient) { float Quotient1= n1 / n2; printf("
    Your Quotient Is : "); printf("%f",Quotient1); } else{ printf("

    Syntax Error!

    Try Again"); } } return 0; }

  • @r7h4d
    @r7h4d Před 9 měsíci

    #include
    int main()
    {
    float a;
    float b;
    printf("Enter first number:");
    scanf("%f", &a);
    printf("Enter second number:");
    scanf("%f", &b);
    printf("Sum is:%f \t
    ", a+b);
    printf("Product is:%f \t
    ", a*b);
    printf("Division is:%f \t
    ",a/b);
    printf("Subtraction:%f\t
    ",a-b);
    }

  • @felipemrj.
    @felipemrj. Před 4 měsíci

    Made mine like this so the user doesnt get prompted for numbers if he inputs invalid operator:
    #include
    void getnumbers(double *num1, double *num2);
    int main()
    {
    char operator;
    double num1, num2, res;
    printf("Enter an operator (+ - * /): ");
    scanf("%c", &operator);
    switch(operator)
    {
    case '+':
    getnumbers(&num1, &num2);
    res = num1 + num2;
    break;
    case '-':
    getnumbers(&num1, &num2);
    res = num1 - num2;
    break;
    case '*':
    getnumbers(&num1, &num2);
    res = num1 * num2;
    break;
    case '/':
    getnumbers(&num1, &num2);
    res = num1 / num2;
    break;
    default:
    printf("%c is not a valid operator.
    ", operator);
    return 1;
    }
    printf("Result: %lf
    ", res);
    return 0;
    }
    void getnumbers(double *num1, double *num2)
    {
    printf("Enter number 1: ");
    scanf("%lf", num1);
    printf("Enter number 2: ");
    scanf("%lf", num2);
    }