0% found this document useful (0 votes)
3 views

if- if_else

The document contains a series of C programming exercises that demonstrate the use of conditional statements (if, else) to solve various problems. Each exercise includes a description, code implementation, and expected output, covering topics such as checking odd/even numbers, finding maximum and minimum values, grading based on marks, and determining leap years. Additionally, it includes a quadratic equation solver that calculates the roots based on user input.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

if- if_else

The document contains a series of C programming exercises that demonstrate the use of conditional statements (if, else) to solve various problems. Each exercise includes a description, code implementation, and expected output, covering topics such as checking odd/even numbers, finding maximum and minimum values, grading based on marks, and determining leap years. Additionally, it includes a quadratic equation solver that calculates the roots based on user input.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

If, If…else

1. Write a program that read an integer and print odd or even.

Code:
#include<stdio.h>
int main()
{
int a;
printf("Enter an integer number: ");
scanf("%d",&a);
if(a%2 ==0)
{
printf("%d is even number.",a);
}
else
{
printf("%d is odd number.",a);
}
return 0;
}

Output:
2. Write a program that read two integer numbers and display maximum.

Code:
#include<stdio.h>
int main()
{
int a,b;
printf("Enter the first number: ");
scanf("%d",&a);
printf("Enter the second number: ");
scanf("%d",&b);

if(a>b)
{
printf("%d is maximum.",a);
}
else
{
printf("%d is maximum.",b);
}
return 0;
}

Output:
3. Write a program that read two integer numbers and display minimum.

Code:
#include<stdio.h>
int main()
{
int a,b;
printf("Enter the first number: ");
scanf("%d",&a);
printf("Enter the second number: ");
scanf("%d",&b);
if(a<b)
{
printf("%d is minimum.",a);
}
else
{
printf("%d is minimum.",b);
}
return 0;
}
Output:
4. Write a program that read three integer numbers and display maximum.

Code:
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter the first number: ");
scanf("%d",&a);
printf("Enter the second number: ");
scanf("%d",&b);
printf("Enter the third number: ");
scanf("%d",&c);
if(a>b && a>c)
{
printf("%d is maximum.",a);
}
else if(b>c)
{
printf("%d is maximum.",b);
}
else
{
printf("%d is maximum.",c);
}
return 0;
}

Output:
5. Write a program that read three numbers and display minimum.

Code:
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter the first number: ");
scanf("%d",&a);
printf("Enter the second number: ");
scanf("%d",&b);
printf("Enter the third number: ");
scanf("%d",&c);

if(a<b && a<c)


{
printf("%d is minimum.",a);
}
else if(b<c)
{
printf("%d is minimum.",b);
}
else
{
printf("%d is minimum.",c);
}
return 0;
}

Output:
6. Write a program that read three numbers and display medium.

Code:
#include<stdio.h>

int main()
{
int a,b,c;
printf("Enter the first number: ");
scanf("%d",&a);
printf("Enter the second number: ");
scanf("%d",&b);
printf("Enter the third number: ");
scanf("%d",&c);

if((a>b && b>c) || (c> b && b>a))


{
printf("%d is medium.",b);
}
else if((a>c && c>b) || (b>c && c>a))
{
printf("%d is medium.",c);
}
else
{
printf("%d is medium.",a);
}
return 0;
}

Output:
7. Write a program that read mark and display pass or fail.

Code:
#include<stdio.h>
int main()
{
double marks;
printf("Enter mark: ");
scanf("%lf",&marks);

if(marks < 0 || marks >100 )


{
printf("Enter a valid mark.");
}
else if(marks >=40)
{
printf("Congratulations!!! You have passed");
}
else
{
printf("You have failed!!!");
}
return 0;
}

Output:
8. Write a program that read mark and display result in division.

Code:
#include<stdio.h>
int main()
{
double marks;
printf("Enter mark: ");
scanf("%lf",&marks);
if(marks < 0 || marks >100 )
{
printf("Enter a valid mark.");
}
else if(marks >=60)
{
printf("First Division");
}
else if(marks >=50 && marks <=59)
{
printf("Second Division");
}
else if(marks >= 40 && marks <=49 )
{
printf("Third Division");
}
else
{
printf("Letter Grade : F");
}
return 0;
}
Output:
9. Write a program that read mark and display result in grade.

Code:
#include<stdio.h>
int main()
{
double marks;
printf("Enter mark: ");
scanf("%lf",&marks);

if(marks < 0 || marks >100 )


{
printf("Enter a valid mark.");
}
else if(marks >=80)
{
printf("Letter Grade : A+");
}
else if(marks >=75 && marks <=79)
{
printf("Letter Grade : A");
}
else if(marks >= 70 && marks <=74 )
{
printf("Letter Grade : A-");
}
else if(marks >= 65 && marks <=69 )
{
printf("Letter Grade : B+");
}
else if(marks >= 60 && marks <=64)
{
printf("Letter Grade : B");
}
else if(marks >= 55 && marks <=59)
{
printf("Letter Grade : B-");
}
else if( marks >= 50 && marks <=55 )
{
printf("Letter Grade : C+");
}
else if( marks >= 45 && marks <=49)
{
printf("Letter Grade : C");
}
else if( marks >= 40 && marks <=44)
{
printf("Letter Grade : C-");
}
else
{
printf("Letter Grade : F");
}
return 0;
}

Output:
10. Write a program that read any year and display leap year or not leap year.

Code:
#include<stdio.h>
int main()
{
int year;
printf("Enter Year: ");
scanf("%d",&year);
if(year % 4 == 0 )
{
if(year % 100 == 0 )
{
if(year % 400 == 0 )
{
printf("%d is Leap Year. ",year);
}
else
{
printf("%d is not a Leap Year.", year);
}
}
else
{
printf("%d is a Leap Year.", year);
}
}
else
{
printf("%d is not a Leap Year.", year);
}
return 0;
}
Output:
11. Write a program that read three numbers (a,b,c) and determine the root of
the quadratic equation:
ax2+bx+c=0

Code:
#include<stdio.h>
#include<math.h>

int main()
{
double a,b,c;
double discriminant;
double root1, root2;
printf("Enter The value of a: ");
scanf("%lf",&a);
printf("Enter The value of b: ");
scanf("%lf",&b);
printf("Enter The value of c: ");
scanf("%lf",&c);

if(a <= 0 )
{
printf("Enter a value of a > 0. ");
}
else
{
discriminant = b*b - 4.0*c*a;
if(discriminant >= 0)
{
root1 = (-b+ sqrt(discriminant)) / (2.0 * a);
root2 = (-b- sqrt(discriminant)) / (2.0 * a);
printf("Root 1: %lf", root1);
printf("\nRoot 2: %lf", root2);
}

else
{
printf("No real Root");
}
}
return 0;
}

Output:

You might also like