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;
       
   if(avg>=90)printf("Grade A");
   else if((avg>=70) && (avg<90)) printf("Grade B");
   else if((avg>=50) && (avg<70))printf("Grade C");
   else if(avg<50) printf("Grade F");
   else printf("Invalid");
   
   printf("\n");
 
   system("PAUSE");  
   return 0;
}


Comments

Judit comment

 Judit

I was missing from the above programs what is the highest score number they could enter.Other than that my program works fine too :)
Slightly different than the others :)

int main(){
int score1;
int score2;
int score3;
float all;
float a9,a7,a5,a6;

printf("Enter 3 score numbers up to 100: \n");
scanf("%d %d %d",&score1,&score2,&score3);

all=score1+score2+score3;
printf("%.2f\n",all);

a9=300*0.9;
a7=300*0.7;
a5=300*0.5;
a6=300*0.4;

printf("%.2f %.2f %.2f %.2f\n",a9,a7,a5,a6);

if(all>=a9){
printf("Grade A");
}
else if(all>=a7){
printf("Grade B");
}else if(all>=a5) {
printf("Grade C");
} else if(all<a6){
printf("Failed!!");
}

return 0;
}


2018-06-13
Rahat comment

 Rahat

#include<stdio.h>
#include<math.h>
int main()
{
int x,y,z,avr;
printf("enter the value of x,y,z \n");
scanf("%d %d %d",&x,&y,&z);
avr=(x+y+z)/3;
printf("%d",avr);

if(avr>=90)
{
printf("Grade A");
}
else if(avr<90&&avr>=70)
{
printf("Grade B");
}
else if(avr<70&&avr>=50)
{
printf("Grade C");
}
else
{
printf("Grade F");
}


return 0;
}


2017-11-02
Dee comment

 Dee

#include <stdio.h>
#include<stdlib.h>
int main (){
float score1, score2, score3;
score1=77.5;
score2=99;
score3=69.5;
if(score1>=90){
printf("GRADE A=%.1f\n",score1);
}else if(score1>=70 && score1<=90){
printf("GRADE B=%.1f\n",score1);
}else if(score1>=50 && score1<=70){
printf("GRADE C=%.1f\n",score1);
}else {
printf("GRADE F+%.1f\n",score1);
}
if(score2>=90){
printf("GRADE A=%.1f\n",score2);
}else if(score2>=70 && score1<=90){
printf("GRADE B=%.1f\n",score2);
}else if(score2>=50 && score2<=70){
printf("GRADE C=%.1f\n",score2);
}else {
printf("GRADE F+%.1f\n",score2);
}
if(score3>=90){
printf("GRADE A=%.1f\n",score3);
}else if(score3>=70 && score3<=90){
printf("GRADE B=%.1f\n",score3);
}else if(score3>=50 && score3<=70){
printf("GRADE C=%.1f\n",score3);
}else {
printf("GRADE F+%.1f\n",score3);
}
return 0;
}


2017-03-30
Dee comment

 Dee

#include <stdio.h>
#include<stdlib.h>
int main(){
int a,b,c;
a=10;b=15;c=203;
if(a>=b&&a>=c){
printf("%d is the greatest",a);
}else if(b>=a && b>=c){
printf("%d is the greatest",b);
}else{
printf("%d is the greatest",c);
}
return 0;
}


2017-03-30



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.