Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<cmath>
- using namespace std;
- int main()
- {
- int firstNum, secondNum;
- cout << "Enter a number: ";
- cin >> firstNum;
- cout << "Enter another number: ";
- cin >> secondNum;
- //Input logic
- while (secondNum < firstNum)
- {
- cout << "\nInvalid input. The second number must be greater than the first.\n" ;
- cout << "Enter another number: ";
- cin >> secondNum;
- }
- //a) Output all odd integers between firstNum and secondNum inclusive.
- int a = firstNum;
- cout << "\nOdd numbers: ";
- while (a <= secondNum)
- {
- if (a%2 != 0)
- cout << a << " ";
- a++;
- }
- cout << "\n\n";
- //b) Output sum of all even numbers between firstNum and secondNum inclusive.
- int b = firstNum, evenSum = 0;
- while (b <= secondNum)
- {
- if (b%2 == 0)
- evenSum = evenSum + b;
- b++;
- }
- cout << "The sum of all even numbers between " << firstNum << " and " << secondNum << " is " << evenSum << "\n\n";
- //c) Output the integers and its square of 10 integers starting from firstNum indlucing firstNum.
- int counter = 1, c = firstNum;
- double s;
- cout << "\nNumber\tSquared\n";
- while (counter <= 10)
- {
- cout << c << "\t";
- s = pow(c, 2.0);
- cout << s << "\n";
- counter++;
- c++;
- }
- //d) Output sum of squares of odd integers between firstNum and secondNum inclusive.
- int d = firstNum, squaredSum = 0;
- while (d <= secondNum)
- {
- if (d%2 != 0)
- {
- squaredSum = squaredSum + pow(d, 2.0);
- }
- d++;
- }
- cout << "\nThe sum of all squared odd numbers between " << firstNum << " and " << secondNum << " is " << squaredSum << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement