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 :: armstrong number in c 
C :: random float number in C 
C :: puts without newline c 
C :: to find greatest of 4 numbers in c 
C :: mariadb utf8mb4 
C :: write array of char to file in c 
C :: how to pass an array of structs as an argument in c 
C :: vbnet create and write on file 
C :: matrix multiplication in c 
C :: c pass int by reference 
C :: callback c++ c 
C :: 2 bytes integer c 
C :: c substring 
C :: c recursion func revers number 
C :: mpi example 
C :: lateinit kotlin 
C :: c code to grade marks 
C :: create array of strings in c from user input 
C :: Bitwise Operators in C language 
C :: multiplication of matrix in c 
C :: passing file as argument in c 
C :: to execute a program using C 
C :: mediawiki upload size 
C :: esp32 dhcp server 
C :: c file struct 
C :: static variable c 
C :: what is %d in C 
C :: allintext:christie kiser filetype:log 
C :: cocktail sort in c 
C :: c to assembly online compiler 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =