Write a C program to illustrate goto statement.
#include <stdio.h>
int main() {
int num1, num2, ch, res;
/* get the input values from the user */
printf("Enter your input for num1 and num2:");
scanf("%d%d", &num1, &num2);
/* get the user choice */
printf("1. Addition, subtraction, multiplication\n");
printf("2. Multiplication alone\nEnter your choice:");
scanf("%d", &ch);
/* if the choice is 2, then execute instructions from divide */
if (ch == 2)
goto divide;
/* Addition of two numbers */
res = num1 + num2;
printf("Addition Result(%d + %d): %d\n", num1, num2, res);
/* Addition of two numbers */
res = num1 - num2;
printf("Subtraction Result(%d - %d): %d\n", num1, num2, res);
divide:
/* Multiplication of two numbers */
res = num1 * num2;
printf("Multiplication Result(%d X %d): %d\n", num1, num2, res);
return 0;
}
int main() {
int num1, num2, ch, res;
/* get the input values from the user */
printf("Enter your input for num1 and num2:");
scanf("%d%d", &num1, &num2);
/* get the user choice */
printf("1. Addition, subtraction, multiplication\n");
printf("2. Multiplication alone\nEnter your choice:");
scanf("%d", &ch);
/* if the choice is 2, then execute instructions from divide */
if (ch == 2)
goto divide;
/* Addition of two numbers */
res = num1 + num2;
printf("Addition Result(%d + %d): %d\n", num1, num2, res);
/* Addition of two numbers */
res = num1 - num2;
printf("Subtraction Result(%d - %d): %d\n", num1, num2, res);
divide:
/* Multiplication of two numbers */
res = num1 * num2;
printf("Multiplication Result(%d X %d): %d\n", num1, num2, res);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input for num1 and num2:100 10
1. Addition, subtraction, multiplication
2. Multiplication alone
Enter your choice:2
Multiplication Result(100 X 10): 1000
jp@jp-VirtualBox:~/$ ./a.out
Enter your input for num1 and num2:30 10
1. Addition, subtraction, multiplication
2. Multiplication alone
Enter your choice:1
Addition Result(30 + 10): 40
Subtraction Result(30 - 10): 20
Multiplication Result(30 X 10): 300
Enter your input for num1 and num2:100 10
1. Addition, subtraction, multiplication
2. Multiplication alone
Enter your choice:2
Multiplication Result(100 X 10): 1000
jp@jp-VirtualBox:~/$ ./a.out
Enter your input for num1 and num2:30 10
1. Addition, subtraction, multiplication
2. Multiplication alone
Enter your choice:1
Addition Result(30 + 10): 40
Subtraction Result(30 - 10): 20
Multiplication Result(30 X 10): 300
No comments:
Post a Comment