Tuesday, 12 December 2017

SwitchCase Statement Programs In C

Write a  program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer, calculate percentage and grade according to given conditions using switch case
If percentage >= 90% : Grade A
If percentage >= 80% : Grade B
If percentage >= 70% : Grade C
If percentage >= 60% : Grade D
If percentage >= 40% : Grade E
If percentage < 40% : Fail

#include <stdio.h>
#include<conio.h>
void main()
{
 int phy, chem, bio, math, comp,n;
 float per;
 clrscr();
    printf("Enter five subjects marks: ");
    scanf("%d%d%d%d%d", &phy, &chem, &bio, &math, &comp);

  /* Calculate percentage */
 per = (phy + chem + bio + math + comp) / 5.0;
 printf("Percentage = %f\n", per);

* Find grade according to the percentage */
    if(per >= 90)
    {
        n=1;
    }
    else if(per >= 80)
    {
      n=2;
    }
    else if(per >= 70)
    {
       n=3;
 }
    else if(per >= 60)
    {
      n=4;
    }
     else if(per >= 40)
    {
      n=5;
    }
    else
    {
      n=6;
    }
    switch(n)
    {
        case 1:
        printf("Grade A");
        break;
        case 2:
        printf("Grade B");
        break;
        case 3:
        printf("Grade C");
        break;
        case 4:
        printf("Grade D");
        break;
        case 5:
        printf("Grade E");
        break;
        default:
        printf("Fail");
   }
getch();
}


Write C Program To Create Simple Calculator (all arithmetic operation) using Switch Case


#include <stdio.h>
#include<conio.h>
void main()
{
   char op;
    int firstNumber,secondNumber;
   clrscr();
    printf("Enter an operator (+, -, *,/)");
    scanf("%c", &op);

    printf("Enter two operands: ");
    scanf("%d%d",&firstNumber, &secondNumber);

    switch(op)
    {
        case '+':
            printf("%d",(firstNumber + secondNumber));
            break;
        case '-':
            printf("%d",(firstNumber - secondNumber));
            break;
         case '*':
            printf("%d",(firstNumber * secondNumber));
            break;

        case '/':
            printf("%d",(firstNumber / secondNumber));
            break;

        // operator doesn't match  (+, -, *, /)
        default:
            printf("invalid operator");
 }
getch();
}
To check vowel or consonant using switch case


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

 
To print number of days in a month using switch case 
 
#include <stdio.h>

#include<conio.h>

void main()

{

   int month;

   clrscr();

    /* Input month number from user */

    printf("Enter month number(1-12): ");

    scanf("%d", &month);

 

    switch(month)

    {

        case 1:

            printf("31 days");

            break;

        case 2:

            printf("28/29 days");

            break;

        case 3:

            printf("31 days");

            break;

        case 4:

            printf("30 days");

            break;

        case 5:

            printf("31 days");

            break;

        case 6:

            printf("30 days");

            break;

        case 7:

            printf("31 days");

            break;

        case 8:

            printf("31 days");

            break;

        case 9:

            printf("30 days");

            break;

        case 10:

            printf("31 days");

            break;

        case 11:

            printf("30 days");

            break;

        case 12:

            printf("31 days");

            break;

        default:

            printf("Invalid month! Please enter month number between 1-12");

}

getch();

}

 

 

 
To check Even or Odd number using switch case

#include <stdio.h>

#include<conio.h>

void main()

{

  int num;

 clrscr();

printf("Enter any number to check even or odd: ");

scanf("%d", &num);

switch(num % 2)

    {

        /* If n%2 == 0 */

        case 0:

            printf("Number is Even");

            break;

 

        /* Else if n%2 == 1 */

        case 1:

            printf("Number is Odd");

            break;

    }

  getch();

}

Find maximum between two numbers using switch case
 
 
#include <stdio.h>
#include<conio.h>
void main()
{
   int numb1, numb2;
   clrscr();
  
    printf("Enter two numbers ");
    scanf("%d%d", &numb1, &numb2);
 
 
    switch(numb1 > numb2)
    {   
        /* If condition (numb1>numb2) is false */
        case 0: 
            printf("%d is maximum", numb2);
            break;
 
        /* If condition (numb1>numb2) is true */
        case 1: 
            printf("%d is minimum", numb1);
            break;
        }
   getch();
  }
 
To print day of week using switch case
 
 #include <stdio.h>

#include<conio.h>

void main()

{

   int week;

    clrscr();

  printf("Enter week number(1-7): ");

    scanf("%d", &week);

    switch(week)

    {

        case 1:

            printf("Monday");

            break;

        case 2:

            printf("Tuesday");

            break;

        case 3:

            printf("Wednesday");

            break;

        case 4:

            printf("Thursday");

            break;

        case 5:

            printf("Friday");

            break;

        case 6:

            printf("Saturday");

            break;

        case 7:

            printf("Sunday");

            break;

        default:

            printf("Invalid week! Please enter week number between 1-7.");

          }

getch();

}



1 comment:

  1. Very informative blog!

    Please take some time to visit my blog @
    loop in C notes

    Thanks!

    ReplyDelete

Global Infrastructure and its components in AWS

You may operate your workloads wherever and however you choose with the AWS Global Infrastructure, and you'll use the same network, cont...