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 (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)

Solutions

Expert Solution

#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.


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 ( 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)...
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