Advertisement
Iam_Sandeep

Untitled

Apr 19th, 2022
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. #include <stdio.h>
  2. // #include <conio.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. FILE *fp=fopen("temp.txt",'a+');
  6.  
  7. void exit();
  8.  
  9.  
  10. struct ops1{
  11. int n1,n2,res;
  12. }m;
  13.  
  14.  
  15. union ops2{
  16.  
  17. int n1,n2,res1;
  18. float res2;
  19. }n;
  20.  
  21. struct add_operation {
  22. int num;
  23. int f_num;
  24. }o;
  25.  
  26. // function prototype
  27.  
  28. int addition()
  29. {
  30. // declare a local variable
  31. int i,sum=0;
  32. printf (" How many numbers you want to add: ");
  33. scanf ("%d", &o.num);
  34. printf (" Enter the numbers: \n ");
  35. for (i = 1; i <= o.num; i++)
  36. {
  37. scanf(" %d", &o.f_num);
  38. sum = sum + o.f_num;
  39. }
  40. fprintf(fp," Total Sum of the numbers = %d", sum);
  41. printf (" Total Sum of the numbers = %d", sum);
  42. printf("written to output file successfully...");
  43. return 0;
  44. }
  45.  
  46.  
  47. int subtract()
  48. {
  49. printf (" The first number is: ");
  50. scanf (" %d", &m.n1);
  51. printf (" The second number is: ");
  52. scanf (" %d", &m.n2);
  53. m.res = m.n1 - m.n2;
  54. printf (" The subtraction of %d - %d is: %d", m.n1, m.n2, m.res);
  55. }
  56.  
  57. // use multiply() function to multiply two numbers
  58. int multiply()
  59. {
  60. printf (" The first number is: ");
  61. scanf (" %d", &m.n1);
  62. printf (" The second number is: ");
  63. scanf (" %d", &m.n2);
  64. m.res = m.n1 * m.n2;
  65. printf (" The multiplication of %d * %d is: %d", m.n1, m.n2, m.res);
  66. }
  67.  
  68. // use divide() function to divide two numbers
  69. int divide()
  70. {
  71. printf (" The first number is: ");
  72. scanf (" %d", &m.n1);
  73. printf (" The second number is: ");
  74. scanf (" %d", &m.n2);
  75.  
  76. if (m.n2 == 0)
  77. {
  78. printf (" \n Divisor cannot be zero. Please enter another value ");
  79. scanf ("%d", &m.n2);
  80. }
  81. m.res = m.n1 / m.n2;
  82. printf (" \n The division of %d / %d is: %d", m.n1, m.n2, m.res);
  83. }
  84.  
  85. // use sq() function to get the square of the given number
  86. int sq()
  87. {
  88. printf (" Enter a number to get the Square: ");
  89. scanf (" %d", &n.n1);
  90.  
  91. n.res1 = n.n1 * n.n1;
  92. printf (" \n The Square of %d is: %d", n.n1, n.res1);
  93. }
  94.  
  95. // use sqrt1() function to get the square root of the given number
  96. int sqrt1()
  97. {
  98. printf (" Enter a number to get the Square Root: ");
  99. scanf (" %d", &n.n2);
  100.  
  101.  
  102. n.res2 = sqrt(n.n2);
  103. printf (" \n The Square Root is: %f", n.res2);
  104. }
  105.  
  106.  
  107.  
  108. int main()
  109. {
  110. // declaration a local variable op;
  111. int op;
  112. do
  113. {
  114. // displays the multiple operations of the C Calculator
  115. printf (" Select an operation to perform the calculation in C Calculator: ");
  116. 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 ");
  117.  
  118. scanf ("%d", &op); // accepts a numeric input to choose the operation
  119.  
  120.  
  121. // use switch statement to call an operation
  122. switch (op)
  123. {
  124. case 1:
  125. addition(); /* It call the addition() function to add the given numbers */
  126. break; // break the function
  127.  
  128. case 2:
  129. subtract(); /* It call the subtract() function to subtract the given numbers */
  130. break; // break the function
  131.  
  132. case 3:
  133. multiply(); /* It call the multiply() function to multiply the given numbers */
  134. break; // break the function
  135.  
  136. case 4:
  137. divide(); // It call the divide() function to divide the given numbers
  138. break; // break the function
  139.  
  140. case 5:
  141. sq(); // It call the sq() function to get the square of given numbers
  142. break; // break the function
  143.  
  144. case 6:
  145. sqrt1(); /* It call the sqrt1() function to get the square root of given numbers */
  146. break; // break the function
  147.  
  148. case 7:
  149. exit(0); // It call the exit() function to exit from the program
  150. break; // break the function
  151.  
  152. default:
  153. printf(" Something is wrong!! ");
  154. break;
  155. }
  156. printf (" \n \n ********************************************** \n ");
  157. } while (op != 7);
  158.  
  159.  
  160. return 0;
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement