Advertisement
3vo

Problem 5. Min, Max, Sum and Average of N Numbers

3vo
Nov 2nd, 2022 (edited)
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Problem 5. Min, Max, Sum and Average of N Numbers
  2. //
  3. // Write a program that reads from the console a sequence of n integer numbers and returns the minimal, the maximal number,
  4. // the sum and the average of all numbers (displayed with 2 digits after the decimal point).
  5. // The input starts by the number n (alone in a line) followed by n lines, each holding an integer number.
  6. // The output is like in the examples below.
  7. //
  8. //   Examples:
  9. // n    output
  10. // 3    min = 1
  11. // 2    max = 5
  12. // 5    sum = 8
  13. // 1    avg = 2.66
  14.  
  15. let input = ['3',
  16.     '2',
  17.     '5',
  18.     '1'
  19. ];
  20.  
  21. let numCount = Number(gets());
  22. let arr = [];
  23.  
  24. let numMin = Number.MAX_SAFE_INTEGER;
  25. let numMax = Number.MIN_SAFE_INTEGER
  26.  
  27. // loop with length of numCount
  28. for (let i = 0; i < numCount; i++) {
  29.     arr.push(+gets());
  30.  
  31. }
  32.  
  33. for (let i = 0; i < arr.length; i++){
  34.     if (numMin > arr[i]) {
  35.         numMin = arr[i].toFixed(2);
  36.     }
  37. }for (let i = 0; i < arr.length; i++){
  38.     if (numMax < arr[i]) {
  39.         numMax = arr[i].toFixed(2);
  40.     }
  41. }
  42.  
  43. let sum = 0;
  44. for( let i = 0; i < arr.length; i++) {
  45.     sum += arr[i];
  46.  
  47. }
  48. sum = sum.toFixed(2);
  49. let numAvg = (sum / numCount).toFixed(2)
  50.  
  51. console.log(`min=${numMin}
  52. max=${numMax}
  53. sum=${sum}
  54. avg=${numAvg}`)
  55.  
  56.  
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement