Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help turning these loops into for loops #include <iostream> using namespa

ID: 3594898 • Letter: I

Question

I need help turning these loops into for loops

#include <iostream>

using namespace std;

int main()

{

int firstNum, secondNum;

cout << "Enter two positive numbers." << endl;

cout << "First number must be less than the second number:" << endl;

cout << "Enter numbers: ";

cin >> firstNum >> secondNum;

while(firstNum >= secondNum || firstNum <= 0 || secondNum <= 0)

{

if(firstNum <= 0 || secondNum <= 0)

cout << "No negative numbers!" << endl;

else

cout << "First number must be less than the second number!" << endl;

cout << "Please try again." << endl;

cout << "Enter two positive numbers." << endl;

cout << "First number must be less than the second number:" << endl;

cout << "Enter numbers: ";

cin >> firstNum >> secondNum;

}

cout << "Odd integers between " << firstNum << " and " << secondNum << " are:" << endl;

int i = 0;

while(firstNum + i <= secondNum)

{

if((firstNum + i) % 2 == 1)

cout << firstNum + i << " ";

i++;

}

cout << endl;

i = 0;

int sum = 0;

while(firstNum + i <= secondNum)

{

if((firstNum + i) % 2 == 0)

sum += firstNum + i;

i++;

}

cout << "Sum of even integers between " << firstNum << " and " << secondNum << " = " << sum << endl;

cout << "Number Square of Number" << endl;

i = 1;

while(i <= 10)

{

cout << i << " " << i*i << endl;

i++;

}

i = 0;

sum = 0;

while(firstNum + i <= secondNum)

{

if((firstNum + i) % 2 == 1)

sum += (firstNum + i) * (firstNum + i);

i++;

}

cout << "Sum of the squares of odd integers between " << firstNum << " and " << secondNum << " = " << sum << endl;

cout << "Upper case letters are: ";

char start = 'A';

i = 0;

while(i < 26)

{

cout << char(start + i) << " ";

i++;

}

cout << endl;

}

Explanation / Answer

Hi

I have modified the code from while to for loops


#include <iostream>
using namespace std;
int main()
{
int firstNum, secondNum;

cout << "Enter two positive numbers." << endl;
cout << "First number must be less than the second number:" << endl;
cout << "Enter numbers: ";

cin >> firstNum >> secondNum;

for(;firstNum >= secondNum || firstNum <= 0 || secondNum <= 0;)

{
if(firstNum <= 0 || secondNum <= 0)
cout << "No negative numbers!" << endl;

else
cout << "First number must be less than the second number!" << endl;
cout << "Please try again." << endl;
cout << "Enter two positive numbers." << endl;
cout << "First number must be less than the second number:" << endl;
cout << "Enter numbers: ";
cin >> firstNum >> secondNum;
}

cout << "Odd integers between " << firstNum << " and " << secondNum << " are:" << endl;

for(int i = 0;firstNum + i <= secondNum;i++)
{
if((firstNum + i) % 2 == 1)
cout << firstNum + i << " ";

}

cout << endl;

int sum = 0;

for(int i = 0;firstNum + i <= secondNum;i++)
{
if((firstNum + i) % 2 == 0)
sum += firstNum + i;

}
cout << "Sum of even integers between " << firstNum << " and " << secondNum << " = " << sum << endl;


cout << "Number Square of Number" << endl;

for(int i = 1;i <= 10;i++)
{
cout << i << " " << i*i << endl;
  
}

sum = 0;

for(int i=0;firstNum + i <= secondNum;i++)
{
if((firstNum + i) % 2 == 1)
sum += (firstNum + i) * (firstNum + i);
  
}

cout << "Sum of the squares of odd integers between " << firstNum << " and " << secondNum << " = " << sum << endl;

cout << "Upper case letters are: ";

char start = 'A';
for(int i = 0;i < 26;i++)

{
cout << char(start + i) << " ";

}

cout << endl;
}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote