Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C Programing Part II Write a program: Ask the user to enter a number between 1 a

ID: 3666616 • Letter: C

Question

C Programing Part II

Write a program:

Ask the user to enter a number between 1 and 20. If they enter a number outside of that range, print an error message, and call the function:

exit(-1); // stop the program immediately with an error code of -1.

Use a switch statement to output the corresponding Roman numeral for the number that they entered. Note that you don’t have to do any calculations for this, just a big switch with 20 cases, and a default case for numbers out of range.

Part III

You can generate a random number in C using the following code:

int myRandomNumber;

srand(time(NULL)); // seed the random number generator

myRandomNumber = rand(); // each time you call this function there will be a different random number.

Generate a random number, and output it. Use if statements to determine if the number is odd or even, and output a message to that effect. Similarly, output if the number is divisible by 3, and if it is divisible by 10. Use the % operator to achieve this.

Run the program, and print the results several times using different numbers to be sure that it’s working.

Hint: In C you can not declare variables after you write executable code. For example if you call the srand function, and then declare some more variables after that call, the program will not compile. The compiler messages are not very helpful. Put all the variable declarations at the top of the function.

Explanation / Answer

//Program to print roman number

#include<stdio.h>

int main(){
  
   int number;
   printf("Enter number : ");
   scanf("%d",&number);
  
   switch(number){
       case 1:
           printf("Roman representation: I");
           break;
       case 2:
           printf("Roman representation: II");
           break;
       case 3:
           printf("Roman representation: III");
           break;
       case 4:
           printf("Roman representation: IV");
           break;
       case 5:
           printf("Roman representation: V");
           break;
       case 6:
           printf("Roman representation: VI");
           break;
       case 7:
           printf("Roman representation: VII");
           break;
       case 8:
           printf("Roman representation: VIII");
           break;
       case 9:
           printf("Roman representation: IX");
           break;
       case 10:
           printf("Roman representation: X");
           break;
       case 11:
           printf("Roman representation: XI");
           break;
       case 12:
           printf("Roman representation: XII");
           break;
       case 13:
           printf("Roman representation: XIII");
           break;
       case 14:
           printf("Roman representation: XIV");
           break;
       case 15:
           printf("Roman representation: XV");
           break;
       case 16:
           printf("Roman representation: XVI");
           break;
       case 17:
           printf("Roman representation: XVII");
           break;
       case 18:
           printf("Roman representation: XVIII");
           break;
       case 19:
           printf("Roman representation: XIX");
           break;
       case 20:
           printf("Roman representation: XX");
           break;
       default:
           exit(1);
  
   return 0;
   }
}

//Part III, Odd-Even Program

#include<stdio.h>
#include<stdlib.h>

int main(){
   int num;
   srand(time(NULL));
   num = rand()/32767; // integer range
  
   printf("Number is: %d ",num);
  
   if(num%2==0)
       printf("Number is Even ");
   else
       printf("Number is Odd ");
  
   if(num%3==0)
       printf("Number is divisible by 3 ");
   else
       printf("Number is not divisible by 3 ");
      
   if(num%10==0)
       printf("Number is divisible by 10 ");
   else
       printf("Number is not divisible by 10 ");

   return 0;
}