Example : A C++ program to display all the values from 1 to 10 using for loop.
# include <iostream>
using namespace std;
int main ()
{
for( int i = 1; i <= 10; i = i + 1 )
{
cout << "The values are : " << i <<endl;
}
return 0;
}
-------------- OR --------------
# include <iostream>
using namespace std;
int main ()
{
int j=10;
for( int i = 1; i <= j; i = i + 1 )
{
cout << "The values is: " << i <<endl;
}
return 0;
}
Output :
The values is: 1
The values is: 2
The values is: 3
The values is: 4
The values is: 5
The values is: 6
The values is: 7
The values is: 8
The values is: 9
The values is: 10
Example : A C++ program to display all the values from 1 to 10 using while loop.
# include <iostream>
using namespace std;
int main ()
{
int i = 1;
while(i <= 10 )
{
cout<<"The values is : " << i <<endl;
i = i + 1 ;
}
return 0;
}
Example : A C++ program to display all the values from 1 to 10 using do-while loop.
# include <iostream>
using namespace std;
int main ()
{
int i ;
i=1;
do
{
cout << "The values is : " << i <<endl;
i = i + 1 ;
} while(i <= 10 );
return 0;
}
Example : A C++ program to display all the values from given two values of the interval of 2.
# include <iostream>
using namespace std;
int main ()
{
int min, max;
cout<<" Enter any two values to display all the numbers : \n";
cin>>min>>max;
if(min<max)
{
for( int i = min; i <= max; i = i + 2 )
{
cout << "The values are : " << i <<endl;
}
}
else
{
for( int i = min; i >= max; i = i - 2 )
{
cout << "The values are : " << i <<endl;
}
}
return 0;
}
Output :
Enter any two values to display all the numbers :
20
10
The values are : 20
The values are : 18
The values are : 16
The values are : 14
The values are : 12
The values are : 10
--------------------------------
Process exited after 10.91 seconds with return value 0
Press any key to continue . . .
Example : A C++ program to display all the values, from given two values by the user dynamically.
# include <iostream>
using namespace std;
int main ()
{
int min, max;
cout<<"Enter the first Minimum and second Maximum value to display all the numbers : \n";
cin>>min>>max;
for( int i = min; i <= max; i = i + 1 )
{
cout << "The values are : " << i <<endl;
}
return 0;
}
Output :
Enter the first Minimum and second Maximum value to display all the numbers :
10
20
The values are : 10
The values are : 11
The values are : 12
The values are : 13
The values are : 14
The values are : 15
The values are : 16
The values are : 17
The values are : 18
The values are : 19
The values are : 20
Example : A C++ program to display all the values from 10 to 1 using for loop.
# include <iostream>
using namespace std;
int main ()
{
for( int i = 10; i >= 1; i = i - 1 )
{
cout << "The values is : " << i <<endl;
}
return 0;
}
Output :
The values is : 10
The values is : 9
The values is : 8
The values is : 7
The values is : 6
The values is : 5
The values is : 4
The values is : 3
The values is : 2
The values is : 1
Example : A C++ program to display all the values, from given any two user’s values, using for loop.
# include <iostream>
using namespace std;
int main ()
{
int min, max;
cout<<" Enter any two values to display all the numbers : \n";
cin>>min>>max;
if(min<max)
{
for( int i = min; i <= max; i = i + 1 )
{
cout << "The values are : " << i <<endl;
}
}
else
{
for( int i = min; i >= max; i = i - 1 )
{
cout << "The values are : " << i <<endl;
}
}
return 0;
}
Example : A C++ program to display all the values using nested homogeneous for loop.
# include <iostream>
using namespace std;
int main ()
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
cout<<i<<" "<<j<<"\n";
}
cout<<"\n";
}
return 0;
}
Example : A C++ program to display all the values using nested heterogeneous loop.
# include <iostream>
using namespace std;
int main ()
{
for(int i=1;i<=3;i++)
{
int j=1;
while(j<=3)
{
cout<<i<<" "<<j<<"\n";
j=j+1;
}
cout<<"\n";
}
return 0;
}
Output :
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
0 Comments