knowledgediarybd.com

Your Virtual Knowledge Campus

Example of Some IF else if and Else C programs

#A program to find smaller/greater number from a reference number

#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{

int a;
printf("Enter an integer number\n");
scanf("%d",&a);
if(a>20)
printf("Your value is greater than 20");
else
printf("Your value is smaller than 20");
getch();
return 0;
}

#A program to find the solution of the quadratic equation Like: Ax^2+Bx+C. 

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

int main()
{
float a,b,c,dis,x1,x2;
printf("Enter the value of coefficients\n");
scanf("%f %f %f",&a,&b,&c);
dis=b*b-4*a*c;
if(dis<0)
printf("\nimaginary");
else
{
x1=(-b+sqrt(dis))/(2*a);
x2=(-b-sqrt(dis))/(2*a);
printf("x1=%f and x2=%f",x1,x2);}

return 0;
}

Sample Input:

Enter the value of coefficients
5
10
2

Sample Output:

x1=-0.225403

x2=-1.774597

 # A program to select and print the largest of three numbers. 

#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{

int a,b,c;
printf("Input the three numbers respectively\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("The Largest number is %d",a);
else if(b>a && b>c)
printf("The Largest number is %d",b);
else
printf("The Largest number is %d",c);
getch();
return 0;
}

Sample Input:

Input the three numbers respectively

7 3 9

Sample Output:

The Largest number is 9

# A program to select and print the lowest of three numbers. 

#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{

int a,b,c;
printf("Input the three numbers respectively:\n");
scanf("%d%d%d",&a,&b,&c);
if(a<b && a<c)
printf("The Lowest number is %d",a);
else if(b<a && b<c)
printf("The Lowest number is %d",b);
else
printf("The Lowest number is %d",c);
getch();
return 0;
}

Sample Input:

Input the three numbers respectively

7 3 9

Sample Output:

The Largest number is 3

# A program to determine whether a number is ‘odd’ or ‘even’ and print the message

NUMBER IS ODD OR NUMBER IS EVEN

Without using else option 

#include<stdio.h>
int main()
{

int i, a;
printf("Enter your number:");
scanf("%d",&a);
if(a%2==0)
printf("your number is even");
if(a%2==1)
printf("your number is odd");

return 0;
}

# A program to determine whether a number is ‘odd’ or ‘even’ and print the message

NUMBER IS ODD OR NUMBER IS EVEN

Using Both if and else option 

#include<stdio.h>
int main()
{

int i, a;
printf("Enter your number:");
scanf("%d",&a);
if(a%2==0)
printf("your number is even");
else
printf("your number is odd");

return 0;
}
knowledgediarybd.com © 2017
shares