Question: /* loop-control-chk1.c - This program requests a positive integer. Functionalities include reversing a number, determining if the number is even or odd, and determining if
/* loop-control-chk1.c - This program requests a positive integer. Functionalities include reversing a number, determining if the number is even or odd, and determining if the number is prime. */
int main(void) {
char ch = 'N'; /* variable to accept the user's choice, N represents the condition not to exit from the program */ int num; /* variable to accept a number */ while( (ch != 'Y') && (ch != 'y') )
{ printf("Enter R to reverse the digits of the number "); printf("Enter E to determine if the number is even or odd "); printf("Enter P to determine if the number is prime "); printf("Enter Y or y to exit the program "); printf("Enter your choice: "); /*The fflush() function writes any buffered data for the stream specified by the
stream parameter and causes any unwritten data for that stream to be written to the file. In this case, the stream parameter 'stdin' takes input from the prompt. */
fflush(stdin); scanf("%c", &ch); switch(ch) {
case 'R': case r': printf("Enter the number to be reversed: ");
scanf("%d", &num); break; case 'E': case 'e': printf("Enter the number to check if it is even or odd: "); scanf("%d", &num);
break; case 'P': case 'p': printf("Enter the number to check if it is prime or not: "); scanf("%d", &num);
break; case Y: case y: printf(Exiting the program. ); break; default: printf("You have entered a wrong choice. Try again "); break;
} /* switch */ } /* while */
return(0); } /* main */
Checkpoint 1 Enter the code for cases 'R' and 'r', which correspond to reversing the digits of a number (any positive integer). Use following 'while' loop to implement this functionality; while(num > 0) {
printf(%d, num%10);
num = num/10; }
Put this code in the implementation of the function reverse_number() and call this function after the scanf() of the switch statement.
Checkpoint 2 For the same cases 'R' and 'r', implement the program using a 'do-while' loop.
Checkpoint 3 Enter code for the cases 'E' and 'e', where you will be checking to see if the number (positive integer) obtained through the keyboard is even or odd. Hint: A number is even if it is divisible by 2 otherwise it is odd. You can check if a number is divisible by 2 by taking modulo 2 (num%2) of that number. If the modulus operations return 0, it means that the number is divisible by 2, else it is not. Use an if-else statement for this purpose.
Checkpoint 4 Write the code for cases P and p that determines whether a number (positive integer) obtained through the keyboard is prime or not. Use following code to implement this: flag = 0; for(i=2; i<=num/2; i++) {
if(num%i == 0) {
flag = 1;
break; }
} if(flag == 0)
printf(Number is prime ); else
printf(Number is not prime ); You need to declare variables flag and i of type int.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
