Search
 
SCRIPT & CODE EXAMPLE
 

C

how to create calculator with switch in c

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float a,b,s;
    char d;

    printf("choose an operation btwn + - * /  : ");
    scanf("%c",&d);
    switch(d){
    case '+' :
        printf("enter two numbers ");
        scanf("%f%f",&a,&b);
        s = a + b ;
        printf("%.1f + %.1f = %.1f",a,b,s);
        break;
    case '-' :
        printf("enter two numbers ");
        scanf("%f%f",&a,&b);
        s = a - b ;
        printf("%.1f - %.1f = %.1f",a,b,s);
        break;
    case '*' :
         printf("enter two numbers ");
        scanf("%f%f",&a,&b);
        s = a * b ;
        printf("%.1f * %.1f = %f",a,b,s);
        break;
    case '/' :
         printf("enter two numbers ");
        scanf("%f%f",&a,&b);
        s = a / b ;
        printf("%.1f / %.1f = %.1f",a,b,s);
        break;
    default :
        printf("enter a valid operation !");
        break;
    }
    return 0;
}
Comment

simple calculator, using switch statement in C

#include<stdio.h>
#include<conio.h>
void main()
{
	int num1,num2,opt;
	clrscr();
	printf("Enter the first Integer:
");
	scanf("%d",&num1);
	printf("Enter the second Integer:
");
	scanf("%d",&num2);
	for(;;)
	{
		printf("



Enter the your option:
");
		printf("1-Addition.
2-Substraction.
3-Multiplication.
4-Division.
5-Exit.
");
		scanf("%d",&opt);
		switch(opt)
		{
			case 1:printf("
Addition of  %d and %d is: %d",num1,num2,num1+num2);
			break;
			case 2:printf("
Substraction of %d  and %d is:  %d",num1,num2,num1-num2);
			break;
			case 3:printf("
Multiplication of %d  and %d is:  %d",num1,num2,num1*num2);
			break;  
			case 4: 
			if(num2==0)
			{
				printf("OOps Devide by zero
");
			}
			else
			{
				printf("
 Division of %d  and %d is:  %d",num1,num2,num1/num2);
			}  
			break;
			case 5: return 0;
			break; 
			default:printf("
 Enter correct option
");
			break; 
		}
	}
	getch();
}
Comment

PREVIOUS NEXT
Code Example
:: how to print value of pointer in c 
::  
C ::  
C ::  
:: typedef pointer 
:: how to check if a string pointer is empty in c 
C ::  
C ::  
C :: how to scanf two dimensional array in c 
C ::  
C ::  
C ::  
C :: c fopen 
:: fgets remove newline 
C ::  
C :: responsive form bootstrap 4 
C ::  
C :: concatenate two strings without standard library in C 
::  
::  
::  
::  
::  
:: Relational Operator in C language 
:: how to debug a segmentation fault in c 
C :: unused variable in c 
C ::  
C ::  
C ::  
::  
ADD CONTENT
Topic
Content
Source link
Name
6+5 =