Write a program that uses while loops and performs the following

Prompts the user to input 2 integers firstNum and secondNum // Easy

Output all odd numbers between firstNum and secondNum // No idea how to start that.

Outputs the sum of all even numbers between firstNum and secondNum

Output the numbers and their squares between 1 and 10

Output the sum of the square of the Odd Numbers between firstNum and secondNum

Output ALL Uppercase letters***.


I have no idea how to even start this program other then the basic input statement, a point in the right direction would be appreciated ^^;

-Exo

Dani AI

Generated

Common problems seen in the thread and how to avoid them:

  • reading or using loop variables before taking input (e.g. setting i = firstNum before cin), which produces garbage or skips the loop;
  • missing braces around multi-statement if/else, which makes blocks execute unintentionally (the “dangling statement” trap);
  • the wrong accumulation expression sum += sum + i; (should be sum += i; or sum = sum + i;);
  • off-by-one or infinite-loop bugs when the loop condition or increment is wrong.
    Read both inputs first, compute a clear low/high range, initialize accumulators, and use braces generously.

A compact, while-based example that covers every required piece (keeps inputs in any order, avoids the common pitfalls, and uses 64-bit accumulation for squares):

#include <iostream>
using namespace std;

int main() {
    int a, b;
    if (!(cin >> a >> b)) return 0;

    int low  = (a <= b) ? a : b;
    int high = (a <= b) ? b : a;

    cout << "Odd numbers: ";
    int cur = low;
    while (cur <= high) {
        if ((cur & 1) != 0) cout << cur << ' ';
        ++cur;
    }
    cout << '\n';

    long long even_sum = 0;
    cur = low;
    while (cur <= high) {
        if ((cur & 1) == 0) even_sum += cur;
        ++cur;
    }
    cout << "Sum of evens: " << even_sum << '\n';

    cout << "1..10 and squares:\n";
    int n = 1;
    while (n <= 10) { cout << n << ' ' << (1LL * n * n) << '\n'; ++n; }

    long long odd_sq_sum = 0;
    cur = low;
    while (cur <= high) { if ((cur & 1) != 0) odd_sq_sum += 1LL*cur*cur; ++cur; }
    cout << "Sum of squares of odds: " << odd_sq_sum << '\n';

    cout << "Uppercase letters: ";
    char ch = 'A';
    while (ch <= 'Z') { cout << ch << ' '; ++ch; }
    cout << '\n';
    return 0;
}

Notes tied to thread posts: correctly moved input outside the loop and handled ordering; highlighted initialization issues; suggested iterating a char from 'A' to 'Z'; showed the square loop idea. Additional tips: prefer <= when the endpoint must be included, test with equal and reversed inputs, watch negative ranges, and compile with warnings enabled. Use long long (or cast when squaring) for safety if inputs can be large.

Recommended Answers

All 17 Replies

Prompts the user to input 2 integers firstNum and secondNum // Easy

Output all odd numbers between firstNum and secondNum // No idea how to start that.

Outputs the sum of all even numbers between firstNum and secondNum

Output the numbers and their squares between 1 and 10

Output the sum of the square of the Odd Numbers between firstNum and secondNum

These would all be easier to do with a for loop really, but if you're required to create a while loop, use this pseudocode to help..

while i < second num
{
//code
i++
}

In order to find if they're even numbers, use i%2 and if it equals 0, then it's even. Otherwise it's odd. A simple if/else can help there.

sum += sum + i;

Make sure you start sum at 0 before the loop.


I'm not sure I understand what the 'Output ALL Uppercase numbers' is?

<snip>

sum += sum + i;

Make sure you start sum at 0 before the loop.

You really meant either:

sum += i;
//or
sum = sum + i;

right?

The uppercase letters problem - use a loop with a character as the loop variable, starting it at 'A'. Increment it till it's 'Z'

#include <iostream>

using namespace std;

int main()
{
		cout << "Please input 2 integers: ";
		cin >> firstNum >> secondNum;
		cout << endl;

	while (firstNum >= secondNum)

Im honestly stuck and cannot find any examples of this in my book could i get a little more help ?

Which do you need more help with?

And yeah Vmanes, I definitely meant that =P

Im not asking anyone to hold my hand thru this problem im just at a road block i definetly learn better by seeing actual codes and the fact my book shows no similar exaples sucks so If you can tell me another step or 2 based on my code i posted i might be able to head in the right direction

#include <iostream>

using namespace std;

int main()
{
		cout << "Please input 2 integers: ";
		cin >> firstNum >> secondNum;
		cout << endl;

	////while (firstNum >= secondNum) <<-- Dont use it like that

this way would be better,

int i = firstNum;
int sum = 0, sum2 = 0;
While ( i < secondNum )
{
if ((i %2) == 0)// for odd if (i%2 == 1) is odd
sum +=i; //<-- sum all the even number, if you want odd
else// the if its not even then it is odd
{
cout<< i << " ";
sum2+= i*i;// for the square
}
i++;
}

What would i initialize second num as ?

the firstNum and the second are constat as soon as the user input them.
so no worry about initlizing them,
but you initialize the i just to make sure you use the i to go throw all the number from the firstNum and thal last.
then you dont have to change them.
so you can use them later if you want.

I feel really stupid perhaps im just not understanding what is being put here

#include <iostream>

using namespace std;

int main()
{
	int i = firstNum;
	int sum = 0, sum2 = 0;

	cout << "Please Enter 2 Integers: ";
	cin >> firstNum >> secondNum;
	cout << endl;

	while (i < secondNum)
	{
		if ((i %2) == 0)
		if (i%2 == 1)
			sum +=i; 
		{
			cout<< i << " ";
			sum2+= i*i;// for the square
		}
		i++;
	}
	return 0;
}

very skeletal but working, on it ^^; another tip ?

did you understand the code I have written.
its just need time to be familiar with everyththing.
and I rememebr my self being t=in the same spot. I hade 2 corces in C++. so these stuff look easy. but now am trying to get familiar to the stuff we are studying.

#include <iostream>

using namespace std;

int main()
{
    int i;
    int sum = 0, sum2 = 0;

    cout << "Please Enter 2 Integers: ";
    cin >> firstNum >> secondNum;
    cout << endl;

                i = firstNum; // <== Should be here so the input value goes here.

    while (i < secondNum)
    {
        if ((i %2) == 0)
        if (i%2 == 1)
            sum +=i; 
        {
            cout<< i << " ";
            sum2+= i*i;// for the square
        }
        i++;
    }
    return 0;
}

Im getting alot more errors with your coding advice then i was originally perhaps im misinterpreting

#include <iostream>

using namespace std;

int main()
{
	int i;
	int sum = 0, sum2 = 0;
	
	i = firstNum;
	
	while (i < secondNum)
	{
		cout << "Please enter 2 integers, the first must be smaller then the second: ";
		cin >> firstNum >> secondNum;
		cout << endl;
		
		if ((i %2) == 0)
			if (i%2 == 1)
				sum +=i;
		{
			cout<< i << " ";
			sum2+= i*i;
		}
		i++;
	}
return 0;
}

Write a program that uses while loops and performs the following

Prompts the user to input 2 integers firstNum and secondNum // Easy

Output all odd numbers between firstNum and secondNum // No idea how to start that.

Outputs the sum of all even numbers between firstNum and secondNum

Output the numbers and their squares between 1 and 10

Output the sum of the square of the Odd Numbers between firstNum and secondNum

Output ALL Uppercase letters***.

*Bump*

Im at a roadblock

#include <iostream>

using namespace std;

int main()
{
	int i;
        int firstNum, secondNum;
        int evensum = 0;
        int oddsum = 0;

	cout << "Please enter 2 integers: ";
	cin >> firstNum >> secondNum;  //moved this here from
                                 //the loop so i got a value
        
        if (firstNum < secondNum)   //first num is smaller 
                                      //so i is first num
	    i = firstNum;                      
        else                       //else second num is smaller
          {
           i=secondNum;             //sets i to smaller number
           secondNum=firstNum;  //sets second number to larger number
           firstNum=i;         //copies smaller number from i
           }
	
	while (i <= secondNum) //needs to be <= so you get last number
	{		
		if ((i %2) == 0)           //if number is even
                  evensum += i;
		else              //not even, so it's odd
		  oddsum += i;
		i++;
	}
return 0;
}

There, I fixed the code you had and commented the fixes so you could follow. That should give you a start. It gets you the even/odd sum. If you need more help, let me know.

good job.
I did not test my code tho.

Output the numbers and their squares between 1 and 10

here's a way you can do this:

int x=1;
    cout<<"Squares of numbers between 1-10"<<endl<<endl;
    while(x<=10)
    {
     cout<<"Square of "<<x<<" is "<<x*x<<endl;
     x++;
    }

please analyze the code so you yourself could learn.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.