knowledgediarybd.com

Your Virtual Knowledge Campus

Example of Some IF Else C programs for Beginners

Example of Some IF Else C programs for Beginners

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

#A program to calculate Grade Point Average(GPA) of a student

#include<stdio.h>
#include<conio.h>
#include<string.h>
float grade();
int main()
{
int x[20],i,n,roll,a;
char name[20];
float avg,d[50],total=0;
//clrscr();
printf("Name=");
gets(name);
printf("Roll:");
scanf("%d",&roll);
printf("how many subject=");
scanf("%d",&n);
for(i=0;i<n;i++)
{printf("mark in sub no %d=",i+1);
scanf("%d",&x[i]);
printf("Grade & GP in sub no %d=",i+1);
if(x[i]>=80) {
printf("A+ = 4");
d[i]=4; }
else if(x[i]>=75)
{ printf("A =3.75");
d[i]=3.75;
}
else if(x[i]>=70)
{
printf("A- =3.5");
d[i]=3.50;
}
else if(x[i]>=65)
{
printf("B+ =3.25");
d[i]=3.25; }
else if(x[i]>=60)
{
printf("B =3.00");
d[i]=3; }
else if(x[i]>=55)
{
printf("B- =2.75");
d[i]=2.75; }
else if(x[i]>=50)
{
printf("C+ =2.50");
d[i]=2.50; }
else if(x[i]>=45)
{
printf("C =2.25");
d[i]=2.25; }
else if(x[i]>=40)
{
printf("D =2.00");
d[i]=2.00; }
else
{
printf("F =0");
d[i]=0; }
printf("\n\n"); }
a=1;
for(i=0;i<n;i++)
{
if(d[i]==0)
{
printf("AVG GP= 0");
a=0;
break; }}
if(a==1)
{for(i=0;i<n;i++)
total=total+d[i];
avg=(float)total/n;
printf("AVG GP=%.2f",avg);
}
getch();
return 0;
}

Related Post Or You may felt Interest:

knowledgediarybd.com © 2017
shares