C Program to Input 3 Arguments and Operate Appropriately on the Numbers

«
»

This is a C Program to input 3 arguments and operate appropriately on the numbers.

Problem Description

This program take 3 arguments as input and operate appropriately on the numbers.

Problem Solution

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.

advertisement
Program/Source Code

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.

  1. /* 
  2.  * C Program to Input 3 Arguments and Operate Appropriately on the 
  3.  * Numbers
  4.  */
  5. #include <stdio.h>
  6.  
  7. void main(int argc, char * argv[])
  8. {
  9.     int a, b, result;
  10.     char ch;
  11.  
  12.     printf("arguments entered: \n");
  13.     a = atoi(argv[1]);
  14.     b = atoi(argv[2]);
  15.     ch  = *argv[3];
  16.     printf("%d %d %c", a, b, ch);
  17.     switch (ch)
  18.     {
  19.     case '+':
  20.         result = a + b;
  21.         break;
  22.     case '-':
  23.         result = a - b;
  24.         break;
  25.     case 'x':
  26.         result = a * b;
  27.         break;
  28.     case '/':
  29.         result = a / b;
  30.         break;
  31.     default:
  32.         printf("Enter a valid choice");
  33.     }
  34.     printf("\nThe result of the operation is %d", result);
  35.     printf("\n");    
  36. }
Program Explanation

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.

advertisement
Runtime Test Cases
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 Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn