Programs with if, if-else, else-if, switch

Program with If(illustration)

#include<stdio.h>
#include<conio.h>
void main()
{
    int a,b;
    printf("enter the value in variables");
    scanf("%d %d",&a,&b);
    if(a >b)
    {
    printf("a is greater");
    }
}

Output:




Program with If-else(find greater among two no.)

#include<stdio.h>
#include<conio.h>
void main()
{
    int a,b;
    printf("enter the value in variables");
    scanf("%d %d",&a,&b);
    if(a >b)
    {
    printf("a is greater");
    }
    else
    {
        printf("a is smaller");
    }
}

Output:


Program with else-if(find the grades like 90>=A, 70>=B, 50>=C, 40>=Pass, 40<=Fail)

#include<stdio.h>
#include<conio.h>
void main()
{
    int mark;
    printf("enter your marks");
    scanf("%d",&mark);
    if(mark >90)
    {
         printf("A grade");
    }
    else if(mark >70)
    {
        printf("B Grade");
    }
    else if(mark >50)
    {
        printf("C Grade");
    }
    else if(mark >=40)
    {
        printf("Pass");
    }
    else if(mark <40)
    {
        printf("Fail");
    }
}

Output:



Program with Else-if statement (creating a program to perform arithmetic operations and average)

#include<stdio.h>
#include<conio.h>
void main()
{
    float a,b;
    int c;
    printf("enter two values");
    scanf("%f %f",&a,&b);
    printf("/n enter 1-add /n 2-sub /n 3-mul /n 4-div /n 5-average");
    scanf("%d",&c);
    if(c==1)
    {
         printf("add = %d ", a+b);
    }
    else if(c==2)
    {
        printf("sub = %d ", a-b);
    }
    else if(c==3)
    {
        printf("mul = %d ", a*b);
    }
    else if(c==4)
    {
        printf("div = %f ", a/b);
    }
    else if(c==5)
    {
        printf("avg = %f ", (a/b)*100);
    }
}

Output:






Program with Else-if(printing good morning,afternoon, night etc)

#include<stdio.h>
#include<conio.h>
void main()
{
    int t;
    printf("enter time ");
    scanf("%d",&t);
    if(t<=5)
    {
         printf("Early Morning");
    }
    else if(t<=12)
    {
        printf("Good Morning");
    }
    else if(t=16)
    {
        printf("Good Afternoon");
    }
    else if(t<=20)
    {
        printf("Good Evening");
    }
    else if(t<=24)
    {
        printf("Good night");
    }
}

Output:








Program with switch statement( print dayname with date)

#include<stdio.h>
#include<conio.h>
void main()
{
int d;
printf("enter the date ");
scanf("%d",&d);
switch (d)
{
case 1:
printf("sunday");
break;
case 2:
printf("monday");
break;
case 3:
printf("tuesday");
break;
case 4:
printf("wednesday");
break;
case 5:
printf("thursday");
break;
case 6:
printf("friday");
break;
case 7:
printf("saturday");
break;
default:
printf("something wrong");
}
getch();
}

Output






















No comments:

Post a Comment