C calculator program 🖩
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;
}
#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;
}
H
What is the meaning of break in c programming.
erm actually using scanf will cause buffer overflow 🤓🤓☝️☝️
@@Buzzy_44 that is used to stop a loop
@@danielduckq good
I love how each video is so short so that I keep getting the satisfaction of completion. Great job Bro!
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
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.
use switch statements whenever you can, use if statements when you cannot use a switch statement.
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
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?
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.
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!
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
Danke, bruder
Very helpful! Thank you!
4.20. Nice double, yup bro code indeed.
Why do u sometimes use main() and sometimes int main(). wat is the difference?
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
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!
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.
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?
ofcourse
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 !!!
extention or plugins
Firstly a C/C++ extension and after that a extension named CODE RUNNER to run the code
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?
Hello Martin! Try using space before %c like this:
scanf(" %c", &operator);
@@slavapavlov9473 It works! Thanks :) but why does it work with the space before %c?
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.
thankyou beh you help so much po
Amazing man!
I can't believe that all of this is free
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?
You're on 30% percent or so
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.
can you make the same calculator program but with if statements please? thank you!
With the switch statment, the calculator is more efficient
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.
Good
thank uuuuuu
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);
I get that error aswell, did u fix it,if so please tell me how!
What if I want to do more than two numbers
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
@@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
@@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?
The easiest way to make a calculator 😮
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?
use break statement
@@erenuzumaki2311 how?
@@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
}
hi
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; }
#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);
}
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);
}