Advertisement
_Black_Panther_

AIUB - Task

Nov 16th, 2021 (edited)
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // Task 1:
  3. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8.  
  9. void findLargeNumber (int a, int b)
  10. {
  11.  
  12. if (a < b)
  13. {
  14. cout << b << " is greater than " << a << endl;
  15. }
  16. else
  17. {
  18. cout << a << " is greater than " << b << endl;
  19. }
  20. }
  21.  
  22. void add (int a, int b)
  23. {
  24. // finding larger number
  25. findLargeNumber (a, b);
  26. cout << a << " + " << b << " = " << a+b << endl;
  27. }
  28.  
  29. void sub (int a, int b)
  30. {
  31. // finding larger number
  32. findLargeNumber (a, b);
  33. int num1, num2;
  34. if (a < b)
  35. {
  36. num1 = b;
  37. num2 = a;
  38. }
  39. else
  40. {
  41. num1 = a;
  42. num2 = b;
  43. }
  44.  
  45. cout << num1 << " - " << num2 << " = " << num1-num2 << endl;
  46. }
  47.  
  48. void multiplcate (int a, int b)
  49. {
  50. // finding larger number
  51. findLargeNumber (a, b);
  52. cout << a << " * " << b << " = " << a*b << endl;
  53. }
  54.  
  55.  
  56.  
  57. void divide(int a, int b)
  58. {
  59. // finding larger number
  60. findLargeNumber (a, b);
  61. int num1, num2;
  62. if (a < b)
  63. {
  64. num1 = b;
  65. num2 = a;
  66. }
  67. else
  68. {
  69. num1 = a;
  70. num2 = b;
  71. }
  72.  
  73. cout << num1 << " / " << num2 << " = " << num1/num2 << " , " << num1 << " % " << num2 << " = " << num1 % num2 << endl;
  74. }
  75.  
  76.  
  77.  
  78. int main ()
  79. {
  80.  
  81. int a, b;
  82.  
  83. cout << "Enter first number: ";
  84. cin >> a;
  85.  
  86. cout << "Enter second number: ";
  87. cin >> b;
  88.  
  89.  
  90. add (a, b);
  91. sub (a, b);
  92. multiplcate (a, b);
  93. divide(a, b);
  94.  
  95.  
  96. return 0;
  97. }
  98.  
  99.  
  100.  
  101. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  102. // Task 2
  103. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  104.  
  105.  
  106. #include <iostream>
  107.  
  108. using namespace std;
  109.  
  110. int n = 1;
  111. int sum = 0;
  112.  
  113. void sumofN(){
  114. if(n==51){
  115. cout << "Sum of first 50 natural number is: " << sum << endl;
  116. return;
  117. }else{
  118. sum+=n++;
  119. sumofN();
  120. }
  121. }
  122.  
  123.  
  124. int main()
  125. {
  126. sumofN();
  127.  
  128. return 0;
  129. }
  130.  
  131.  
  132.  
  133.  
  134. /////////////////////////////////////////////////////////////////////////////
  135. // Task: 3
  136. /////////////////////////////////////////////////////////////////////////////
  137.  
  138.  
  139. #include <iostream>
  140. using namespace std;
  141.  
  142. int fact(int n) {
  143. if (n == 0 || n == 1)
  144. return 1;
  145. else
  146. return n * fact(n - 1);
  147. }
  148.  
  149.  
  150. int main() {
  151.  
  152. int n, r, comb, per;
  153.  
  154. cout<<"Enter n : ";
  155. cin>>n;
  156.  
  157. cout<<"\nEnter r : ";
  158. cin>>r;
  159.  
  160. comb = fact(n) / (fact(r) * fact(n-r));
  161. cout << "\nCombination : " << comb;
  162.  
  163. per = fact(n) / fact(n-r);
  164. cout << "\nPermutation : " << per;
  165.  
  166. return 0;
  167. }
  168.  
  169.  
  170.  
  171. /////////////////////////////////////////////////////////////////////////////
  172. // Task: 4
  173. /////////////////////////////////////////////////////////////////////////////
  174.  
  175.  
  176.  
  177.  
  178. #include <iostream>
  179.  
  180. using namespace std;
  181.  
  182.  
  183. static int n = 1; // static variable
  184.  
  185. int sum = 0; // global variable
  186.  
  187. void sumofN(){
  188.  
  189. if(n==51){
  190. cout << "Sum of first 50 natural number is: " << sum << endl;
  191.  
  192. string str = "I am a local variable";
  193. cout << "Hi, " << str << endl;
  194. return;
  195. }else{
  196. sum+=n++;
  197. sumofN();
  198. }
  199. }
  200.  
  201.  
  202. int main()
  203. {
  204. sumofN();
  205. return 0;
  206. }
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement