DATA STRUCTURES
C Programming
LAB 1
CALCULATOR
Algorithm of Calculator Program
Step 1: Declare local variables n1, n2, res, opt. For example, where n1 and n2 take two numeric
values, res will store results and opt variable define the operator symbols.
Step 2: Print the Choice (Addition, Subtraction, multiplication, division, etc.
Step 3: Enter the Choice
Step 4: Takes two numbers, n1 and n2
Step 5: Switch case jump to an operator selected by the user
Step 6: Store result into res variable.
Step 7: Display the operation result
Step 8: Exit from the program.
Different ways to create a Calculator Program in C
Following are the different ways to write a Calculator Program in the C language.
1. Calculator Program in C using the switch statement
2. Calculator Program in C using if else if statement
3. Calculator Program in C using do-while loop and switch statement
4. Calculator Program in C using function and switch statement
Example 1: Calculator Program in C using the switch statement
Let's write a program to create a Calculator program using switch statement
program.c
1. #include <stdio.h>
2. int main()
3. {
4. // declare local variables
5. char opt;
6. int n1, n2;
7. float res;
8. printf (" Choose an operator(+, -, *, /) to perform the operation in C Calculator \n ");
9. scanf ("%c", &opt); // take an operator
10. if (opt == '/' )
11. {
12. printf (" You have selected: Division");
13. }
14. else if (opt == '*')
15. {
16. printf (" You have selected: Multiplication");
17. }
18.
19. else if (opt == '-')
20. {
21. printf (" You have selected: Subtraction");
22. }
23. else if (opt == '+')
24. {
25. printf (" You have selected: Addition");
26. }
27. printf (" \n Enter the first number: ");
28. scanf(" %d", &n1); // take fist number
29. printf (" Enter the second number: ");
30. scanf (" %d", &n2); // take second number
31.
32. switch(opt)
33. {
34. case '+':
35. res = n1 + n2; // add two numbers
36. printf (" Addition of %d and %d is: %.2f", n1, n2, res);
37. break;
38.
39. case '-':
40. res = n1 - n2; // subtract two numbers
41. printf (" Subtraction of %d and %d is: %.2f", n1, n2, res);
42. break;
43.
44. case '*':
45. res = n1 * n2; // multiply two numbers
46. printf (" Multiplication of %d and %d is: %.2f", n1, n2, res);
47. break;
48.
49. case '/':
50. if (n2 == 0) // if n2 == 0, take another number
51. {
52. printf (" \n Divisor cannot be zero. Please enter another value ");
53. scanf ("%d", &n2);
54. }
55. res = n1 / n2; // divide two numbers
56. printf (" Division of %d and %d is: %.2f", n1, n2, res);
57. break;
58. default: /* use default to print default message if any condition is not satisfied */
59. printf (" Something is wrong!! Please check the options ");
60. }
61. return 0;
62. }
Output:
Example 2: Calculator Program in C using if else if statement
Let's consider an example to write a simple Calculator program in C using if else if statement.
program2.c
1. #include <stdio.h>
2. int main()
3. {
4. // declare local variables
5. char opt;
6. int n1, n2;
7. float res;
8. printf (" Select an operator (+, -, *, /) to perform an operation in C calculator \n ");
9. scanf ("%c", &opt); // take an operator
10. printf (" Enter the first number: ");
11. scanf(" %d", &n1); // take fist number
12. printf (" Enter the second number: ");
13. scanf (" %d", &n2); // take second number
14.
15. if (opt == '+')
16. {
17. res = n1 + n2; // add two numbers
18. printf (" Addition of %d and %d is: %f", n1, n2, res);
19. }
20.
21. else if (opt == '-')
22. {
23. res = n1 - n2; // subtract two numbers
24. printf (" Subtraction of %d and %d is: %f", n1, n2, res);
25. }
26.
27. else if (opt == '*')
28. {
29. res = n1 * n2; // multiply two numbers
30. printf (" Multiplication of %d and %d is: %f", n1, n2, res);
31. }
32.
33. else if (opt == '/')
34. {
35. if (n2 == 0) // if n2 == 0, take another number
36. {
37. printf (" \n Divisor cannot be zero. Please enter another value ");
38. scanf ("%d", &n2);
39. }
40. res = n1 / n2; // divide two numbers
41. printf (" Division of %d and %d is: %.2f", n1, n2, res);
42. }
43. else
44. {
45. printf(" \n You have entered wrong inputs ");
46. }
47. return 0;
48. }
Output:
Example 3: Calculator Program in C using do while loop and switch statement
Let's create a Calculator program using do while loop and switch case statement in C
program3.c
1. #include <stdio.h>
2. #include <math.h>
3. #include <stdlib.h>
4.
5. int main()
6. {
7. // declaration of local variable op;
8. int op, n1, n2;
9. float res;
10. char ch;
11. do
12. {
13. // displays the multiple operations of the C Calculator
14. printf (" Select an operation to perform the calculation in C Calculator: ");
15. printf (" \n 1 Addition \t \t 2 Subtraction \n 3 Multiplication \t 4 Division \n 5 Square \t \
t 6 Square Root \n 7 Exit \n \n Please, Make a choice ");
16.
17. scanf ("%d", &op); // accepts a numeric input to choose the operation
18.
19.
20. // use switch statement to call an operation
21. switch (op)
22. {
23. case 1:
24. // Add two numbers
25. printf (" You chose: Addition");
26. printf ("\n Enter First Number: ");
27. scanf (" %d", &n1);
28. printf (" Enter Second Number: ");
29. scanf (" %d", &n2);
30. res = n1 + n2; // Add two numbers
31. printf (" Addition of two numbers is: %.2f", res);
32. break; // break the function
33.
34. case 2:
35. // Subtract two numbers
36. printf (" You chose: Subtraction");
37. printf ("\n Enter First Number: ");
38. scanf (" %d", &n1);
39. printf (" Enter Second Number: ");
40. scanf (" %d", &n2);
41. res = n1 - n2; // subtract two numbers
42. printf (" Subtraction of two numbers is: %.2f", res);
43. break; // break the function
44.
45. case 3:
46. // Multiplication of the numbers
47. printf (" You chose: Multiplication");
48. printf ("\n Enter First Number: ");
49. scanf (" %d", &n1);
50. printf (" Enter Second Number: ");
51. scanf (" %d", &n2);
52. res = n1 * n2; // multiply two numbers
53. printf (" Multiplication of two numbers is: %.2f", res);
54. break; // break the function
55.
56. case 4:
57. // Division of the numbers
58. printf (" You chose: Division");
59. printf ("\n Enter First Number: ");
60. scanf (" %d", &n1);
61. printf (" Enter Second Number: ");
62. scanf (" %d", &n2);
63. if (n2 == 0)
64. {
65. printf (" \n Divisor cannot be zero. Please enter another value ");
66. scanf ("%d", &n2);
67. }
68. res = n1 / n2; // divide two numbers
69. printf (" Division of two numbers is: %.2f", res);
70. break; // break the function
71.
72. case 5:
73. // getting square of a number
74. printf (" You chose: Square");
75. printf ("\n Enter First Number: ");
76. scanf (" %d", &n1);
77.
78. res = n1 * n1; // get square of a number
79. printf (" Square of %d number is: %.2f", n1, res);
80. break; // break the function
81.
82. case 6:
83. // getting the square root of the number
84. printf (" You chose: Square Root");
85. printf ("\n Enter First Number: ");
86. scanf (" %d", &n1);
87.
88. res = sqrt(n1); // use sqrt() function to find the Square Root
89. printf (" Square Root of %d numbers is: %.2f", n1, res);
90. break; // break the function
91.
92. case 7:
93. printf (" You chose: Exit");
94. exit(0);
95. break; // break the function
96.
97. default:
98. printf(" Something is wrong!! ");
99. break;
100. }
101. printf (" \n \n ********************************************** \n ");
102. } while (op != 7);
103.
104. return 0;
105. }
Output:
Example 4: Calculator Program in C using function and switch statement
Let's create a Calculator program using function and switch case statement in C
program4.c
1. #include <stdio.h>
2. #include <conio.h>
3. #include <math.h>
4. #include <stdlib.h>
5.
6. // function declarations
7. int addition();
8. int subtract();
9. int multiply();
10. int divide();
11. int sq();
12. int sqrt1();
13. void exit();
14.
15. int main()
16. {
17. // declaration a local variable op;
18. int op;
19. do
20. {
21. // displays the multiple operations of the C Calculator
22. printf (" Select an operation to perform the calculation in C Calculator: ");
23. printf (" \n 1 Addition \t \t 2 Subtraction \n 3 Multiplication \t 4 Division \n 5 Square \t \
t 6 Square Root \n 7 Exit \n \n Please, Make a choice ");
24.
25. scanf ("%d", &op); // accepts a numeric input to choose the operation
26.
27.
28. // use switch statement to call an operation
29. switch (op)
30. {
31. case 1:
32. addition(); /* It call the addition() function to add the given numbers */
33. break; // break the function
34.
35. case 2:
36. subtract(); /* It call the subtract() function to subtract the given numbers */
37. break; // break the function
38.
39. case 3:
40. multiply(); /* It call the multiply() function to multiply the given numbers */
41. break; // break the function
42.
43. case 4:
44. divide(); // It call the divide() function to divide the given numbers
45. break; // break the function
46.
47. case 5:
48. sq(); // It call the sq() function to get the square of given numbers
49. break; // break the function
50.
51. case 6:
52. sqrt1(); /* It call the sqrt1() function to get the square root of given numbers */
53. break; // break the function
54.
55. case 7:
56. exit(0); // It call the exit() function to exit from the program
57. break; // break the function
58.
59. default:
60. printf(" Something is wrong!! ");
61. break;
62. }
63. printf (" \n \n ********************************************** \n ");
64. } while (op != 7);
65.
66.
67. return 0;
68. }
69.
70.
71.
72. // function definition
73. int addition()
74. {
75. int i, sum = 0, num, f_num; // declare a local variable
76. printf (" How many numbers you want to add: ");
77. scanf ("%d", &num);
78. printf (" Enter the numbers: \n ");
79. for (i = 1; i <= num; i++)
80. {
81. scanf(" %d", &f_num);
82. sum = sum + f_num;
83. }
84. printf (" Total Sum of the numbers = %d", sum);
85. return 0;
86. }
87.
88. // use subtract() function to subtract two numbers
89. int subtract()
90. {
91. int n1, n2, res;
92. printf (" The first number is: ");
93. scanf (" %d", &n1);
94. printf (" The second number is: ");
95. scanf (" %d", &n2);
96. res = n1 - n2;
97. printf (" The subtraction of %d - %d is: %d", n1, n2, res);
98. }
99.
100. // use multiply() function to multiply two numbers
101. int multiply()
102. {
103. int n1, n2, res;
104. printf (" The first number is: ");
105. scanf (" %d", &n1);
106. printf (" The second number is: ");
107. scanf (" %d", &n2);
108. res = n1 * n2;
109. printf (" The multiply of %d * %d is: %d", n1, n2, res);
110. }
111.
112. // use divide() function to divide two numbers
113. int divide()
114. {
115. int n1, n2, res;
116. printf (" The first number is: ");
117. scanf (" %d", &n1);
118. printf (" The second number is: ");
119. scanf (" %d", &n2);
120.
121. if (n2 == 0)
122. {
123. printf (" \n Divisor cannot be zero. Please enter another value ");
124. scanf ("%d", &n2);
125. }
126. res = n1 / n2;
127. printf (" \n The division of %d / %d is: %d", n1, n2, res);
128. }
129.
130. // use sq() function to get the square of the given number
131. int sq()
132. {
133. int n1, res;
134. printf (" Enter a number to get the Square: ");
135. scanf (" %d", &n1);
136.
137. res = n1 * n1;
138. printf (" \n The Square of %d is: %d", n1, res);
139. }
140.
141. // use sqrt1() function to get the square root of the given number
142. int sqrt1()
143. {
144. float res;
145. int n1;
146. printf (" Enter a number to get the Square Root: ");
147. scanf (" %d", &n1);
148.
149. res = sqrt(n1);
150. printf (" \n The Square Root of %d is: %f", n1, res);
151. }
Output:
******
LAB 2
Linear DS
Arrays:
C Program:
Linked Lists:
Queues:
Stacks:
*****
LAB 3
Non-Linear DS
Trees:
Graphs:
******
LAB 4
Merge sort
The mergeSort() function keeps dividing array into subarrays till it cannot be further divided (i.e.
single element). Then merge() function is called to merge two subarrays at a time in the required
order until we get back the whole array in the sorted order.
Implementation of Merge Sort in C
C language does not have inbuilt function for merge sort, so we have to manually implement it.
*****