This is a simple calculator written in C language and works under DOS Command prompt. The calculator performs all the basic arithmetic operations including addition, subtraction, trigonometric calculations, and many other functions. It can also calculate logarithms, powers, and roots of integer numbers.
To understand this calculator C program, you should have the knowledge of following C programming topics:
Simple Calculator C Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | #include <stdio.h> #include <conio.h> #include <math.h> #include <string.h> void main() { clrscr(); char choice, choice2; int addition=0, sum=0, subtraction=0, sub=0, multiplication=1, multi=1, division=1, dvs=1, square=0; double logx, resultlog, sinx, resultsin, cosx, resultcos, tanx, resulttan, rootx, resultroot, expx, resultexp; double fact, factorial, resultfact=1; printf("Press enter to use the calculator"); getche(); clrscr(); printf("Enter choice \n"); printf("Press \n"); printf("[1] for Addition \n"); printf("[2] for Subtraction \n"); printf("[3] for Multiplication \n"); printf("[4] for Division \n"); printf("[5] for Scientific Functions \n"); printf("[E] for Exit \n"); choice=getch(); if((choice=='e')||(choice=='E')) exit(0); else if(choice=='1') { clrscr(); printf("Add numbers or press q to quit \n"); do { printf("Enter number "); scanf("%d",&addition); sum=addition+sum; } while(getch()!='q'); printf("Result is %d",sum); getche(); } else if(choice=='2') { clrscr(); printf("This program subtract given number from last given number or press q to quit \n"); printf("Enter number "); scanf("%d",&subtraction); sub=subtraction; while(getch()!='q') { printf("Enter number "); scanf("%d",&subtraction); sub=sub-subtraction; } while(getch()!='q'); printf("Result is %d",sub); getch(); } else if(choice=='3') { clrscr(); printf("Multiply numbers or press q to quit "); do { printf("Enter number "); scanf("%d",&multiplication); multi=multiplication*multi; } while(getch()!='q'); printf("Result is %d",multi); getch(); } else if(choice=='4') { clrscr(); printf("This program divide given number from last given number or press q to quit \n"); printf("Enter number "); scanf("%d",&division); dvs=division; while(getch()!='q') { printf("Enter number "); scanf("%d",&division); dvs=subtraction/dvs; } printf("Result is %d",dvs); getch(); } else if(choice=='5') { clrscr(); printf(" Scientific Function \n"); printf("Press \n"); printf("[1] for x square \n"); printf("[2] for root of x \n"); printf("[3] for log x \n"); printf("[4] for exp of x \n"); printf("[5] for Factorial of x \n"); printf("[6] for Sin x \n"); printf("[7] for Cos x \n"); printf("[8] for Tan x \n"); printf("[E] for Exit \n"); choice2=getch(); } if((choice2=='e')||(choice2=='E')) exit(0); else if(choice2=='1') { clrscr(); printf("This program calculate square of x \n"); printf("Enter number "); scanf("%d",&square); square=square*square; printf("Result is %d",square); getch(); } else if(choice2=='2') { clrscr(); printf("This program calculate Square root of x \n"); printf("Enter number "); scanf("%lf",&rootx); resultroot=sqrt(rootx); printf("The square root of %lf is %lfn", rootx, resultroot); getch(); } else if(choice2=='3') { clrscr(); printf("This program calculate Natural log of x \n"); printf("Enter number "); scanf("%lf",&logx); resultlog=log(logx); printf("The natural log of %lf is %lfn", logx, resultlog); getche(); } else if(choice2=='4') { clrscr(); printf("This program calculate e ^ x \n"); printf("Enter number "); scanf("%lf",&expx); resultexp = exp(expx); printf("'e' raised to the power of %lf (e ^ %lf) = %lfn", expx, expx, resultexp); getche(); } else if(choice2=='5') { clrscr(); printf("This program calculate Factorial x \n"); printf("Enter number "); scanf("%lf",&fact); for(int factorial=fact; factorial>=1; factorial--) resultfact=resultfact*factorial; printf("The factorial of %lf is %lf ",fact,resultfact); getch(); } else if(choice2=='6') { clrscr(); printf("This program calculate Sin x \n"); printf("Enter number "); scanf("%lf",&sinx); resultsin=sin(sinx); printf("The sin of %lf is %lfn", sinx, resultsin); getche(); } else if(choice2=='7') { clrscr(); printf("This program calculate Cos x \n"); printf("Enter number "); scanf("%lf",&cosx); resultcos=cos(cosx); printf("The Cos of %lf is %lfn", cosx, resultcos); } else if(choice2=='8') { clrscr(); printf("This program calculate Tan x \n"); printf("Enter number "); scanf("%lf",&tanx); resulttan=tan(tanx); printf("The Tan of %lf is %lfn", cosx, resultcos); } } |