Question

Write code in C to demo how to test the content of a variable and do...

Write code in C to demo how to test the content of a variable and do some action accordingly.
Add a one line comment above the code to explain the code.

Here is an example code:
/* the following code checks if variable x is divisible by variable y and displays a message accordingly */
int x,y;
printf( "Enter two numbers \n" );
scanf("%d %d",&x,&y);
if (x % y ==0) { // use == to test for equality % returns remainder of dividing x by y could remove ==0(How)?
printf(" Divisible "); }
else
  printf("Not Divisible "); }

Then, write the code using switch statements.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

OUTPUT

Enter a digit: 4
That's four

CODE

#include <stdio.h>

int main(void) {

int n;

printf("Enter a digit: ");

scanf("%d", &n);

switch(n)

{

case 1:

printf("That's one");

break;

case 2:

printf("That's two");

break;

case 3:

printf("That's three");

break;

case 4:

printf("That's four");

break;

default:

printf("You entered more than four");

}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write code in C to demo how to test the content of a variable and do...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write code in C to demo how to test the content of a variable and do...

    Write code in C to demo how to test the content of a variable and do some action accordingly. Add a one line comment above the code to explain the code. Here is an example: /* the following code checks if variable x is divisible by variable y and displays a message accordingly */ int x,y; printf( "Enter two numbers \n" ); scanf("%d %d",&x,&y); if (x % y ==0) { // use == to test for equality % returns remainder...

  • C programming Rewrite the following code replacing the else-if construct with a switch statement. Make sure...

    C programming Rewrite the following code replacing the else-if construct with a switch statement. Make sure you test your code. Supplied code #include <stdio.h> int main(void) { char ch; int countA = 0; int countE = 0; int countI = 0; printf("Enter in a letter A, E, or I.\n"); scanf(" %c", &ch); //Replace the following block with your switch if(ch == 'E' || ch == 'e') countE++; else if(ch == 'A' || ch == 'a') countA++; else if(ch == 'I'...

  • C linux please write it by only using “if and else” conditions, thanks so much this...

    C linux please write it by only using “if and else” conditions, thanks so much this is my task 3 Requirements 1. Copy your solution for Task 3 t3.c to a new file t4.c. 2. The input is guaranteed to contain at least one non-whitespace character. 3. If the input is well-formed, i.e., can be parsed to a number, the program should behave identically to Task 3. All the requirements of Task 3 still apply except the file name. 4....

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • 20) What is the output of the following segment of C code: int avg(int n, int*...

    20) What is the output of the following segment of C code: int avg(int n, int* a); int main () {             int array[4]={1,0,6,9};             printf("%d", avg(4, array)+ 1);             system("pause");             return 0; } int avg(int n, int* a) { int i, sum=0; for (i=0;i<n;i++) { sum+=a[i]; } return sum/n; } a) 16 b) 5 c) 4 d) 8 21) What is the output of the following segment of C code: int x = 2; int y = 3;...

  • C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt...

    C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...

  • the coding language is just the basic c language and here is my first attempt at...

    the coding language is just the basic c language and here is my first attempt at this problem my problem is that if the user inputs a number that is equal to a number that has been entered previously the code will never end until the user enters a number that is bigger than any other number previously entered any help will be helpful Write a program to read-in a sequence of integers from the keyboard using scanf(). Your program...

  • Question- How would I allow the program to run both upper and lower case letters. How...

    Question- How would I allow the program to run both upper and lower case letters. How would I write a switch statement for upper and lower cases to see if the value entered for Grade2 is a A or a? C programming int main() { char Grade2; float gradepoint; char Grade = 'X'; // Declares a character type variable named Grade printf("Enter a grade\t"); // Prompts for Grade scanf("%c", &Grade); // Inputs Grade printf("Grade is: \t%c\n", Grade); // Prints the...

  • I have a question on an assignment for C++. I have my code available here and...

    I have a question on an assignment for C++. I have my code available here and just want someone to look over it using MS VS 2010 express, for that is what my instructor is using. Write a program that mimics a calculator. The program should take as input two integers and the opreation to be performed. It should then output the numbers, the operator, and the result. (For division, if the denominator is zero, output an appropriate message.) //**This...

  • C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want...

    C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT