Skip to content

PRO TIP Block youtube.com at the DNS level โ€” Pi-hole, NextDNS or your hosts file โ€” but allow youtube-nocookie.com and i.ytimg.com. These tutorials keep playing; the rabbit hole does not.

Learning without distractions

Let’s code a calculator with C programming! ๐Ÿ–ฉ

14K views on YouTube

#coding #programming #cprogramming

// CALCULATOR PROGRAM

char operator = ‘\0’;
double num1 = 0.0;
double num2 = 0.0;
double result = 0.0;

printf(“Enter the first number: “);
scanf(“%lf”, &num1);

printf(“Enter the operator (+ – * /): “);
scanf(” %c”, &operator); // clear \n from input buffer

printf(“Enter the second number: “);
scanf(“%lf”, &num2);

switch(operator){
case ‘+’:
result = num1 + num2;
break;
case ‘-‘:
result = num1 – num2;
break;
case ‘*’:
result = num1 * num2;
break;
case ‘/’:
if(num2 == 0){
printf(“You can’t divide by zero!\n”);
}
else{
result = num1 / num2;
}
break;
default:
printf(“Invalid operator\n”);
}

printf(“result: %.4lf”, result);

return 0;