Vladi1442

Untitled

Feb 16th, 2022
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main(){
  5. // when you have something that you are gonna be using as a sum of some numbers, you set it like a 0
  6. // when you have something that you are gonna be using as a product of some numbers, you set it like a 1
  7. // in this case we have result = 1, because I'll use it like a product
  8.  
  9. int base, result = 1;
  10. double exponent;
  11.  
  12. cout << "Enter base: ";
  13. cin >> base;
  14.  
  15. cout << "Enter exponent: ";
  16. cin >> exponent;
  17.  
  18. while(exponent != 0){
  19. result = result * base;
  20. exponent--;
  21. }
  22.  
  23. cout << "Result: " << result << endl;
  24.  
  25. // it's the same like in past task(we have a product), that's why we have factorial = 1
  26.  
  27. int n, factorial = 1;
  28. cout << "Enter a number: ";
  29. cin >> n;
  30.  
  31. for(int i = 1; i <= n; i++){
  32. factorial = factorial * i;
  33. }
  34.  
  35. cout << "The factorial of a number is: " << factorial << endl;
  36.  
  37. int n;
  38. cout << "Enter a number: ";
  39. cin >> n;
  40.  
  41. for(int i = 0; i < n; i++){
  42. cout << "a" << i << " = " << i * i + 3 * i << endl;
  43. }
  44.  
  45. int n, currentNegativeNumber = INT_MAX;
  46. cin >> n;
  47.  
  48. int number;
  49. for(int i = 0; i < n; i++){
  50. cin >> number;
  51. if(number > 0 && number < currentNegativeNumber){
  52. number =currentNegativeNumber;
  53. }
  54. }
  55.  
  56. cout << currentNegativeNumber;
  57.  
  58. // another solution for biggest negative number!
  59.  
  60. double number;
  61. double negative = 0;
  62. double largest = 0;
  63.  
  64. cout << "Enter a number until reaches zero: ";
  65. cin >> number;
  66.  
  67. while(number != 0){
  68. if(number < 0){
  69. negative = number;
  70. if(largest == 0){
  71. largest = negative;
  72. } else {
  73. if(negative > largest){
  74. largest = negative;
  75. }
  76. }
  77. } // when we are at the end of the loop, we need to cin a number, until we get biggest negative
  78. cin >> number;
  79. }
  80.  
  81. if(largest != 0){
  82. cout << largest << " ";
  83. } else {
  84. cout << "non negative number" << endl;
  85. }
  86.  
  87. // find the sum of digits(n % 10 and n / 10 - that's what we need)
  88. // sum = 0, because we have the sum of digits and set it as zero
  89. int num, sum = 0;
  90. cout << "Enter a number: ";
  91. cin >> num;
  92.  
  93. while(num > 0){
  94. sum += num % 10;
  95. num /= 10;
  96. }
  97.  
  98. cout << "The sum of digits in one number is " << sum << endl;
  99.  
  100. int number, sum = 0;
  101.  
  102. while(number > 0){
  103. cin >> number;
  104. sum += number;
  105. }
  106.  
  107. cout << "The sum of numbers is " << sum << endl;
  108.  
  109.  
  110. int a, b, product = 1;
  111. cout << "Enter two numbers: ";
  112. cin >> a >> b;
  113.  
  114. for(int i = a; i <= b; i++){
  115. product *= i;
  116. }
  117.  
  118. cout << "The product of this interval is " << product << endl;
  119.  
  120. // there is one way to print fibonacci series
  121.  
  122. int n, term1 = 0, term2 = 1, nextTerm = 0;
  123. cin >> n;
  124.  
  125. for(int i = 2; i <= n; i++){ // it's started from 2, we are not counting 0 and 1
  126. nextTerm = term1 + term2;
  127. term1 = term2;
  128. term2 = nextTerm;
  129. }
  130.  
  131. cout << nextTerm << endl;
  132.  
  133. // here is another way
  134.  
  135. int n, term1 = 0, term2 = 1, nextTerm = 0;
  136. cin >> n;
  137.  
  138. for(int i = 0; i <= n; i++){ // it's kinda saving up the code
  139. if(i == 0){
  140. continue;
  141. }
  142. if(i == 1){
  143. continue;
  144. }
  145. nextTerm = term1 + term2;
  146. term1 = term2;
  147. term2 = nextTerm;
  148. }
  149.  
  150. cout << nextTerm << endl;
  151.  
  152. int n;
  153. bool isPrime = true;
  154. cin >> n;
  155.  
  156. if(n == 0 || n == 1){ // if(n <= 1) - we can write it like that also
  157. isPrime = false;
  158. }
  159.  
  160. for(int i = 2; i < n / 2; i++){
  161. if(n % i == 0){
  162. isPrime = false;
  163. break;
  164. }
  165. }
  166.  
  167.  
  168. cout << (isPrime ? "True" : "False") << endl;
  169.  
  170.  
  171.  
  172. int n;
  173. char symbol1, symbol2;
  174. cin >> n;
  175. cin >> symbol1 >> symbol2;
  176.  
  177. for(int i = 0; i < n - 1; i++){
  178. for(int j = 0; j < n; j++){
  179. if(i >= j){
  180. cout << symbol1 << " ";
  181. } else if(i < j){
  182. cout << symbol2 << " ";
  183. }
  184. }
  185. cout << endl;
  186. }
  187.  
  188.  
  189.  
  190.  
  191. int n, d = 1, binary_number = 0;
  192. cin >> n;
  193.  
  194. if(n < 0 && n > 1000){
  195. cout << "Wtf are you doing, you're gonna get the error!" << endl;
  196. } else {
  197. while(n != 0){
  198. binary_number += (n % 2) * d;
  199. d *= 10;
  200. n /= 2;
  201. }
  202. cout << binary_number;
  203. }
  204.  
  205.  
  206.  
  207. cout << "We need to get all consonants!" << endl;
  208.  
  209. for(int i = 'A'; i <= 'Z'; i++){
  210. bool isConsonant = !(i == 'A' || i == 'E' || i == 'U' || i == 'O' || i == 'I');
  211.  
  212. if(isConsonant){
  213. cout << (char)i << " ";
  214. }
  215. }
  216.  
  217.  
  218.  
  219.  
  220. int n;
  221. cin >> n;
  222.  
  223. for(int i = 0; i < n; i++){
  224. for(int j = 0; j < n; j++){
  225. if(i > j){
  226. cout << "-";
  227. } else if(i == j){
  228. cout << "0";
  229. } else if(i < j){
  230. cout << "+";
  231. }
  232. }
  233. cout << endl;
  234. }
  235.  
  236.  
  237. for(int test = 5; test != 0; test++){
  238. cout << "< ";
  239. cin >> test;
  240. cout << test << endl;
  241. }
  242.  
  243. for(int test = 5; test != 0;){
  244. cout << "< ";
  245. cin >> test;
  246. cout << test << endl;
  247. }
  248.  
  249.  
  250.  
  251. int number;
  252. int count = 0;
  253. int sum = 0;
  254. double average;
  255.  
  256. cout << "Enter a number: ";
  257. cin >> number;
  258.  
  259. while(number != 0){
  260. sum += number;
  261. count++;
  262.  
  263. cout << "Enter a number: ";
  264. cin >> number;
  265. }
  266.  
  267. if(count != 0){
  268. average = sum / count;
  269. }
  270.  
  271. cout << "Average: " << average << endl;
  272.  
  273. return 0;
  274. }
Add Comment
Please, Sign In to add comment