Question

In: Computer Science

Write a C++ code to perform the following steps: 1. Ask the user to enter two...

Write a C++ code to perform the following steps:

1. Ask the user to enter two whole numbers number1 and number2 ( Number 1 should be less than Number 2)

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 2 and 20 (inclusive)

6. Calculate and print the sum of the square of the odd numbers between Number1 and Number2

Solutions

Expert Solution

CPP Program :

//header files
#include <iostream>
using namespace std;
//main() method
int main()
{
//declaring variable to store number1
int number1=0;
//declaring variable to store number2
int number2=0;
//declaring variable to store sumOfEvens
int sumOfEvens=0;
//declaring variable to store sum of the square of the odd numbers
int sumOfOddNumbers=0;
//asking user to enter number1
cout<<"Enter number1 : ";
//reading number1
cin>>number1;
//asking user to enter number2
cout<<"Enter number2 : ";
//reading number2
cin>>number2;
while(number2<number1)
{
//if number2 is less than number1 then
//asking user to enter number
cout<<"Enter number2 greter than "<<number1<<" : ";
//reading number2
cin>>number2;
}
cout<<"Even numbers between "<<number1<<" and "<<number2<<" : "<<endl;
//using for loop
for(int i=number1;i<=number2;i++)
{
  
//checking numbers for odd and event
if(i%2==0)
{
//if even number then print it
cout<<i<<" ";//print even numbers
//add i to the sumOfEvens
sumOfEvens+=i;
}
}
cout<<"\nOdd numbers between "<<number1<<" and "<<number2<<endl;
//using for loop
for(int i=number1;i<=number2;i++)
{
  
//checking numbers for odd and event
if(i%2!=0)
{
//if odd number then print it
cout<<i<<" ";//print even numbers
sumOfOddNumbers+=i*i;//square number and store in the sumOfOddNumbers
}
}
//print sum of even numbers
cout<<"\nSum of even numbers :"<<sumOfEvens<<endl;
//print sum of the square of the odd numbers
cout<<"Sum of the square of the odd numbers between "<<number1<<" and "<<number2<<" : "<<sumOfOddNumbers<<endl;
// print the numbers and their squares between 2 and 20
cout<<"Numbers and their squares between 2 and 20"<<endl;
cout<<"Number Square"<<endl;
cout<<"----------------------"<<endl;
//using for loop
for(int i=2;i<=20;i++)
{
cout<<i<<" "<<(i*i)<<endl;//print number and square
}
return 0;
}
============================================================

Output :


Related Solutions

Write a C++ code to perform the following steps: 1. Ask the user to enter two...
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...
Write a C++ script to perform the following: a.) Ask a user to enter 2 real...
Write a C++ script to perform the following: a.) Ask a user to enter 2 real numbers. b.) Save the user response as a variable a and b. c.) Display if the numbers are even or odd. d.) Check if the number a belongs in the interval D = [10, 99], display the output as "a is between 10 and 99" or "a is not between 10 and 99". e.) Check if the number b belongs in the interval E...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code should print the numbers in descending order.
Write a java program that perform the following: 1. Ask a user ti enter 10 student...
Write a java program that perform the following: 1. Ask a user ti enter 10 student test score on a test (100 point test and save the score in an array 2. Iterate through the array to find the average and highest of these score, print them out 3 Count how many student score below average and print them out 4 . Instructor decide to curve the student score using square root curving method( take the square root of the...
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
1. Write Python code to - (a) ask user to enter a list of animals, one...
1. Write Python code to - (a) ask user to enter a list of animals, one item at a time, until “done” to terminate input (b) randomly display a selected item from the list, starting with “I like ______”. When run, the output should look something like this: Enter an item; 'done' when finished: cats Enter an item; 'done' when finished: dogs Enter an item; 'done' when finished: guinea pigs Enter an item; 'done' when finished: done You are done...
Write a mips assembly code program that ask the user to enter an integer value, and...
Write a mips assembly code program that ask the user to enter an integer value, and then print the result of doubling that number.
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
Write a program that does the following in order: 1. Ask user to enter a name...
Write a program that does the following in order: 1. Ask user to enter a name 2. Ask the user to enter five numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 3. Calculate the sum of the numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 4. If the sum is greater than 0, print out the sum 5. If the sum is equal to zero, print out “Your account balance is zero” 6. If the sum is less than 0, print out “Your account...
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT