Friday, 2 November 2012

Chapter 3-Switch case

SWITCH CASE:
decisions using switch case

#include <stdio.h>
int main ( )
{
    int marks,status;
    printf("\nEnter marks = ");
    scanf("%d",&marks);
   
    if (marks > 79 && marks < 100)
    printf("\nA,Pass\n");
    else if(marks > 60 && marks < 79)
    printf("\nB,Pass\n");
    else if(marks > 40 && marks < 59)
    printf("\nC,Pass\n");
    else
    printf("\nF,Fail\n");
    scanf("%d",&marks);
return 0;
}


example of switch case..:

void main(void)

{

int i = 2 ;


switch ( i )

{

case 1 :

printf ( "I am in case 1 \n" ) ;

break ;

case 2 :

printf ( "I am in case 2 \n" ) ;

break ;

case 3 :

printf ( "I am in case 3 \n" ) ;

break ;

default :

printf ( "I am in default \n" ) ;

}

}

and the flowchart:


and this is my example:

#include<stdio.h>
int main(void)
{
    int select,area,w,h;
    printf("**********program to calculate area rectangle********\n");
    printf("*              please select an option                                      *\n");
    printf("*                                                                                        *\n");
    printf("*                  1.rectangle                                                     *\n");
    printf("*                   2.exit                                                             *\n");
    printf("**********************************************\n");
   
    scanf("\n%d,",&select);
   
       switch(select)
       {
                     case 1:
                     printf("program to calculate rectangle\n");
                     //printf("calculate the area of rectangle");
                     printf("key in the width for calculation = ");
                     scanf("\n%d",&w);
                     printf("key in the height for calculation = ");
                     scanf("\n%d",&h);
                     area=w*h;
                     goto answer;
                     break;
                     default:
                     printf("wrong case selelcted!!!");
                     goto exit;
       }
       answer:printf("the area is = %d\n",area);
       exit:;
       scanf("\n%d,",&select);
       }

No comments:

Post a Comment