Programs Using If...Else


Program to check weather given number is positive, negative or zero.
#include<stdio.h>
#include<conio.h>
void main()
{
    int n;
    clrscr();
    printf("Enter Value of n:");
    scanf("%d",&n);
    if(n < 0)
    printf("%d is Negative",n);
    else if (n > 0)
    printf("%d is Positive",n);
    else
    printf("%d is zero",n);
    getch();
}

Output

Enter Value of n:
-3
-3 is Negative
Program to find weather given number is ODD or EVEN.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main ()

    int n;
    clrscr (); 
    printf("Enter value of n"); 
    scanf("%d", &n); 
    if (n % 2 == 0) 
    printf ("%d is even",n);
    else
    printf ("%d is odd",n);
    getch ();
}

Output

Enter value of n
3
3 is odd
     
Program to find maximum from two numbers.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main ()

    int a,b;
    clrscr();
    printf("Enter value of a:");
    scanf("%d",&a);
    printf("Enter value of b:");
    scanf("%d",&b);
    if (a > b)
    printf("%d is maximum",a);
    else
    printf("%d is maximum",b);
    getch ();
}

Output

Enter value of a:
12
Enter value of b:
15
15 is maximum
Program to find maximum from three numbers.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main ()

    int a,b,c;
    clrscr();
    printf("Enter value of a:");
    scanf("%d",&a);
    printf("Enter value of b:");
    scanf("%d",&b);
    printf("Enter value of c:");
    scanf("%d",&c);
    if ((a > b) && (a>c))
    printf("%d is maximum",a);
    else if ((b>c) && (b>a))
    printf("%d is maximum",b);
    else
    printf("%d is maximum",c);
    getch ();
}

Output

Enter value of a:
12
Enter value of b:
15
Enter value of c:
22
22 is maximum
Program to read number until you entered positive number.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main ()

    int a;
    clrscr();
    read:
    printf("Enter value of a:");
    scanf("%d",&a);
    if (a < 0)
    goto read;
    else
    printf("value of a is %d",a);
    getch ();
}

Output

Enter value of a
-2
Enter value of a
-4
Enter value of a
2
value of a is 2
Program to find roots of quadratic equation.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main ()

    float a,b,c,x1,x2,d;
    clrscr();
    printf("Enter Value for the co-efficients\n");
    scanf("%f%f%f",&a,&b,&c);
    d=b*b-4*a*c;
    printf("Delta=%f\n",d);
    if(d<0)
    {
        printf("Roots are imaginary\n");
    }
    else if(d>0)
    {
        x1=(-b+sqrt(d))/(2*a);
        x2=(-b-sqrt(d))/(2*a);
        printf("The roots are distinct\n");
        printf("X1=%f\nX2=%f\n",x1,x2);
    }
    else
    {
        x1=x2=-b/(2*a);
        printf("The roots are equal\n");
        printf("x1=%f\nx2=%f\n",x1,x2);
    }
    getch();
}

Output

Enter Value for the co-efficients:
1
4
1
delta=12.000000
X1=-0.267949
X2=-3.732051
Program to calculate electricity bill.
<120 unit 4Rs/Unit
>120 and <=320 4.5 Rs/Unit
>320 and <=420 5.0 Rs/Unit
>420 6.0 Rs/Unit
#include <stdio.h>
#include <conio.h> 
void main ()
{
    int unit, total;
    clrscr ();
    printf("Enter Total Units:");
    scanf ("%d", &unit);
    if (unit<120) 
    total=unit*4;
    else if (unit>120 && unit<=320)
    total=unit*4.5; 
    else if (unit >320 && unit<=420)
    total=unit*5;
    else
    total=unit*6;
    printf("Total: %d", total);
    getch ();
}


Output

Enter Total Units
100
Total:400
Program to make simple calculator using swich case.
#include <stdio.h>
#include <conio.h> 
void main ()
{
    float a,b,c;
    int ch;
    clrscr();
    printf("(1) ADD\n(2)SUB\n(3)MUL\n(4)DIV\n");
    printf("Enter Value of A:");
    scanf("%f",&a);
    printf("Enter Value of B:");
    scanf("%f",&b);
    printf("Enter Your Choice:");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1:
        printf("Addition=%f",a+b);
        break;
        case 2:
        printf("Subtraction=%f",a-b);
        break; 
        case 3:
        printf("Multiplication=%f",a*b);
        break;
        case 4:
        printf("Division=%f",a/b);
        break;
        default:
        printf("Wrong Choice");
        break;
     }
getch();
}

Output

(1) Add
(2) Sub
(3) Mul
(4) Div
Enter Value of A: 2
Enter Value of B: 3
Enter Your Choice:1
Addition:5
Program to find entered year is leap year or not.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main ()

    int year;
    clrscr ();
    printf("Enter year");
    scanf("%d",&year);
    if (year % 4 == 0)
    printf("%d is a leap year",year);
    else
    printf("%d is not a leap year,year");
    getch();
}

Output

Enter Year
2012
2012 is a leap year

Download Projects


Download Programs