Member Avatar for Member #956535

How to write a program using nested for loop to display the following output:

0
* *
0 1 2
* * * *
0 1 2 3 4
* * * *
* * *
* *
*

Dani AI

Generated

You can think of the required output as two triangles back to back. Top half: 1..rows items, where even-indexed rows (0,2,4,...) print numbers 0..i and odd rows print only asterisks. Bottom half: a mirror that prints only asterisks from rows-1 down to 1. That matches what described, and the second triangle echoes the idea in ’s star-only loop.

Here is a small, parameterized version that avoids trailing spaces and makes the intent obvious. Set rows to 5 to reproduce the sample (numbers go 0..4).

#include <iostream>
#include <sstream>

void print_numbers(int n) {
    std::ostringstream oss;
    for (int k = 0; k < n; ++k) {
        if (k) oss << ' ';
        oss << k;              // use k+1 if you want 1..n instead of 0..n-1
    }
    std::cout << oss.str() << '\n';
}

void print_stars(int n) {
    for (int k = 0; k < n; ++k) {
        if (k) std::cout << ' ';
        std::cout << '*';
    }
    std::cout << '\n';
}

int main() {
    const int rows = 5;        // widest line length
    for (int i = 0; i < rows; ++i)
        (i % 2 == 0) ? print_numbers(i + 1) : print_stars(i + 1);
    for (int i = rows - 1; i >= 1; --i)
        print_stars(i);
}

Tips:

  • If your output is shifted, check for extra spaces like cout << "* "; the functions above avoid a trailing space.
  • Use zero-based thinking: line i has i+1 items. That is why the largest numeric line shows 0..4 when rows == 5, which matches the sample.
  • Prefer '\n' over std::endl for speed, and avoid system("pause") as would note it is not portable.

Recommended Answers

All 12 Replies

We will not do your homework for you. Have you tried? What code do you have so far? Do you have a specific problem? You have to show some efforts before we can help.

this is the part of programe where the * are printed and u have to reverses it as per ur choice

for(i=0;i<4;i++)
{
    for(j=0;j<4-i-1;j++)
    {
    printf("* ")
   }
   printf("\n")
 }

Here's the pseudocode:

SIZE<-10
subalg prints(){
    for (i<-0, i<SIZE/2) do
        for (j<-0, j<=i) do
            if i%2=0 then
                print j
            else
                print '*'
            end_if
        end_for
        print new line
    end_for
    for (i<-SIZE/2-1, downto 0) do
        for (j<-0, j<=i) do
            print '*'
        end_for
        print new line
    end_for
end_subalg
Member Avatar for Member #956535

this is what i have, but understand anything

#include <iostream>

using namespace std;

int main()
{  

for(int a = 0; a <= 4; a++)
{
 for(int b = 0; b <= a; b++)
 { 
 if(a % 2 == 0)
 {
 cout << b << " ";
 }
  else

  cout << "* "; 
  }
    cout << endl;

}

for(int c = 0; c <= 4; c++)
{
   for(int d = 4; d > c; d--)
    { 
      cout <<"* ";
    } 
      cout << endl;    
    } 

   system("pause");
   return(0);  
}
>
> for(int a = 0; a <= 4; a++)
> {
>  for(int b = 0; b <= a; b++)
>  { 
>  if(a % 2 == 0)
>  {
>  cout << b << " ";
>  }
>   else
>   cout << "* "; 
>   }
>     cout << endl;
> }
>

This is the upper part of the requirement. Here you'll print asterix if the row is odd, or you'll print all the numbers which are smaller than the current number of the row, if the row is even.
In the lower part of the requirement you'll just print out the asterixes, in reverse order.

how i can make game in C++

include <iostream>

using namespace std;
int main()

for(int a = 0; a <= 4; a++)

#include <iostream>
using namespace std;
int main()
{  
for(int a = 0; a <= 4; a++)
{
 for(int b = 0; b <= a; b++)
 { 
 if(a % 2 == 0)
 {
 cout << b << " ";
 }
  else
  cout << "* "; 
  }
    cout << endl;
}
}
for(i=0;i<4;i++)
{
    for(j=0;j<4-i-1;j++)
    {
    printf("* ")
   }
   printf("\n")
 }
> for(int a = 0; a <= 4; a++)
> {
>  for(int b = 0; b <= a; b++)
>  { 
>  if(a % 2 == 0)
>  {
>  cout << b << " ";
>  }
>   else
>   cout << "* "; 
>   }
>     cout << endl;
> }
>

The dead post - !
... It - it lives again! What have you done??!

That which was dead should have stayed dead! The grave will not be lightly robbed her rightful prize.

You fools! You've doomed us all!

Oops, didn't see the flagrant silly repeats by umer 4!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.