This is a C Program to input 3 arguments and operate appropriately on the numbers.
This program take 3 arguments as input and operate appropriately on the numbers.
1. Take two numbers and a operator as 3 arguments.
2. Use switch statement to test the operator.
3. According to the operator, do the operation and exit.
Here is source code of the C Program to input 3 arguments and operate appropriately on the numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Input 3 Arguments and Operate Appropriately on the
* Numbers
*/
#include <stdio.h>
void main(int argc, char * argv[])
{
int a, b, result;
char ch;
printf("arguments entered: \n");
a = atoi(argv[1]);
b = atoi(argv[2]);
ch = *argv[3];
printf("%d %d %c", a, b, ch);
switch (ch)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case 'x':
result = a * b;
break;
case '/':
result = a / b;
break;
default:
printf("Enter a valid choice");
}
printf("\nThe result of the operation is %d", result);
printf("\n");
}
1. Take two numbers and a operator as input and store it in the variables a, b and ch respectively.
2. Using switch statement, test the operator stored in the variable ch.
3. If it is +, then add a & b and break.
4. If it is -, then subtract a & b and break.
5. If it is *, then multiply a & b and break.
6. If it is /, then divide a & b and break.
7. In the default case print it as “Enter a valid choice”.
8. Store the solution got at steps 3-6 in the variable result.
9. Print the variable result as output and exit.
arguments entered: 5 4 + The result of the operation is 9 arguments entered: 8 7 - The result of the operation is 1 arguments entered: 9 6 x The result of the operation is 54 arguments entered: 100 10 / The result of the operation is 10
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for Computer Science Internship
- Practice BCA MCQs
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Buy C Books