In: Computer Science
Write a C++ code to perform the following steps:
1. Ask the user to enter two whole numbers number1 and number2 (Number1 should be less than number2)
2. Calculate and print the sum of all even numbers between Number1 and Number2
3. Find and print all even numbers between Number1 and Number2
4. Find and print all odd numbers between Number1 and Number2
5. Calculate and print the numbers and their squares between 1 and 20 (inclusive)
6. Calculate and print the sum of the square of the odd numbers between Number1 and Number2
*(USE WHILE; Do not use For)
#include <iostream> // allows program to perform input and output
using namespace std; // program uses names from the std namespace
int main()
{
int number1{0};
int i{0};// first integer read from user
int number2{0}; // second integer read from user
int sum_even{0};
int square{0};
int sum_odd_square{0};
cout << "Enter two integers: "; // prompt user for data
cin >> number1 >> number2; // read values from user
i = number1+1;
// output the results
cout << "All the Even numbers is" << " ";
while( i>number1 && i < number2){
/* If number is divisible by 2, then print.*/
if(i % 2 == 0){
sum_even = sum_even +i;
cout <<i<< " ";
}
/* Increment i. */
i++;
}
cout << "\nSum of all even nummbers is " << sum_even <<" ";
i = number1+1;
cout << "\nAll the odd numbers is" << " ";
while( i>number1 && i < number2){
/* If number is divisible by 2, then print.*/
if(i % 2 != 0){
square = i*i;
sum_odd_square = sum_odd_square + square;
cout <<i<< " ";
}
/* Increment i. */
i++;
}
cout << "\nSum of square of all odd nummbers is " << sum_odd_square <<" ";
i = 1;
cout << "\nnumbers and their square between 1 to 20 " <<" ";
while( i<20){
/* If number is divisible by 2, then print.*/
square = i*i;
cout <<"\n"<< i << " " << square <<" ";
/* Increment i. */
i++;
}
cout << " "<< endl;
return 0;
}
output:
Please upvote if you like it.