Write a C++ program that should ask the user to input the final range up to which the loop should run and then prints all the even numbers from zero (0) to that final value of the range, like
cpp programming exercise with solution |
By using for loop
#include <iostream>
using namespace std;
int main()
{
int rang;
cout<<"Please enter a range upto which loop should run: ";
cin>>rang;
for (int i = 0; i<= rang; i++)
{
if (i % 2 == 0)
{
cout<<i<<endl;
}
}
return 0;
}
By using while loop
#include <iostream>
using namespace std;
int main()
{
int rang;
int i = 0;
cout<<"Please enter a range upto which loop should run: ";
cin>>rang;
while (i <= rang)
{
if (i % 2 == 0)
{
cout<<i<<endl;
}
i++;
}
return 0;
}