Write a c++ program that use a whole loop to perform the following tasks?
‐Prompt to accept two number
display all odd numbers between the numbers
- display the sun of all even number between the number
-output the number and its square for all number between the give number
#include <iostream>
using namespace std;
int main() {
int n1, n2, i;
int sum_even = 0;
int sum_of_sq = 0;
int count = 0;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
i = n1;
while (i <= n2) {
if (i % 2 == 1)
cout << i << " ";
else
sum_even += i;
i++;
}
cout << endl;
cout << "the sum of even number is " << sum_even << endl;
i = n1;
cout << "number \t its square" << endl;
while (i<=n2) {
cout << " " << i << "\t " << i*i << endl;
sum_of_sq += i*i;
count++;
i++;
}
if (count % 2 == 1)
cout << "The sum of squares is " << sum_of_sq << endl;
}
Comments
Leave a comment