DO NOT MISS

Ads

Friday, 22 July 2016

C-Programs Using else-if ladder

Their is another way of putting it's together when multiple decisions are involved.
A multi path decision is a chain of it's in which the statement associated with each else is an it.
It takes the fallowing general form.

Syntax :-
if(cond 1)
{
statement 1
}
else if(cond 2)
{
statement 2
}
else if(cond 3)
{
statement 3
}

.
.
.
.
.
.
else
{
statement n
}



1.First it checks cond 1.
2.If the cond 1 is true it executes true statement and then goes to next statement.
3.If the cond 1 is false it again checks the cond 2.
4.If the cond 2 is true it executes true statement and then goes to next statement.
5.If it false its again checks the condition.
6.If number of conditions is satisfy finally it executes false (or) else block.


Flow Chart:-

Examples:-

* Write a program to check the given no. +ve (or) -ve (or) 0.


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

void main()

      {

           int a;
           clrscr();

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

           if(a>0)
           printf("+ve");

                else if(a<0)
                printf("-ve");

                else
                printf("zero");

           getch();

     }
Output:-

enter a value:5
+ve
enter a value:-14
-ve
enter a value:0
zero



* Write a program to check whether the given no is big ( or ) small ( or ) equal.

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

void main()

     {

          int a,b;
          clrscr();

          printf("enter two numbers:");
          scanf("%d%d",&a,&b);

         
if(a>b)
          printf("a is big ");

                else if(a<b)
                printf("b is big");

                else
                printf("a,b values are equal");

          getch();

     }
Output:-

enter two numbers:5
3
a is big
enter two numbers:12
26
b is big
enter two numbers:18
18
a,b values are equal

* Write a program to perform arithmetic operations by users given choice.

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

void main()

     {

          int a,b,ch;
          clrscr();

          printf("enter two numbers:");
          scanf("%d%d",&a,&b);
          printf("\nMENU:");
          printf("1.add\n 2.sub\n 3.mul\n 4.div\n 5.mod_div\n 6.exit\n\n ");
          printf("enter your choice:");
          scanf("%d",&ch);

         
if(ch==1)
          printf("add=%d\n",a+b);

               else if(ch==2)
               printf("sub=%d\n",a-b);

               else if(ch==3)
               printf("mul=%d\n",a*b);

               else if(ch==4)
               printf("div=%d\n",a/b);

               else if(ch==5)
               printf("mod_div=%d\n",a%b);

               else
               printf("invalid choice");

          getch();

    }
Output:-

enter two numbers:6
5

MENU:
1.add
2.sub
3.mul
4.div
5.mod_div
6.exit


enter your choice:5
mul:30
enter two numbers:6
5

MENU:
1.add
2.sub
3.mul
4.div
5.mod_div
6.exit


enter your choice:8
invalid choice
___________________________________________________________________________________
___________________________________________________________________________________
_____________________________________________

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

Greataims

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