Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Code for Range of two integers in C++, Please Help

+1 vote
asked Jan 29, 2019 by anonymous
I tried to code in c++ to get the range of two integers, but unfortunately it didn't show the range, just the input of two numbers. Kindly help me if there is something wrong in my code. TIA

#include <iostream>
using namespace std;

int a, b;

int main(){
    cout << "Input your first number: ";
    cin >> a;
    cout << "Input your second number: ";
    cin >> b;

    for (int i = a; i<=b; i++){
        cout << "Range of two integers are: " << endl;
        cout << i;
    }
    
    return 0;
}

2 Answers

0 votes
answered Jan 30, 2019 by metal gamer (560 points)
I SOLVE YOUR ISSUE.

#include <iostream>
using namespace std;
int main()
{
    int a, b, i;
    cout << "Input your first number: ";
    cin >> a;
    cout << "Input your second number: ";
    cin >> b;
   for ( i = a; i<=b; i++)
   cout << "Range of two integers are: " << endl;
   cout << i;
   return 0;
}
0 votes
answered Jan 30, 2019 by anonymous
I'm assuming you want to print the numbers from first input to second input including both. If that is the case, the following should work

#include <iostream>
using namespace std;

int a, b;

int main(){
    cout << "Input your first number: ";
    cin >> a;
    cout << "Input your second number: ";
    cin >> b;

    cout << "Range of two integers are: " << endl;
    for (int i = a; i<=b; i++){
        cout << i << endl;
    }
    
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...