Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include <iostream>
  2. #include<cmath>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int firstNum, secondNum;
  8.  
  9. cout << "Enter a number: ";
  10. cin >> firstNum;
  11.  
  12. cout << "Enter another number: ";
  13. cin >> secondNum;
  14.  
  15. //Input logic
  16. while (secondNum < firstNum)
  17. {
  18. cout << "\nInvalid input. The second number must be greater than the first.\n" ;
  19. cout << "Enter another number: ";
  20. cin >> secondNum;
  21. }
  22.  
  23. //a) Output all odd integers between firstNum and secondNum inclusive.
  24. int a = firstNum;
  25.  
  26. cout << "\nOdd numbers: ";
  27. while (a <= secondNum)
  28. {
  29. if (a%2 != 0)
  30. cout << a << " ";
  31. a++;
  32. }
  33. cout << "\n\n";
  34.  
  35. //b) Output sum of all even numbers between firstNum and secondNum inclusive.
  36.  
  37. int b = firstNum, evenSum = 0;
  38.  
  39. while (b <= secondNum)
  40. {
  41. if (b%2 == 0)
  42. evenSum = evenSum + b;
  43. b++;
  44. }
  45.  
  46. cout << "The sum of all even numbers between " << firstNum << " and " << secondNum << " is " << evenSum << "\n\n";
  47.  
  48. //c) Output the integers and its square of 10 integers starting from firstNum indlucing firstNum.
  49. int counter = 1, c = firstNum;
  50. double s;
  51.  
  52. cout << "\nNumber\tSquared\n";
  53.  
  54. while (counter <= 10)
  55. {
  56. cout << c << "\t";
  57. s = pow(c, 2.0);
  58. cout << s << "\n";
  59. counter++;
  60. c++;
  61. }
  62.  
  63. //d) Output sum of squares of odd integers between firstNum and secondNum inclusive.
  64.  
  65. int d = firstNum, squaredSum = 0;
  66.  
  67. while (d <= secondNum)
  68. {
  69. if (d%2 != 0)
  70. {
  71. squaredSum = squaredSum + pow(d, 2.0);
  72. }
  73. d++;
  74. }
  75. cout << "\nThe sum of all squared odd numbers between " << firstNum << " and " << secondNum << " is " << squaredSum << "\n";
  76.  
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement