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

This C Program input 3 arguments and operate appropriately on the numbers.
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. }

$ gcc arg2.c
$ a.out 5 4 +
arguments entered:
5 4 +
The result of the operation is 9
$ a.out 8 7 -
arguments entered:
8 7 -
The result of the operation is 1
$ a.out 9 6 x
arguments entered:
9 6 x
The result of the operation is 54
$ a.out 100 10 /
arguments entered:
100 10 /
The result of the operation is 10

0 comments:

Post a Comment

 
Top