C-Conditional Statements
C-Conditional Statements
#include <stdio.h>
int max_of_four(int a,int b,int c,int d)
{
if(a>b && a>c && a>d)
return a;
else if(b>a && b>c && b>d)
return b;
else if(c>a && c>b && c>d)
return c;
else {
return d;
}
}
int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int ans = max_of_four(a, b, c, d);
printf("%d", ans);
return 0;
}
else
printf("It is not a leap year");
return 0;
C program to accept the height of a person in centimeters and categorize the person according
to their height.
#include<stdio.h>
int main()
{
int height;
printf("Height in cm:");
scanf("%d",&height);
if(height>150)
printf("The person is tall");
else
{
if(height<150)
printf("The person is dwarf");
else
printf("The person is in average height");
}
}
Or
#include <stdio.h>
void main()
{
float PerHeight;
printf("Input the height of the person (in centimetres) :");
scanf("%f", &PerHeight);
if (PerHeight < 150.0)
printf("The person is Dwarf. \n");
else if ((PerHeight >= 150.0) && (PerHeight < 165.0))
printf("The person is average heighted. \n");
else if ((PerHeight >= 165.0) && (PerHeight <= 195.0))
printf("The person is taller. \n");
else
printf("Abnormal height.\n");
}
To check in which coordinate the point lies:
#include<stdio.h>
int main()
{
int a,b,res;
printf("Enter the coordinates:");
scanf("%d %d",&a,&b);
//printf("The coordinates are (%d,%d)",a,b);
if(a>0 && b>0)
printf("\nThe coordinates (%d %d) lie in the First quadrant",a,b);
else if(a<0 && b>0)
printf("\nThe coordinates (%d %d) lies in the Second quadrant",a,b);
else if(a<0 && b<0)
printf("\nThe coordinates (%d %d) lies in the third quadrant",a,b);
else if(a>0 && b<0)
printf("\nThe coordinates (%d %d) lies in the fourth quadrant",a,b);
else
printf("\nThe coordinates (%d %d) lies in origin",a,b);
return 0;
}
Eligibility criteria for student:
#include<stdio.h>
int main()
{
int mat,phy,chem,total;
printf("Enter your marks:");
scanf("%d %d %d",&mat,&phy,&chem);
total=mat+phy+chem;
if(mat >=65 && phy >=55 && chem>=50)
{
if(total>=190 || (mat+phy>=140))
printf("The candidate is eligible");
else
printf("The candidate is not eligible");
}
else
printf("The candidate is not eligible");
return 0;
}
Root of quadratic equation:
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c,root;
float x1,x2;
scanf("%d %d %d",&a,&b,&c);
root=b*b-4*a*c;
if(root==0)
{
printf("Both roots are equal");
x1=-b/(2*a);
x2=x1;
printf("The first root: %f",x1);
printf("The second root: %f",x2);
}
else if(root>0)
{
printf("Both the roots are real and different");
x1=(-b+sqrt(root))/(2*a);
x2=(-b-sqrt(root))/(2*a);
printf("The first root: %f",x1);
printf("The second root: %f",x2);
}
else
printf("Roots are imaginary\n");
printf("No solution\n");
return 0;
}
Triangle formation:
#include<stdio.h>
int main()
{
int a,b,c;
scanf("%d %d %d",&a, &b, &c);
if(a<(b+c)&&b<(a+c)&&c<(a+b))
{
if(a==b&&a==c&&b==c)
printf("Equilateral Triangle");
else if(a==b||a==c||b==c)
printf("Isosceles Triangle");
else if((a*a)==(b*b)+(c*c)||(b*b)==(a*a)+(c*c)||(c*c)==(a*a)+(b*b))
printf("Right-angle Triangle");
else if(a!=b&&a!=c&&b!=c)
printf("Scalene Triangle");
}
else
printf("Triangle is not possible");
return 0;
}
To check for an alphabet or digit or special character:
#include <stdio.h>
int main()
{
char ch;
printf("Input a character: ");
scanf("%c", &ch);
case 1:
printf("one\n");
break;
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
case 4:
printf("Four\n");
break;
case 5:
printf("Five\n");
break;
case 6:
printf("Six\n");
break;
case 7:
printf("Seven\n");
break;
case 8:
printf("Eight\n");
break;
case 9:
printf("Nine\n");
break;
default:
printf("invalid digit. \nPlease try again ....\n");
break;
}
}