﻿ C exercises and solutions: c conditional statements if else

# C exercises and solutions: c conditional statements if else

## C conditional statements if else exercises

### 1. Write a C code that prompts the user to input tree integer values and find the greatest value of the three values.

Example:
Enter 3 integer vales separated by space: 10 15 20
The greatest value is: 20 Solution:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

int x, y, z, max;
printf("Enter 3 integer vales separated by space:");
scanf("%d %d %d",&x,&y,&z);
max=x;    if(max<y) max=y;    if(max<z) max=z;     printf("The max is %d.",max);
printf("\n");

system("PAUSE");
return 0;
}

### 2. Write a program that determines a student’s grade. The program will read three scores and determine the grade based on the following rules:

-if the average score =90% =>grade=A
-if the average score >= 70% and <90% => grade=B
-if the average score>=50% and <70% =>grade=C
-if the average score<50% =>grade=F Solution:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

float x;
float y;
float z;
float avg;
printf("Enter 3 score vales separated by space:");
scanf("%f %f %f",&x,&y,&z);
avg=(x+y+z)/3;

else if((avg>=70) && (avg<90)) printf("Grade B");
else printf("Invalid");

printf("\n");

system("PAUSE");
return 0;
}