(Drawing Patterns with Nested for Loops) Write a program that uses for statements to
print the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form cout << '*'; (this causes the asterisks to print side by side).
Solution:a)
#include < iostream>
using namespace std;
int main()
{
for( int i = 1; i <= 10; i++ )
{
for( int j = 1; j <= 10; j++)
{
cout << (j <= i ? "*" : " " ) ;
}c
out << endl;
}
//for pause
system("PAUSE") ;
return 0;
}
b)
#include < iostream>
using namespace std;
int main()
{
for( int i = 10; i >= 1; i‐‐ )
{
for( int j = 1; j <= 10; j++)
{
cout << (j <= i ? "*" : " " ) ;
}
cout << endl;
}
system("PAUSE") ;
return 0;
}
c)
#include < iostream>
using namespace std;
int main()
{
for( int i = 10; i > 0; i‐‐ )
{
for( int j = 10; j > 0; j‐‐)
{
cout << (j <= i ? "*" : " " ) ;
}c
out << endl;
}
//for pause
system("PAUSE") ;
return 0;
}
d)
#include < iostream>
print the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form cout << '*'; (this causes the asterisks to print side by side).
[Hint: The last two patterns require that each line begin with an appropriate number of blanks. Extra credit: Combine your code from the four separate problems into a single program that prints all four patterns side by side by making clever use of nested for loops.]
(Drawing Patterns with Nested for Loops) Write a program that uses for statements to
print the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form cout << '*'; (this causes the asterisks to print side by side).
print the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form cout << '*'; (this causes the asterisks to print side by side).
[Hint: The last two patterns require that each line begin with an appropriate number of blanks. Extra credit: Combine your code from the four separate problems into a single program that prints all four patterns side by side by making clever use of nested for loops.]
Solution:a)
#include < iostream>
using namespace std;
int main()
{
for( int i = 1; i <= 10; i++ )
{
for( int j = 1; j <= 10; j++)
{
cout << (j <= i ? "*" : " " ) ;
}c
out << endl;
}
//for pause
system("PAUSE") ;
return 0;
}
b)
#include < iostream>
using namespace std;
int main()
{
for( int i = 10; i >= 1; i‐‐ )
{
for( int j = 1; j <= 10; j++)
{
cout << (j <= i ? "*" : " " ) ;
}
cout << endl;
}
system("PAUSE") ;
return 0;
}
c)
#include < iostream>
using namespace std;
int main()
{
for( int i = 10; i > 0; i‐‐ )
{
for( int j = 10; j > 0; j‐‐)
{
cout << (j <= i ? "*" : " " ) ;
}c
out << endl;
}
//for pause
system("PAUSE") ;
return 0;
}
d)
#include < iostream>