C program to create a simple Calculator using Function.

Question:

Write a Program in C to create a simple calculator to perform addition, subtraction, multiplication division and modulus.

Source Code:

#include<stdio.h> int add(int a, int b) { return a+b; } int sub(int a, int b) { return a-b; } int mul(int a, int b) { return a*b; } int divi(int a, int b) { return (a/b); } int mod(int a, int b) { return a%b; } int main() { int num1,num2,choice; char c; hello: printf("*Calculator*\n"); printf("Enter Choice\n"); printf("1. Addition \n 2. Subtraction \n 3. Multiplication \n 4. Division \n 5. Modulus \n "); scanf("%d",&choice); switch(choice) { case 1: printf("Enter two numbers;\n"); scanf("%d \n %d",&num1,&num2); printf("Addition:%d",add(num1,num2)); break; case 2: printf("Enter two numbers;\n"); scanf("%d \n %d",&num1,&num2); printf("Subtraction:%d",sub(num1,num2)); break; case 3: printf("Enter two numbers;\n"); scanf("%d \n %d",&num1,&num2); printf("Multiplication:%d",mul(num1,num2)); break; case 4: printf("Enter two numbers;\n"); scanf("%d \n %d",&num1,&num2); printf("Division:%d",divi(num1,num2)); break; case 5: printf("Enter two numbers;\n"); scanf("%d \n %d",&num1,&num2); printf("Modulus:%d",mod(num1,num2)); break; default: printf("Invalid Option"); } printf("Do you want to continue\n press Y for yess and N for no \n"); scanf("%c",&c); if (c=='Y' || c=='y') { goto hello; } return 0; }

Output:

*Calculator* Enter Choice 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Modulus 1 Enter two numbers 4 5 Addition: 9 Do you want to continue press Y for yess and N for no n

Comments

Popular posts from this blog

Introduction to Python.

Decision Making Statement.