0% found this document useful (0 votes)
500 views

Code For Calculator Application

This C code sample demonstrates a basic calculator application that allows users to perform various mathematical operations like addition, subtraction, multiplication, division, modulus, power, and factorial functions. The user is prompted to select an operation and then enter values, before the result is displayed. Key functions are defined to handle each operation, and a main menu function displays the available options to the user.

Uploaded by

anilperfect
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
500 views

Code For Calculator Application

This C code sample demonstrates a basic calculator application that allows users to perform various mathematical operations like addition, subtraction, multiplication, division, modulus, power, and factorial functions. The user is prompted to select an operation and then enter values, before the result is displayed. Key functions are defined to handle each operation, and a main menu function displays the available options to the user.

Uploaded by

anilperfect
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Code for Calculator Application

A sample C programming code for a real time calculator application program is given
below. This program will perform the below calculator operations.

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Power
7. 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 // Calculator example using C code


2 #include<stdio.h>
3 #include<conio.h>
4 #include<math.h>
5 #include<stdlib.h>
6  
7 #define KEY "Enter the calculator Operation you want to do:"
8  
9 // Function prototype declaration
10 void addition();
11 void subtraction();
12 void multiplication();
13 void division();
14 void modulus();
15 void power();
16 int factorial();
17 void calculator_operations();
18  
19 // Start of Main Program
20 int main()
21 {
22     int X=1;
23     char Calc_oprn;
24  
25     // Function call
26     calculator_operations();
27  
28     while(X)
29     {
30         printf("\n");
31         printf("%s : ", KEY);
32  
33         Calc_oprn=getche();
34  
35         switch(Calc_oprn)
36         {
37             case '+': addition();
38                       break;
39  
40             case '-': subtraction();
41                       break;
42  
43             case '*': multiplication();
44                       break;
45  
46             case '/': division();
47                       break;
48  
49             case '?': modulus();
50                       break;
51  
52             case '!': factorial();
53                       break;
54  
55             case '^': power();
56                       break;
57  
58             case 'H':
59             case 'h': calculator_operations();
60                       break;
61  
62             case 'Q':
63             case 'q': exit(0);
64                       break;
65             case 'c':
66             case 'C': system("cls");
67                       calculator_operations();
68                       break;
69  
70             default : system("cls");
71  
72     printf("\n**********You have entered unavailable option");
73     printf("***********\n");
74     printf("\n*****Please Enter any one of below available ");
75     printf("options****\n");
76                       calculator_operations();
77         }
78     }
79 }
80  
81 //Function Definitions
82  
83 void calculator_operations()
84 {
85     //system("cls");  use system function to clear
86     //screen instead of clrscr();
87     printf("\n             Welcome to C calculator \n\n");
88  
89     printf("******* Press 'Q' or 'q' to quit ");
90     printf("the program ********\n");
91     printf("***** Press 'H' or 'h' to display ");
92     printf("below options *****\n\n");
93     printf("Enter 'C' or 'c' to clear the screen and");
94     printf(" display available option \n\n");
95  
96     printf("Enter + symbol for Addition \n");
97     printf("Enter - symbol for Subtraction \n");
98     printf("Enter * symbol for Multiplication \n");
99     printf("Enter / symbol for Division \n");
100     printf("Enter ? symbol for Modulus\n");
101     printf("Enter ^ symbol for Power \n");
102     printf("Enter ! symbol for Factorial \n\n");
103 }
104  
105 void addition()
106 {
107     int n, total=0, k=0, number;
108     printf("\nEnter the number of elements you want to add:");
109     scanf("%d",&n);
110     printf("Please enter %d numbers one by one: \n",n);
111     while(k<n)
112     {
113         scanf("%d",&number);
114         total=total+number;
115         k=k+1;
116     }
117     printf("Sum of %d numbers = %d \n",n,total);
118 }
119  
120 void subtraction()
121 {
122     int a, b, c = 0;
123     printf("\nPlease enter first number  : ");
124     scanf("%d", &a);
125     printf("Please enter second number : ");
126     scanf("%d", &b);
127     c = a - b;
128     printf("\n%d - %d = %d\n", a, b, c);
129 }
130  
131 void multiplication()
132 {
133     int a, b, mul=0;
134     printf("\nPlease enter first numb   : ");
135     scanf("%d", &a);
136     printf("Please enter second number: ");
137     scanf("%d", &b);
138     mul=a*b;
139     printf("\nMultiplication of entered numbers = %d\n",mul);
140 }
141  
142 void division()
143 {
144     int a, b, d=0;
145     printf("\nPlease enter first number  : ");
146     scanf("%d", &a);
147     printf("Please enter second number : ");
148     scanf("%d", &b);
149     d=a/b;
150     printf("\nDivision of entered numbers=%d\n",d);
151 }
152  
153 void modulus()
154 {
155     int a, b, d=0;
156     printf("\nPlease enter first number   : ");
157     scanf("%d", &a);
158     printf("Please enter second number  : ");
159     scanf("%d", &b);
160     d=a%b;
161     printf("\nModulus of entered numbers = %d\n",d);
162 }
163  
164 void power()
165 {
166     double a,num, p;
167     printf("\nEnter two numbers to find the power \n");
168     printf("number: ");
169     scanf("%lf",&a);
170  
171     printf("power : ");
172     scanf("%lf",&num);
173  
174     p=pow(a,num);
175  
176     printf("\n%lf to the power %lf = %lf \n",a,num,p);
177 }
178  
179 int factorial()
180 {
181     int i,fact=1,num;
182  
183     printf("\nEnter a number to find factorial : ");
184     scanf("%d",&num);
185  
186     if (num<0)
187     {
188         printf("\nPlease enter a positive number to");
189         printf(" find factorial and try again. \n");
190         printf("\nFactorial can't be found for negative");
191         printf(" values. It can be only positive or 0  \n");
192         return 1;
193     }              
194  
195     for(i=1;i<=num;i++)
196     fact=fact*i;
197     printf("\n");
198     printf("Factorial of entered number %d is:%d\n",num,fact);
199     return 0;
200 }

You might also like