DO NOT MISS

Ads

Thursday 21 July 2016

C-Programs Using if-else Statement


The if-else statement is an extension of simple-if statement. The general form is

Syntax:-

if(condition)
{
True statement
}
else
{
False Statement
}


First it checks the condition. 
If the condition is true it executes true statement and then goes to next statement. 

If the condition is false it executes false statement and then goes to next statement.



Flow Char:-
Examples:-



* write a program to check whether the given age is eligible for vote or not.



#include<stdio.h>
#include<conio.h>

void main()

       {

             int age;
             clrscr();

             printf(“enter your age:”);
             scanf(“%d”,&age);


             If(age>18)
             printf(“you are eligible for vote”);
             else
             printf(“you are not eligible for vote”);

             getch();

      }


Output:-

enter your age:24
you are eligible for vote
enter your age:13
you are not eligible for vote



* write a program to check whether the given age is teenager or not.



#include<stdio.h>
#include<conio.h>

void main()

      {

          int age;
          clrscr();

          printf(“enter your age:”);
          scanf(“%d”,&age);


          If(age>=13&&age<=19)

          printf(“you are teenager”);
         
else
          printf(“you are not teenager”);


          getch();

     }

Output:-

enter your age:17
you are teenager
enter your age:23
you are not teenager

* write a program input two numbers & print which is big value.

#include<stdio.h>
#include<conio.h>

void main()

     {

          int a,b;
          clrscr();

          printf(“enter any two numbers:”);
          scanf(“%d%d”,&a,&b);


          if(a>b)

          printf(“big value=%d”,a);
         
else
          printf(“big value=%d”,b);

          getch();

    }
Output:-

enter any two numbers:21
14
Big value=21

* write a program input marks in 3 subjects & print pass/fail.

#include<stdio.h>
#include<conio.h>

void main()

     {

          int m1,m2,m3;
          clrscr();

          printf(“enter three subject marks:”);
          scanf(“%d%d%d”,&m1,&m2,&m3);

          if(m1>=35&&m2>=35&&m3>=35)

          printf(“PASS”);
         
else
          printf(“FAIL”);


          getch();

    }
Output:-

enter three subject marks:85
65

47

PASS
enter three subject marks:96
32

78
FAIL

* write a program another logic in above program.

#include<stdio.h>
#include<conio.h>

void main()

     {

         int m1,m2,m3;
         clrscr();

         printf(“enter three subject marks:”);
         scanf(“%d%d%d”,&m1,&m2,&m3);


         if(m1<35||m2<35||m3<35)

         printf(“FAIL”);
         
else          

         printf(“PASS”);

         getch();

   }
Output:-

enter three subject marks:55
74
28
FAIL
enter three subject marks:74
66

44 PASS

* write a program input 2 values & print equal or not equal.

#include<stdio.h>
#include<conio.h>

void main()

     {

           int a,b;
           clrscr();

           printf(“enter any two numbers:”);
           scanf(“%d%d”,&a,&b);


           if(a==b)

           printf(“a is equal to b”);
         
 else
           printf(“a is not equal to b”);

           getch();

    }
Output:-

enter any two numbers:8
8
a is equal to b
enter any two numbers:12
8
a is not equal to b

* write a program to input 2 values using logical AND operator.

#include<stdio.h>
#include<conio.h>

void main()

    {

         int a,b;
         clrscr();

         printf(“enter any two numbers:”);
         scanf(“%d%d”,&a,&b);

         if(a&&b)

         printf(“Condition is True”);
       
 else
         printf(“Condition if Not True”);

         getch();

    }
Output:-

enter any two numbers:8
14
Condition is true
enter any two numbers:16
0
Condition is not true

* write a program to input 2 values using logical OR operator.


#include<stdio.h>
#include<conio.h>

void main()

     {

         int a,b;
         clrscr();

         printf(“enter any two numbers:”);
         scanf(“%d%d”,&a,&b);


         if(a||b)

         printf(“Condition is True”);
       
 else
         printf(“Condition if Not True”);

         getch();

    }
Output:-

enter any two numbers:0
17
Condition is true
enter any two numbers:0
0
Condition is not true




* write a program to input 2 values using logical AND & NOT operators.



#include<stdio.h>
#include<conio.h>

void main()

    {

         int a,b;
         clrscr();

         printf(“enter any two numbers:”);
         scanf(“%d%d”,&a,&b);

         if(a&&b)

         printf(“Condition is True\n”);
         else
         printf(“Condition if Not True\n”);

         if(!(a&&b))

         printf(“Condition is True\n”);
         
else
         printf(“Condition if Not True\n”);

         getch();

   }


Output:-

enter any two numbers:11
17
Condition is true
Condition if Not True
enter any two numbers:13
0
Condition is not true
Condition if True

* write a program to check whether the given no. is even or odd.

#include<stdio.h>
#include<conio.h>

void main()

     {

          int a;
          clrscr();

          printf("enter a number:");
          scanf("%d",&a);


          if(a%2==0)

          printf("the number is even=%d\n",a);
         
else
          printf("the number is odd=%d\n",a);

          getch();

     }
Output:-

enter a number:16
the number is even=
16
enter a number:13
the number is odd=
13

* write a program to check whether the given year is leap year or not.

#include<stdio.h>
#include<conio.h>

void main()

     {

         int a;
         clrscr();

         printf("enter a year:");
         scanf("%d",&a);  


         if(a%4==0)

         printf("the year is a leap year:=%d\n",a);
         
else          

         printf("the year is not leap year:=%d\n",a);

         getch();

    }
Output:-

enter a year:2016
the year is a leap year=2016
enter a year:2014
the year is a not leap year=2014
___________________________________________________________________________________
>> Back to Conditional Control Statements
___________________________________________________________________________________
>> Back to Control Statements
_____________________________________________
>> Back to C-Language Notes (Topic Wise)
-------------------------------------------------------------------------------------------------------------------------------------------------

Greataims

 
Copyright © 2016 Computersadda. Designed by @ Computersadda Team - Published By Greataims