In: Computer Science
Write a code fragment in C language that tests the value of an integer num1. If the value is 10, square num1. If it is 9, read a new value into num1. If it is 2 or 3, multiply num1 by 99 and print out the result. Implement your code using SWITCH statements
Here are the desired outputs
Output 1
Output 2
Output 3
Output 4
And here is the C code
#include <stdio.h>
int main(void) {
int num1;
L1:
printf("Enter num1 value\n");
scanf("%d",&num1);
switch(num1)
{
case 10: num1=num1*num1;
printf("The new value of num is %d", num1);
break;
case 9: printf("Re-enter a new value for num1\n");
goto L1;
break;
case 2: num1 = num1*99;
printf("The new value of num is %d", num1);
break;
case 3: num1 = num1*99;
printf("The new value of num is %d", num1);
break;
default: printf("Entered value of num1 isn't right. Either enter 9 or 10 or 2 or 3\n");
break;
}
return 0;
}
A humble request. If you liked the answer please don't forget to like or upvote it. Your appreciation motivates experts to help other students too. Thank you :)