Advertisement
Iam_Sandeep

Untitled

Apr 19th, 2022
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 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. // fputs("bi",fp);
  41. fprintf(fp," Total Sum of the numbers = %d\n", sum);
  42. printf (" Total Sum of the numbers = %d\n", sum);
  43. printf("written to output file successfully...\n");
  44. return 0;
  45. }
  46.  
  47.  
  48. int subtract()
  49. {
  50. printf (" The first number is: ");
  51. scanf (" %d", &m.n1);
  52. printf (" The second number is: ");
  53. scanf (" %d", &m.n2);
  54. m.res = m.n1 - m.n2;
  55. printf (" The subtraction of %d - %d is: %d\n", m.n1, m.n2, m.res);
  56.  
  57. fprintf(fp," The subtraction of %d - %d is: %d\n", m.n1, m.n2, m.res);
  58. printf("written to output file successfully...\n");
  59. }
  60.  
  61. // use multiply() function to multiply two numbers
  62. int multiply()
  63. {
  64. printf (" The first number is: ");
  65. scanf (" %d", &m.n1);
  66. printf (" The second number is: ");
  67. scanf (" %d", &m.n2);
  68. m.res = m.n1 * m.n2;
  69.  
  70.  
  71. fprintf(fp," The multiplication of %d * %d is: %d\n", m.n1, m.n2, m.res);
  72.  
  73. printf (" The multiplication of %d * %d is: %d", m.n1, m.n2, m.res);
  74. printf("written to output file successfully...\n");
  75. }
  76.  
  77. // use divide() function to divide two numbers
  78. int divide()
  79. {
  80. printf (" The first number is: ");
  81. scanf (" %d", &m.n1);
  82. printf (" The second number is: ");
  83. scanf (" %d", &m.n2);
  84.  
  85. if (m.n2 == 0)
  86. {
  87. printf (" \n Divisor cannot be zero. Please enter another value ");
  88. scanf ("%d", &m.n2);
  89. }
  90. m.res = m.n1 / m.n2;
  91. printf (" \n The division of %d / %d is: %d\n", m.n1, m.n2, m.res);
  92. fprintf(fp," \n The division of %d / %d is: %d\n", m.n1, m.n2, m.res);
  93.  
  94. printf("written to output file successfully...\n");
  95. }
  96.  
  97. // use sq() function to get the square of the given number
  98. int sq()
  99. {
  100. printf (" Enter a number to get the Square: ");
  101. scanf (" %d", &n.n1);
  102.  
  103. n.res1 = n.n1 * n.n1;
  104. printf (" \n The Square of %d is: %d\n", n.n1, n.res1);
  105.  
  106. fprintf(fp," \n The Square of %d is: %d\n", n.n1, n.res1);
  107.  
  108. printf("written to output file successfully...\n");
  109. }
  110.  
  111. // use sqrt1() function to get the square root of the given number
  112. int sqrt1()
  113. {
  114. printf (" Enter a number to get the Square Root: ");
  115. scanf (" %d", &n.n2);
  116.  
  117.  
  118. n.res2 = sqrt(n.n2);
  119. printf (" \n The Square Root is: %f", n.res2);
  120. fprintf(fp," \n The Square Root is: %f", n.res2);
  121.  
  122. printf("written to output file successfully...\n");
  123. }
  124.  
  125.  
  126.  
  127. int main()
  128. {
  129. // declaration a local variable op;
  130. int op;
  131. do
  132. {
  133. // displays the multiple operations of the C Calculator
  134. printf (" Select an operation to perform the calculation in C Calculator: ");
  135. 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 ");
  136.  
  137. scanf ("%d", &op); // accepts a numeric input to choose the operation
  138.  
  139.  
  140. // use switch statement to call an operation
  141. switch (op)
  142. {
  143. case 1:
  144. addition(); /* It call the addition() function to add the given numbers */
  145. break; // break the function
  146.  
  147. case 2:
  148. subtract(); /* It call the subtract() function to subtract the given numbers */
  149. break; // break the function
  150.  
  151. case 3:
  152. multiply(); /* It call the multiply() function to multiply the given numbers */
  153. break; // break the function
  154.  
  155. case 4:
  156. divide(); // It call the divide() function to divide the given numbers
  157. break; // break the function
  158.  
  159. case 5:
  160. sq(); // It call the sq() function to get the square of given numbers
  161. break; // break the function
  162.  
  163. case 6:
  164. sqrt1(); /* It call the sqrt1() function to get the square root of given numbers */
  165. break; // break the function
  166.  
  167. case 7:
  168. exit(0); // It call the exit() function to exit from the program
  169. break; // break the function
  170.  
  171. default:
  172. printf(" Something is wrong!! ");
  173. break;
  174. }
  175. printf (" \n \n ********************************************** \n ");
  176. } while (op != 7);
  177.  
  178. fclose(fp);
  179. return 0;
  180. }
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement