Creating a simple code that scans two numbers, asks the user if they would like to add or multiply them, and then performs the operation and prints the output.
#include <stdio.h>
int main(){
int num1;
int num2;
char oper[] = "";
printf("Enter a number: ");
scanf("%d", &num1);
printf("Enter another number: ");
scanf("%d", &num2);
printf("Would you like to add or multiply these numbers? ");
scanf("%s", &oper);
if(strcmp(oper, "multiply") == 0){
int prod = num1 * num2;
printf("The product is %d", prod);
}
else if(strcmp(oper, "add") == 0){
int sum = num1 + num2;
printf("The sum is %d", sum);
}
else{
printf("Why would you input something that you knew wouldn't work?");
}
return 0;
}
You are using strcmp function without declaring them. You need to include the header file which contains strcmp function declaration.
Use:
#include <string.h>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With