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
of dividing x by y could remove ==0
(How)?
printf(" Divisible "); }
else
printf("Not Divisible "); }
Then, write the code using switch statements.
Note: for the ‘how?’ part in given example code, we can get rid of ==0 from if statement. In C if you simply use x%y in conditions instead of x%y==0, x%y evaluates to true, if it yields a non zero value, and false if it yields a zero value. So we might need to swap the messages being printed. So it should be like below if you are planning to get rid of ==0
if(x%y){
printf("Not Divisible\n");
}else{
printf("Divisible\n");
}
Following the given example code’s footsteps, the below code refers to a simple program that accepts a letter grade from user, and display a proper remarks for that grade after analyzing the grade. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
int main(){
char grade;
//prompting and getting a letter grade
printf("Enter your grade (A/B/C/D/F) : ");
scanf("%c",&grade);
//the below code displays the proper remarks for a given grade
if(grade=='A'){
printf("Outstanding\n");
}else if(grade=='B'){
printf("Good\n");
}else if(grade=='C'){
printf("Average\n");
}else if(grade=='D'){
printf("Satisfactory\n");
}else if(grade=='F'){
printf("Failed\n");
}else{
//anything other than A/B/C/D/F
printf("That is an invalid grade\n");
}
return 0;
}
Below is the same program, using switch statements
#include<stdio.h>
int main(){
char grade;
//prompting and getting a letter grade
printf("Enter your grade (A/B/C/D/F) : ");
scanf("%c",&grade);
//the below code displays the proper remarks for a given grade
switch(grade){
case 'A':
printf("Outstanding\n");
break;
case 'B':
printf("Good\n");
break;
case 'C':
printf("Average\n");
break;
case 'D':
printf("Satisfactory\n");
break;
case 'F':
printf("Failed\n");
break;
default:
//anything other than A/B/C/D/F
printf("That is an invalid grade\n");
}
return 0;
}
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...
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 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 *); 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* 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 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 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 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...
My homework is to write a tic tac toe function, this code isn't working and I can not find the error. It prints the board and accepts user input just fine, the only problem is the checkBoard function. I can not get it to return a value(player win) to end the while loop. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int checkBoard(char board[3][3]) { int i, j; for (i = 0; i < 3; i += 1) { if...
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...