C calculator program 🖩

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

#C #calculator #program

int main(){

char operator;
double num1;
double num2;
double result;

printf("\nEnter 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("\nresult: %lf", result);
break;
case '-':
result = num1 - num2;
printf("\nresult: %lf", result);
break;
case '*':
result = num1 * num2;
printf("\nresult: %lf", result);
break;
case '/':
result = num1 / num2;
printf("\nresult: %lf", result);
break;
default:
printf("%c is not valid", operator);
}

return 0;
}
Рекомендации по теме
Комментарии
Автор

#include <stdio.h>

int main(){

char operator;
double num1;
double num2;
double result;

printf("\nEnter 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("\nresult: %lf", result);
break;
case '-':
result = num1 - num2;
printf("\nresult: %lf", result);
break;
case '*':
result = num1 * num2;
printf("\nresult: %lf", result);
break;
case '/':
result = num1 / num2;
printf("\nresult: %lf", result);
break;
default:
printf("%c is not valid", operator);
}

return 0;
}

BroCodez
Автор

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

somethingwine
Автор

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

mr.sharingan
Автор

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?

jadeharvyabapo
Автор

Why is it that if i want to put:
printf("Enter an operator(+ - * /): ");
scanf("%c", &operator);

after:
printf("\nEnter 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!

Nuno
Автор

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

SelloTape
Автор

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!

jtjc
Автор

4.20. Nice double, yup bro code indeed.

rwdybandit
Автор

I can't believe that all of this is free

prumchhangsreng
Автор

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

tanjakilibarda
Автор

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?

katnax
Автор

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_
Автор

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

engirckt
Автор

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?

abusufiansarker
Автор

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?

Partybauneparty
Автор

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

Ben-uhzc
Автор

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?

aryan_baka
Автор

What if I want to do more than two numbers

pentasquare
Автор

The easiest way to make a calculator 😮

Buzzy_
Автор

Made mine like this so the user doesnt get prompted for numbers if he inputs invalid operator:

#include <stdio.h>

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.\n", operator);
return 1;
}

printf("Result: %lf\n", res);
return 0;

}

void getnumbers(double *num1, double *num2)
{
printf("Enter number 1: ");
scanf("%lf", num1);
printf("Enter number 2: ");
scanf("%lf", num2);
}

felipemrj.