Equality and relational operators

Equality and relational expressions.

Write an expression that will cause “Dollar or less” to print if the value of numCents is less than or equal to 100.

#include <stdio.h>

int main(void) {
 int numCents;

scanf("%d", &numCents); // Program will be tested with values: 99, 100, 101, 102.

if (numCents <= 100) {
 printf("Dollar or less\n");
 }
 else {
 printf("More than a dollar\n");
 }

return 0;
}

Write an expression that will cause “less or equal to -10” to print if the value of userNum is less than or equal to -10.

#include <stdio.h>

int main(void) {
 int userNum;

scanf("%d", &userNum); // Program will be tested with values: -8, -9, -10, -11.

if (userNum <= -10) {
 printf("less or equal to -10\n");
 }
 else {
 printf("more than -10\n");
 }

return 0;
}

Complete the expression so that userPoints is assigned with 0 if userItems is greater than 30 (second branch). Otherwise, userPoints is assigned with 10 (first branch).

The <= operator can be used to evaluate if userItems is greater than 30. The expression should evaluate to false so that the second branch executes and assigns userPoints with 0

#include <stdio.h>

int main(void) {
 int userItems;
 int userPoints;

scanf("%d", &userItems); // Program will be tested with values: 15, 20, 25, 30, 35.
 userPoints = 0;

if (userItems <= 30) {
 userPoints = 10;
 }
 else {
 userPoints = 0;
 }

printf("%d\n", userPoints);

return 0;
}

If-else statement: Fix errors..

Find and fix the error in the if-else statement…

fix

#include <stdio.h>

int main(void) {
 int userNum;

scanf("%d", &userNum); // Program will be tested with values: 1, 2, 3, 4.

if (userNum = 2) {
 printf("Num is less or equal to two\n");
 }
 else {
 printf("Num is greater than two\n");
 }

return 0;
}

solution

#include <stdio.h>

int main(void) {
 int userNum;

scanf("%d", &userNum); // Program will be tested with values: 1, 2, 3, 4.

if (userNum <= 2) { //needed the less than symbol
 printf("Num is less or equal to two\n");
 }
 else {
 printf("Num is greater than two\n");
 }

return 0;
}

If-else statement: Print senior citizen.

#include <stdio.h>

int main(void) {
 int patronAge;

patronAge = 55;


 if (patronAge >= 55){
 printf("Senior citizen\n");
 }
 else {
 printf("Not senior citizen\n");
 }


 return 0;
}