While loop problem for beginners

Hi everyone,

I just started learning c++. Since, it is common to have numerous doubts to arise when learn to code. Same goes with me...I got stuck while learning the While Statement...the problem is down below...I didn't even understand the question ...plz help me understand it and with solution too...

Question : Write a program that prompts the user for two integers. Print each number in the range specified by those two integers.

Thanks

#include
using namespace std;

int main() {
int a,b;
cin >> a >> b;
int i=a;
while(i>=a && i<=b){
cout << i << " ";
i++;
}
return 0;
}

you can use long long int if the starting and ending nos. are bigger than 10^6.

Here is the code for your problem. :slight_smile:

It’s just asking you to input two integers. Then print each number in the range, for example let’s say two integers are 5 and 8, then your output should be 5, 6, 7, 8 if the range are inclusive or 6,7 if the range is exclusive.

Below is the code, if range is inclusive.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int num1, num2;
    cin>>num1>>num2;
    while(num1 <= num2){
        cout<<num1<<" ";
        num1++;
    }
}

And this is the code if it is exclusive.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int num1, num2;
    cin>>num1>>num2;
    while(num1 < num2 - 1){
        num1++;
        cout<<num1<<" ";
    }
}

Thank you very much…u really reduced my burden which was for the last two days…u know I was looking for answers everywhere…but couldn’t find…thanks bro…

and how to learn c++ effectively and which is the best resource to learn it…

Thank you very much for explaining elaborately …

Search Youtube for C++ Bootcamp, Go with freecodecamp.org video. It’s great.
You are Welcome :slight_smile: