JSPM - DS C Programming-1
JSPM - DS C Programming-1
JSPM - DS C Programming-1
C Programming
LAB 1
CALCULATOR
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.
Following are the different ways to write a Calculator Program in the C language.
program.c
1. #include <stdio.h>
2. int main()
3. {
5. char opt;
7. float res;
11. {
13. }
15. {
17. }
18.
20. {
22. }
24. {
26. }
31.
32. switch(opt)
33. {
36. printf (" Addition of %d and %d is: %.2f", n1, n2, res);
37. break;
38.
41. printf (" Subtraction of %d and %d is: %.2f", n1, n2, res);
42. break;
43.
46. printf (" Multiplication of %d and %d is: %.2f", n1, n2, res);
47. break;
48.
51. {
52. printf (" \n Divisor cannot be zero. Please enter another value ");
54. }
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:
program2.c
1. #include <stdio.h>
2. int main()
3. {
5. char opt;
7. float res;
14.
16. {
18. printf (" Addition of %d and %d is: %f", n1, n2, res);
19. }
20.
22. {
24. printf (" Subtraction of %d and %d is: %f", n1, n2, res);
25. }
26.
28. {
31. }
32.
34. {
36. {
37. printf (" \n Divisor cannot be zero. Please enter another value ");
39. }
41. printf (" Division of %d and %d is: %.2f", n1, n2, res);
42. }
43. else
44. {
46. }
47. return 0;
48. }
Output:
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. {
9. float res;
11. do
12. {
14. printf (" Select an operation to perform the calculation in C Calculator: ");
16.
17. scanf ("%d", &op); // accepts a numeric input to choose the operation
18.
19.
22. {
23. case 1:
33.
34. case 2:
44.
45. case 3:
55.
56. case 4:
63. if (n2 == 0)
64. {
65. printf (" \n Divisor cannot be zero. Please enter another value ");
67. }
71.
72. case 5:
77.
81.
82. case 6:
87.
88. res = sqrt(n1); // use sqrt() function to find the Square Root
89. printf (" Square Root of %d numbers is: %.2f", n1, res);
91.
92. case 7:
94. exit(0);
97. default:
99. break;
100. }
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();
14.
16. {
19. do
20. {
22. printf (" Select an operation to perform the calculation in C Calculator: ");
24.
25. scanf ("%d", &op); // accepts a numeric input to choose the operation
26.
27.
30. {
31. case 1:
32. addition(); /* It call the addition() function to add the given numbers */
34.
35. case 2:
36. subtract(); /* It call the subtract() function to subtract the given numbers */
39. case 3:
40. multiply(); /* It call the multiply() function to multiply the given numbers */
42.
43. case 4:
44. divide(); // It call the divide() function to divide the given numbers
46.
47. case 5:
48. sq(); // It call the sq() function to get the square of given numbers
50.
51. case 6:
52. sqrt1(); /* It call the sqrt1() function to get the square root of given numbers */
54.
55. case 7:
56. exit(0); // It call the exit() function to exit from the program
58.
59. default:
61. break;
62. }
65.
66.
67. return 0;
68. }
69.
70.
71.
74. {
76. printf (" How many numbers you want to add: ");
80. {
83. }
85. return 0;
86. }
87.
90. {
97. printf (" The subtraction of %d - %d is: %d", n1, n2, res);
98. }
99.
100. // use multiply() function to multiply two numbers
102. {
109. printf (" The multiply of %d * %d is: %d", n1, n2, res);
110. }
111.
114. {
120.
121. if (n2 == 0)
122. {
123. printf (" \n Divisor cannot be zero. Please enter another value ");
125. }
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. {
136.
139. }
140.
141. // use sqrt1() function to get the square root of the given number
143. {
146. printf (" Enter a number to get the Square Root: ");
148.
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.
C language does not have inbuilt function for merge sort, so we have to manually implement it.
*****