Kaloyankerr

SchoolHW №2

Mar 16th, 2021 (edited)
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.94 KB | None | 0 0
  1. Задача 1)
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {  
  7.     int i, n;
  8.     cout << "Number n: ";
  9.     cin >> n;
  10.     i = 1;
  11.  
  12.     while (i <= n) {
  13.     cout << i << endl;
  14.     i++;
  15.     }
  16. }
  17. =====================================
  18. Задача 2)  
  19. #include <iostream>
  20. using namespace std;
  21.  
  22. int main()
  23. {  
  24.     int n;
  25.     cout << "Number n: ";
  26.     cin >> n;
  27.     int i = 1, sum = 0;
  28.    
  29.     while (i <= n) {
  30.     sum += i;
  31.     i++;
  32.     }
  33.     cout << "Total sum: " << sum << endl;
  34. }
  35. =====================================
  36. Задача 3)
  37. #include <iostream>
  38. using namespace std;
  39.  
  40. int main()
  41. {  
  42.     int n, i = 1;
  43.     cout << "Number n: ";
  44.    
  45.     if (n <= 0){
  46.         cout << "No numbers!";
  47.         n = 1;
  48.     }
  49.     cin >> n;
  50.    
  51.     while (i < n) {
  52.         if (i % 2 == 0)
  53.                 cout << i << endl;
  54.        
  55.     i++;
  56.     }
  57.    
  58. }
  59. =====================================
  60. Задача 4)  
  61. #include <iostream>
  62. #include <cmath>
  63. using namespace std;
  64.  
  65. int main()
  66. {  
  67.     int x, y, power;
  68.     cout << "x: ";
  69.     cin >> x;
  70.    
  71.     cout << "y: ";
  72.     cin >> y;
  73.    
  74.     power = 1;
  75.    
  76.     while (pow(x, power) < y){
  77.         cout << power << endl;
  78.         power ++;
  79.     }
  80. }
  81. =====================================
  82. Задача 5)
  83. #include <iostream>
  84. using namespace std;
  85.  
  86. int main()
  87. {  
  88.     int num, sum;
  89.     cout << "Number: ";
  90.     cin >> num;
  91.    
  92.     while (num != 0){
  93.         sum += num;
  94.         cout << "Number: ";
  95.         cin >> num;
  96.     }
  97.     cout << "Total sum: " << sum;
  98. }
  99. =====================================
  100. Задача 6)
  101. #include <iostream>
  102. using namespace std;
  103.  
  104. int main()
  105. {  
  106.     int num, mul = 1;
  107.     cout << "Number: ";
  108.     cin >> num;
  109.    
  110.     while (num != 0){
  111.         mul *= num;
  112.         cout << "Number: ";
  113.         cin >> num;
  114.     }
  115.     cout << "Total multiplication: " << mul;
  116. }
  117. =====================================
  118. Задача 7)
  119. #include <iostream>
  120. using namespace std;
  121.  
  122. int main()
  123. {  
  124.     int num, count;
  125.     cout << "Number: ";
  126.     cin >> num;
  127.    
  128.     while (num != 0){
  129.         count += 1;
  130.         cout << "Number: ";
  131.         cin >> num;
  132.     }
  133.     cout << "Total count: " << count;
  134. }
  135. =====================================
  136. Задача 8)
  137. #include <iostream>
  138. #include <bits/stdc++.h>
  139. using namespace std;
  140.  
  141.  
  142.  
  143. int main()
  144. {  
  145.     vector<int> numbers;   
  146.     int n;
  147.     cout << "Number n: ";
  148.     cin >> n;
  149.    
  150.     while (n != 0){
  151.         numbers.push_back(n);
  152.         cout << "Number n: ";
  153.         cin >> n;
  154.     }
  155.    
  156.     cout << "Max Element: " << *max_element(numbers.begin(), numbers.end()) << endl;
  157.     cout << "Min Element: " << *min_element(numbers.begin(), numbers.end());
  158.        
  159.     return 0;
  160. }
  161. =====================================
  162. Задача 9)
  163. #include <iostream>
  164. using namespace std;
  165.  
  166. int main()
  167. {  
  168.     int num, possitvie_counter = 1;
  169.    
  170.     while (possitvie_counter <= 3){
  171.        
  172.     cout << "Enter a number: ";
  173.     cin >> num;
  174.    
  175.     if (num > 0)
  176.         possitvie_counter++;
  177.    
  178.    
  179.     }
  180.     cout << "You've entered 3 possitive numbers: " << possitvie_counter;
  181.     return 0;
  182. }
  183. =====================================
  184. Задача 10)
  185. Начин 1)
  186. #include <iostream>
  187. using namespace std;
  188.  
  189. int main() {
  190.     int n, reversedNumber = 0, remainder;
  191.  
  192.     cout << "Enter an integer: ";
  193.     cin >> n;
  194.  
  195.     while(n != 0) {
  196.         remainder = n%10;
  197.         reversedNumber = reversedNumber*10 + remainder;
  198.         n /= 10;
  199.     }
  200.  
  201.     cout << "Reversed Number = " << reversedNumber;
  202.  
  203.     return 0;
  204. }
  205.  
  206. Начин 2)
  207. #include <iostream>
  208. #include <sstream>
  209. #include <bits/stdc++.h>
  210. using namespace std;
  211.  
  212. int main()
  213. {  
  214.     int num;
  215.    
  216.     cout << "Enter a number: ";
  217.     cin >> num;
  218.    
  219.     stringstream ss;
  220.     ss << num;
  221.    
  222.     string number_to_str;
  223.     ss >> number_to_str;
  224.    
  225.     reverse(number_to_str.begin(), number_to_str.end());
  226.     cout << "The number " << num << " in reversed is " + number_to_str;
  227.    
  228.     return 0;
  229. }
  230. =====================================
  231. Задача 11)
  232. #include <iostream>
  233. using namespace std;
  234.  
  235.  
  236. int main()  
  237. {  
  238.     int num, sum = 0, int_char;
  239.    
  240.     cout << "Enter a number: ";
  241.     cin >> num;
  242.    
  243.     while (num > 0) {
  244.         int_char = num % 10;    
  245.         sum = sum + int_char;    
  246.         num = num / 10;    
  247.     }    
  248.    
  249.     cout << "Sum is: " << sum << endl;
  250.     return 0;  
  251. }
  252. =====================================
  253. Задача 12)
  254. #include <iostream>
  255. using namespace std;
  256.  
  257.  
  258. int main()  
  259. {  
  260.     int num, num_numerical = 0, int_char;
  261.     cout << "Enter a number: ";
  262.     cin >> num;
  263.    
  264.     while (num > 0) {
  265.         int_char = num % 10;
  266.         num = num / 10;
  267.         num_numerical++;
  268.     }    
  269.    
  270.     cout << "Number numerical: " << num_numerical << endl;
  271.     return 0;  
  272. }
  273. =====================================
  274. Задача 13)
  275. #include <iostream>
  276. using namespace std;
  277.  
  278.  
  279. int main()  
  280. {  
  281.     int num, reversed_num = 0, remainder, sum, num_copy;
  282.  
  283.     cout << "Enter an number: ";
  284.     cin >> num;
  285.     num_copy = num;
  286.  
  287.     while(num != 0) {
  288.         remainder = num % 10;
  289.         reversed_num = reversed_num * 10 + remainder;
  290.         num /= 10;
  291.     }
  292.  
  293.     cout << "Reversed Number = " << reversed_num << endl << endl;
  294.    
  295.     sum = (num_copy + reversed_num);
  296.     cout << "The sum of the 2 numbers is: " << sum;
  297.     return 0;  
  298. }  
Advertisement
Add Comment
Please, Sign In to add comment