Question

Write a C program requesting the user to input two integer numbers and then requesting the...

Write a C program requesting the user to input two integer numbers and then requesting the user to either add, subtract, or multiply the two numbers (choose 0 to add, 1 to subtract, or 2 to multiply. Declares three separate functions for these choices to come after main(). Program needs to use a pointer to these three functions to perform the requested action. Must print to output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
int add(int,int);
int sub(int,int);
int mul(int,int);

int main()
{
int n1,n2,op;
printf("Enter 2 numbers : ");
//reading 2 numbers from user
scanf("%d%d",&n1,&n2);
printf("choose 0 to add, 1 to subtract, or 2 to multiply");
scanf("%d",&op);
//declaring function pointer
int (*fp)(int, int);
//assinging function name based on user input
if(op==0)
fp=add;
if(op==1)
fp=sub;
if(op==2)
fp=mul;
  
printf("Result is : %d",fp(n1,n2));
return 0;
}
int add(int n1,int n2){
return n1+n2;
}
int sub(int n1,int n2){
return n1-n2;
}
int mul(int n1,int n2){
return n1*n2;
}

Add a comment
Know the answer?
Add Answer to:
Write a C program requesting the user to input two integer numbers and then requesting the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT