0

I have to make a program for generating the following output:

Example:

 & & & & & & &
   & & & & &
     & & &    
       &

What I have done so far:

#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    int i,j,k,n;
    cout<<"How many rows?\n";
    cin>>n;
    for(i=n;i>0;i-=2)
    {
        cout<<"\n";

        for(k=(i+1)/2;k>0;--k)
        cout<<" ";

        for(j=1;j<=i;++j)
        cout<<"&";
    }
}

What the output comes:

    & & & & &
  & & &
&

Please correct me where I am making the mistake. Any help will be appreciated. Thanks.

3 Answers 3

1

Your first try seems a bit complicated. I'd do something like this:

#include <iostream>

int main()
{
    int i,j,k,n;
    std::cout << "How many &'s in the start row?\n";
    std::cin >> n;
    std::cout << std::endl;
    for(i=0; i < n; i+=2)
    {
        for(k=0 ; k < i; k++ )
        {
                std::cout << " ";
        }
        for(j=0; j<(n - i); ++j)
        {
                std::cout<<" &";
        }
        std::cout << std::endl;
    }
    return (0);
} 
Sign up to request clarification or add additional context in comments.

5 Comments

compiler is giving "Declaration Syntax Error" in the first line
You are running an awfully old compiler. Change the include back to the old #include<iostream.h> and remove all the "std::"'s.
can u give me some link to download a better and improved version for running C++. I have windows 8.
Why don't you use the excellent VS 2012 express for Desktop (microsoft.com/visualstudio/deu/downloads) or the great GCC.
@ManojPandey It would be great, very relieving and a very good practice to also accept the satisfying answer.
0

Better late than never. Try this simple solution :

#include <iostream>
using namespace std;

int main()
{
  char ch = '&';
  for(int i = 1; i <= 4; i++)
  {
    for(int j = 1; j <= 3; j++)
    {
        if( i > j)
            cout << " ";
        else
            cout << ch;   
    }

    cout << ch;

    for(int j = 3; j >= 1; j--)
    {
        if( i > j)
            cout << " ";
        else
            cout << ch;   
    }

    cout << "\n";
  }

  return 0;
}

1 Comment

Seems like the provided answer and comments have already solved this old problem.
0
import java.util.*;
import java.lang.*;
import java.io.*;
class Design{
     public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        for(int i=0;i<4;i++)
        {
            for(int k=0;k<i;k++)
            {
                System.out.print(" ");
            }
            for(int j=i;j<7-i;j++)
            {
                System.out.print("&");
            }
            System.out.println();

        }
    }

}

2 Comments

Consider adding an explanation for what the problem was that you are solving and how this code accomplishes that.
its better to provide some tech details like how its working instead of just posting code only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.