Answer to Question #132463 in C++ for thulile

Question #132463
Practical 3 (while loop)
Write a program that uses while loops to perform the following steps:
a. Prompt the user to input two integers: firstNum and secondNum
(firstNum must be less than secondNum).
b. Output all odd numbers between firstNum and secondNum.
c. Output the sum of all even numbers between firstNum and secondNum.
d. Output the numbers and their squares between 1 and 10.
e. Output the sum of the square of the odd numbers between firstNum and secondNum.
f. Output all uppercase letters.
1
Expert's answer
2020-09-10T15:45:14-0400
#include <iostream>
using namespace std;


int main()
{
    //a. Prompt the user to input two integers: firstNum and secondNum
    //(firstNum must be less than secondNum).
    cout << "a. Prompt the user to input two integers. First must be less than second\n";
    double firstNum = 2;
    double secondNum = 1;


    while (firstNum > secondNum)
    {
       cout << "enter first number: \n";
       cin >> firstNum;
       cout << "enter second number (must be more than first number): \n";
       cin >> secondNum;


       if (firstNum > secondNum) 
           cout << "first number must be less than second number. Try again. \n";
    }
    cout << "\n";


    //b. Output all odd numbers between firstNum and secondNum.
    cout << "b. Output all odd numbers between firstNum and secondNum.\n";
    int number = firstNum;


    while (number <= secondNum)
    {
        if (number % 2)
            cout << number++ << "\n";
        else
        {
            number++;
            continue;
        }
    }
    cout<<"\n";


    //c. Output the sum of all even numbers between firstNum and secondNum.
    cout << "c. Output the sum of all even numbers between firstNum and secondNum.\n";
    number = firstNum;
    int sum = 0;


    while (number <= secondNum)
    {
        if (!(number % 2))
            sum += number++;
        else
        {
            number++;
            continue;
        }
    }
    cout << "sum = " << sum << "\n\n";


    //d. Output the numbers and their squares between 1 and 10.
    cout << "d.Output the numbers and their squares between 1 and 10.\n";
    number = 1;
    while (number <= 10)
    {
        cout << number << " -> " << number << "^2" << " = " << number * number << '\n';
        number++;
    }
    cout << "\n";


    //e. Output the sum of the square of the odd numbers between firstNum and secondNum.
    cout << "Output the sum of the square of the odd numbers between firstNum and secondNum\n";
    number = firstNum;
    sum = 0;
    while (number <= secondNum)
    {
        if (number % 2)
        {
            sum += (number * number);
            number++;
        }
        else
        {
            number++;
            continue;
        }
    }
    cout << "sum: " << sum << "\n\n";


    //f. Output all uppercase letters.
    cout << "Output all uppercase letters.\n";
    int c = 65;


    while (c < 91)
        cout << char(c++) << ", ";
    cout << "\n";
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Hailan
24.10.21, 17:04

Hi thanks a lot for this code I currently studying how this code works

Assignment Expert
25.06.21, 14:16

Dear BG,

Questions in this section are answered for free. We can't fulfill them all and there 

is no guarantee of answering certain question but we are doing our best

Although if you have serious assignment that requires large amount 

of work and hence cannot be done for free you can submit it as assignment and our 

experts will surely assist you.



BG
25.06.21, 13:02

Hello, can someone help break down this code? I don't fully understand how it works.

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS