C calculator program 🖩

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ก.ย. 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;
    }

ความคิดเห็น • 59

  • @BroCodez
    @BroCodez  3 ปีที่แล้ว +46

    #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;
    }

    • @michelleannartiaga8164
      @michelleannartiaga8164 ปีที่แล้ว

      H

    • @Buzzy_44
      @Buzzy_44 6 หลายเดือนก่อน

      What is the meaning of break in c programming.

    • @danielduckq
      @danielduckq 5 หลายเดือนก่อน

      erm actually using scanf will cause buffer overflow 🤓🤓☝️☝️

    • @danielduckq
      @danielduckq 5 หลายเดือนก่อน +1

      @@Buzzy_44 that is used to stop a loop

    • @li-lunarink
      @li-lunarink 4 หลายเดือนก่อน

      @@danielduckq good

  • @somethingwine
    @somethingwine 4 หลายเดือนก่อน +8

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

  • @SelloTape1651
    @SelloTape1651 ปีที่แล้ว +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

  • @mr.sharingan2945
    @mr.sharingan2945 2 ปีที่แล้ว +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 ปีที่แล้ว

      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 10 หลายเดือนก่อน

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

  • @jadeharvyabapo922
    @jadeharvyabapo922 2 ปีที่แล้ว +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 11 หลายเดือนก่อน +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.

  • @Amy-mo9ki
    @Amy-mo9ki ปีที่แล้ว +2

    Very helpful! Thank you!

  • @tanjakilibarda9802
    @tanjakilibarda9802 2 ปีที่แล้ว +13

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

    • @jurescuiacob2237
      @jurescuiacob2237 8 หลายเดือนก่อน +6

      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

  • @Nuno97
    @Nuno97 8 หลายเดือนก่อน +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 6 หลายเดือนก่อน +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 9 หลายเดือนก่อน +2

    Danke, bruder

  • @jtjc7705
    @jtjc7705 ปีที่แล้ว +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 11 หลายเดือนก่อน

      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.

  • @r0wdybandit015
    @r0wdybandit015 3 หลายเดือนก่อน +1

    4.20. Nice double, yup bro code indeed.

  • @katnax3059
    @katnax3059 10 หลายเดือนก่อน +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 2 ปีที่แล้ว +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 2 ปีที่แล้ว +1

      extention or plugins

    • @mohdsajid9465
      @mohdsajid9465 2 ปีที่แล้ว +1

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

  • @Partybauneparty
    @Partybauneparty 2 ปีที่แล้ว +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 2 ปีที่แล้ว +1

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

    • @Partybauneparty
      @Partybauneparty 2 ปีที่แล้ว +1

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

    • @michaelucho
      @michaelucho 11 หลายเดือนก่อน

      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.

  • @naveenkumar1853
    @naveenkumar1853 9 หลายเดือนก่อน

    Good

  • @joshuaramos6866
    @joshuaramos6866 ปีที่แล้ว

    thankyou beh you help so much po

  • @fernandoalencar3767
    @fernandoalencar3767 ปีที่แล้ว

    Amazing man!

  • @engirckt5410
    @engirckt5410 2 ปีที่แล้ว +1

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

    • @yncor9756
      @yncor9756 2 ปีที่แล้ว +3

      With the switch statment, the calculator is more efficient

    • @michaelucho
      @michaelucho 11 หลายเดือนก่อน +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.

  • @aryan_baka
    @aryan_baka ปีที่แล้ว +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 ปีที่แล้ว +1

      You're on 30% percent or so

    • @michaelucho
      @michaelucho 11 หลายเดือนก่อน +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.

  • @Ben-uh5zc
    @Ben-uh5zc ปีที่แล้ว +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 ปีที่แล้ว +2

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

  • @prumchhangsreng979
    @prumchhangsreng979 2 ปีที่แล้ว +1

    I can't believe that all of this is free

  • @Buzzy_44
    @Buzzy_44 6 หลายเดือนก่อน

    The easiest way to make a calculator 😮

  • @multicitygirl5527
    @multicitygirl5527 2 ปีที่แล้ว

    thank uuuuuu

  • @pentasquare
    @pentasquare 7 หลายเดือนก่อน +1

    What if I want to do more than two numbers

    • @futuredoctor7148
      @futuredoctor7148 7 หลายเดือนก่อน

      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 7 หลายเดือนก่อน

      @@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 5 หลายเดือนก่อน

      @@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?

  • @abusufiansarker9572
    @abusufiansarker9572 ปีที่แล้ว

    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 10 หลายเดือนก่อน

      use break statement

    • @luminouscat2117
      @luminouscat2117 3 หลายเดือนก่อน

      @@erenuzumaki2311 how?

    • @luminouscat2117
      @luminouscat2117 3 หลายเดือนก่อน

      @@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 5 หลายเดือนก่อน

    hi

  • @AbuBakkarRaihan
    @AbuBakkarRaihan 6 หลายเดือนก่อน +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 9 หลายเดือนก่อน

    #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. 5 หลายเดือนก่อน

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