Saturday, 5 March 2022

12. Write a C program to implement calculator using switch case.

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

void main()
{
    int no,no1,no2;
    float n1,n2;
    clrscr();

    printf("1. addition");
    printf("\n2. subtraction");
    printf("\n3. multiplication");
    printf("\n4. divison");
    printf("\n5. exit");


  x:    printf("\n\nenter your choice: ");
        scanf("%d",&no);

        switch(no)
        {
            case 1: printf("\nenter 1st number: ");
                      scanf("%d",&no1);

                      printf("\nenter 2nd number: ");
                      scanf("%d",&no2);

                      printf("\n%d + %d = %d",no1,no2,no1 + no2);
                      break;

            case 2: printf("\nenter 1st number: ");
                      scanf("%d",&no1);

                      printf("\nenter 2nd number: ");
                      scanf("%d",&no2);

                      printf("\n%d - %d = %d",no1,no2,no1 - no2);
                      break;

            case 3: printf("\nenter 1st number: ");
                      scanf("%d",&no1);

                      printf("\nenter 2nd number: ");
                      scanf("%d",&no2);

                      printf("\n%d * %d = %d",no1,no2,no1 * no2);
                      break;

            case 4: printf("\nenter 1st number: ");
                      scanf("%f",&n1);

                      printf("\nenter 2nd number: ");
                      scanf("%f",&n2);

                      printf("\n%f / %f = %f",n1,n2,n1 / n2);
                      break;

            case 5: exit(0);

            default: printf("\nplease enter valid choice");

                        goto x;
    }
     getch();
}

/*  output:

1. addition
2. subtraction                                                                  
3. multiplication                                                              
4. divison                                                                      
5. exit                                                                        
                                                                               
enter your choice: 4                                                            
                                                                               
enter 1st number: 350                                                          
                                                                               
enter 2nd number: 3                                                            

350.000000 / 3.000000 = 116.666667

*/ 

No comments:

Post a Comment

python programs

1. sum of two number