C-Conditional Statements

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Conditional Statements

Greatest of four numbers using function:

#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;
}

Leap year or not


#include <stdio.h>
#include<conio.h>
int main()
{
int year;
printf("Enter the year:");
scanf("%d",&year);
if((year%4==0 && year%100!=0) || year%400==0)
printf("It is a perfect leap year");

else
printf("It is not a leap year");
return 0;

Both the numbers are equal or not


#include<stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b);
if(a==b)
printf("Number 1 and number 2 are equal");
else
printf("Number 1 and number 2 are not equal");
return 0;
}
Odd or Even
#include<stdio.h>
int main()
{
int a;
scanf("%d",&a);
if(a%2==0)
printf("The given number is even");
else
printf("The given number is odd");
return 0;
}
Eligible to vote or not:
#include <stdio.h>
void main()
{
int vote_age;
printf("Input the age of the candidate : ");
scanf("%d",&vote_age);
if (vote_age<18)
{
printf("Sorry, You are not eligible to caste your vote.\n");
printf("You would be able to caste your vote after %d year.\n",18-vote_age);
}
else
printf("Congratulation! You are eligible for casting your vote.\n");
return 0;
}
Write a C program to read the value of an integer m and display the value of n is 1 when m is
larger than 0, 0 when m is 0 and -1 when m is less than 0.
#include<stdio.h>
int main()
{
int m,n;
scanf("%d",&m);
if(m>0)
printf("n=1");
else
{
if(m==0)
printf("n=0");
else
printf("n=-1");
}
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);

if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))


{
printf("This is an alphabet.\n");
}
else if(ch>='0' && ch<='9') /* whether it is digit */
{
printf("This is a digit.\n");
}
else /* Else special character */
{
printf("This is a special character.\n");
}
}
To check whether given character is vowel or consonant:
#include <stdio.h>
int main()
{
char ch;
printf("Input a character: ");
scanf("%c", &ch);
if(ch=='a'|| ch=='e'||ch=='i'||ch=='o'||ch=='u')
printf("The given character is a vowel");
else
printf("The given character is a consonant");
return 0;
}
To read a digit and display it in word:
#include <stdio.h>
void main()
{
int cdigit;

printf("Input Digit(0-9) : ");


scanf("%d",&cdigit);
switch(cdigit)
{
case 0:
printf("Zero\n");
break;

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;
}
}

You might also like