C program to illustrate function with arguments and return value.
/*
* Function with argument and return value.
* funWithArg - performs addition, subtraction,
* multiplication and division.
*/
int funWithArg(int a, int b, int ch) {
int res;
switch (ch) {
case 1:
res = a + b;
break;
case 2:
res = a - b;
break;
case 3:
res = a * b;
break;
case 4:
res = a / b;
break;
}
return res;
}
int main() {
int a, b, ch, res;
printf("1. Addition\n2. Subtraction\n");
printf("3. Multiplication\n4. Division\n");
printf("Enter your choice:");
scanf("%d", &ch);
if (ch < 1 || ch > 4) {
printf("Wrong Option!!\n");
return 0;
}
/* get the inputs from the user */
printf("Enter your inputs(a & b):");
scanf("%d%d", &a, &b);
/* perform arithmetic operation */
res = funWithArg(a, b, ch);
/* print the result */
printf("Output: %d\n", res);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter your choice:1
Enter your inputs(a & b):100 12345
Output: 12445
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter your choice:1
Enter your inputs(a & b):100 12345
Output: 12445
No comments:
Post a Comment