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
C :: How to convert string to int without using library functions in c 
C :: 2 dimensional array in c 
C :: Passing a matrix in a function C 
C :: simple bootstrap form example 
C :: keep last n bits 
C :: how to use malloc in c 
C :: c check if character is an alphabet 
C :: windows forms picturebox change image 
C :: do...while loop c 
C :: second largest element in an array 
C :: c concatenate and allocate string 
C :: c median of an array 
C :: hourglass sum 
C :: pandoc set margins pdf 
C :: Turn on the first n Bits in number 
C :: looping through an array in c 
C :: esp32 dhcp server 
C :: enum case statement in c 
C :: what is the use of malloc in c 
C :: english to russian translation 
C :: c malloc 
C :: pipe system call 
C :: le reste de division in algorithm c 
C :: how to convert c program in to assembly 8051 online 
C :: ask the user if they would like to do something again in C 
C :: ejemplo c holamundo 
C :: deepak 
C :: change data type inline in c 
C :: 50 north main 07522 
C :: find a substring within a string in c and return the index 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =