A sample C programming code for a real time calculator application program is given below. This program will perform the below calculator operations.
- Addition
- Subtraction
- Multiplication
- Division
- Modulus
- Power
- Factorial
In below c programming code example, the user is prompted to choose the operations(i.e. addition, subtraction etc) to be performed and then prompted to key in the values which are used to perform the operations. Then, the result will be shown as output to the user.
Sample C programming code for Calculator Application:
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
// Calculator example using C code #include<stdio.h> #include<conio.h> #include<math.h> #include<stdlib.h> #define KEY "Enter the calculator Operation you want to do:" // Function prototype declaration void addition(); void subtraction(); void multiplication(); void division(); void modulus(); void power(); int factorial(); void calculator_operations(); // Start of Main Program int main() { int X=1; char Calc_oprn; // Function call calculator_operations(); while(X) { printf("\n"); printf("%s : ", KEY); Calc_oprn=getche(); switch(Calc_oprn) { case '+': addition(); break; case '-': subtraction(); break; case '*': multiplication(); break; case '/': division(); break; case '?': modulus(); break; case '!': factorial(); break; case '^': power(); break; case 'H': case 'h': calculator_operations(); break; case 'Q': case 'q': exit(0); break; case 'c': case 'C': system("cls"); calculator_operations(); break; default : system("cls"); printf("\n**********You have entered unavailable option"); printf("***********\n"); printf("\n*****Please Enter any one of below available "); printf("options****\n"); calculator_operations(); } } } //Function Definitions void calculator_operations() { //system("cls"); use system function to clear //screen instead of clrscr(); printf("\n Welcome to C calculator \n\n"); printf("******* Press 'Q' or 'q' to quit "); printf("the program ********\n"); printf("***** Press 'H' or 'h' to display "); printf("below options *****\n\n"); printf("Enter 'C' or 'c' to clear the screen and"); printf(" display available option \n\n"); printf("Enter + symbol for Addition \n"); printf("Enter - symbol for Subtraction \n"); printf("Enter * symbol for Multiplication \n"); printf("Enter / symbol for Division \n"); printf("Enter ? symbol for Modulus\n"); printf("Enter ^ symbol for Power \n"); printf("Enter ! symbol for Factorial \n\n"); } void addition() { int n, total=0, k=0, number; printf("\nEnter the number of elements you want to add:"); scanf("%d",&n); printf("Please enter %d numbers one by one: \n",n); while(k<n) { scanf("%d",&number); total=total+number; k=k+1; } printf("Sum of %d numbers = %d \n",n,total); } void subtraction() { int a, b, c = 0; printf("\nPlease enter first number : "); scanf("%d", &a); printf("Please enter second number : "); scanf("%d", &b); c = a - b; printf("\n%d - %d = %d\n", a, b, c); } void multiplication() { int a, b, mul=0; printf("\nPlease enter first numb : "); scanf("%d", &a); printf("Please enter second number: "); scanf("%d", &b); mul=a*b; printf("\nMultiplication of entered numbers = %d\n",mul); } void division() { int a, b, d=0; printf("\nPlease enter first number : "); scanf("%d", &a); printf("Please enter second number : "); scanf("%d", &b); d=a/b; printf("\nDivision of entered numbers=%d\n",d); } void modulus() { int a, b, d=0; printf("\nPlease enter first number : "); scanf("%d", &a); printf("Please enter second number : "); scanf("%d", &b); d=a%b; printf("\nModulus of entered numbers = %d\n",d); } void power() { double a,num, p; printf("\nEnter two numbers to find the power \n"); printf("number: "); scanf("%lf",&a); printf("power : "); scanf("%lf",&num); p=pow(a,num); printf("\n%lf to the power %lf = %lf \n",a,num,p); } int factorial() { int i,fact=1,num; printf("\nEnter a number to find factorial : "); scanf("%d",&num); if (num<0) { printf("\nPlease enter a positive number to"); printf(" find factorial and try again. \n"); printf("\nFactorial can't be found for negative"); printf(" values. It can be only positive or 0 \n"); return 1; } for(i=1;i<=num;i++) fact=fact*i; printf("\n"); printf("Factorial of entered number %d is:%d\n",num,fact); return 0; } |
Sample calculator Output using C programming code:
Continue on “C code for bank application”….
Continue on “C interview questions and answers”….
OTHER C PROGRAMS:
- C program for prime number
- C program for factorial
- C program for fibonacci series
- C program for palindrome
- C program for swapping 2 numbers with and without temp variables
- C program to find leap year
- C program to find armstrong number
- C program to find simple and compound interest
- C program to find largest of given 3 numbers
- C program to find smallest of given 3 numbers
- C program to convert lower case into upper case and vice versa
- C program to find sum and average of given 3 numbers
- C program to sum up all individual digits
- C program to reverse given number
- C program to reverse given string
- C program to find strong number
- C program to find square and cube of given number
- C program to print hello world without using semi colon
- C program to sort given names in alphabetical order
- C program to copy content of one file to another
- C program to sort given numbers in ascending order
- C program to sort given numbers in descending order
- C program to search given number in an array
- C program for recursive function
- C program for calculator application
- C program for bank application
- C program to check given number is perfect number or not
- C program to find array size
- C program to find whether given character vowel or not
- C program to check whether given number is positive or negative
- C program to find sum of n numbers
- C program to compare 2 numbers without if statement
- C program to generate random numbers
- C program to compare 2 arrays whether they are equal or not
- C program to print number from 1 to 500 without using any loop conditions
- C program to insert an element into an array
- C program to delete an element from an array
- C program to find hcf (gcd) and lcm
- C program to print diamond pattern
- C program to print pascal triangle
- C program to add two complex numbers
- C program for matrix addition
- C program for matrix multiplication
- C program for bubble sort
- C program for insertion sort
- C program for selection sort