Amazon q is so good for learning new things!
I was trying to learn switch case conditional statements and wanted to make something cool using it... preferably something math related... I made an easy basic calculator and a simple temperature converter for 2 units.... it was so fun and easy to learn, and I typed like 2 lines, and then it was tab tab tab tab ^_^ ..... Not only that, thought this would make me rely more on it and I wouldn't be able to write without it later on, but it really is a great tool for learning, and I am loving it so far!!!
Published Sep 15, 2024
#include<stdio.h>#include<ctype.h>#include<math.h>
void main(){
char operator; double num1, num2, result;
printf("Enter an operator (+, -, *, /): "); scanf("%c", &operator); printf("Enter two numbers: "); scanf("%lf %lf", &num1, &num2);
switch (operator) { case '+': printf("Result: %.2lf\n", num1 + num2); break; case '-': printf("Result: %.2lf\n", num1 - num2); break; case '*': printf("Result: %.2lf\n", num1 * num2); break; case '/': printf("Result: %.2lf\n", num1 / num2); break; default: printf("Invalid operator"); }}
void main(){
char operator; double num1, num2, result;
printf("Enter an operator (+, -, *, /): "); scanf("%c", &operator); printf("Enter two numbers: "); scanf("%lf %lf", &num1, &num2);
switch (operator) { case '+': printf("Result: %.2lf\n", num1 + num2); break; case '-': printf("Result: %.2lf\n", num1 - num2); break; case '*': printf("Result: %.2lf\n", num1 * num2); break; case '/': printf("Result: %.2lf\n", num1 / num2); break; default: printf("Invalid operator"); }}
#include<stdio.h>#include<ctype.h>
int main(){ char unit; float temp;
printf("Is the temperature in (F) or (C)?: "); scanf("%c", &unit); unit = toupper(unit);
if (unit == 'C') { printf("Enter the temp in Celsius: "); scanf("%f", &temp); temp = (temp * 9/5) + 32; printf("The temp in Fahrenheit is: %.1f\n", temp); } else if(unit == 'F') { printf("Enter the temp in Fahrenheit: "); scanf("%f", &temp); temp = ((temp - 32) * 5) / 9; printf("The temp in Celsius is: %.1f\n", temp); } else { printf("%c is not a valid unit of measurement", unit); }
}
int main(){ char unit; float temp;
printf("Is the temperature in (F) or (C)?: "); scanf("%c", &unit); unit = toupper(unit);
if (unit == 'C') { printf("Enter the temp in Celsius: "); scanf("%f", &temp); temp = (temp * 9/5) + 32; printf("The temp in Fahrenheit is: %.1f\n", temp); } else if(unit == 'F') { printf("Enter the temp in Fahrenheit: "); scanf("%f", &temp); temp = ((temp - 32) * 5) / 9; printf("The temp in Celsius is: %.1f\n", temp); } else { printf("%c is not a valid unit of measurement", unit); }
}