Sunday 26 June 2016

SWITCH CASE programming

1. WAP in c to find addition,subtraction, multiplication and division of two numbers using switch.

#include<stdio.h>
#include<conio.h>
void main()
{
int x;
float a,b,c;
clrscr();
printf("\n1. Add");
printf("\n2. Subtract");
printf("\n3. Multiply");
printf("\n4. Divide");
printf("\nEnter your choice:");
scanf("%d",&x);
printf("\nEnter two numbers:\n");
scanf("%f%f",&a,&b);
switch(x)
{
case 1:c=a+b;
break;
case 2:c=a-b;
break;
case 3:c=a*b;
break;
case 4:c=a/b;
break;
default:
printf("\nwrong choice");
}
printf("result=%f",c);
getch();
}

Output:







2. WAP in c to find month name if month number is given.

#include<stdio.h>
#include<conio.h>
void main()
{
int ch;
float a,b,c;
clrscr();
printf("\nEnter any month number");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nJanuary");
break;
case 2:printf("\nFebruary");
break;
case 3:printf("\nMarch");
break;
case 4:printf("\nApril");
break;
case 5:printf("\nMay");
break;
case 6:printf("\nJune");
break;
case 7:printf("\nJuly");
break;
case 8:printf("\nAugust");
break;
case 9:printf("\nSeptember");
break;
case 10:printf("\nOctober");
break;
case 11:printf("\nNovember");
break;
case 12:printf("\nDecember");
break;
default:printf("\nwrong choice");
}
getch();
}

Output:






3. WAP in c to find number of days in a month if month number is given using switch (assume feb has 28 days).

#include<stdio.h>
#include<conio.h>
void main()
{
int ch;
clrscr();
printf("\nEnter any month number");
scanf("%d",&ch);
switch(ch)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf("\n31 days");
break;
case 4:
case 6:
case 9:
case 11:printf("\n30 days");
break;
case 2:printf("\n28 days");
break;
default:printf("\nwrong choice");
}
getch();
}

Output:





4. WAP in c to check whether a given character is vowel or consonant.

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("\nEnter any character:");
scanf("%c",&ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':printf("\nvowel");
break;
default:printf("\nconsonent");
}
getch();
}

Output:

No comments:

Post a Comment

index

MENU Sample Papers  (new sample paper added for computers) C Language Mathematics IX (NCERT solution)   Computer IX (CBSE) ...