I have a part of code that cannot show the result as shown below:

patter#1
*****
****
***
**
*
pattern2
*
**
***
****
*****
pattern3
*****
****
***
**
*

Any idea to solve the incorrect code below:

#include <iostream.h>
using namespace std;
int main(){
int i=0 ;
int j=0 ;
for(i=0;i<5;i++){
for(j=0;j<1;j++){
cout<<" ";
}
for(j=0;j<5;j++){
cout<<"*";
cout<<endl;
}
}


i=0;
j=0;
for(i=0;i<5;i++){
for(j=i;j<4;j++){
cout<<" ";
}
for(j=0;j<1;j++){
cout<<"*";
}
cout<<endl;
}


i=0;
j=0;
for(j=0;j<5;j++)
for(i=0;i<4;i++){
cout<<" ";
}
for(i=0;i<1;i++){
cout<<"*";
}
cout<<endl;
system("pause");
}

Try to simplify your algorithms. Remember that you can combine loop types. This is often useful ;) . Also don't forget the code tags when you post code, because with them it's much more readable. One possible solution I paste below. Enjoy.

#include <iostream>

using namespace std;

int main(void)
{
    short count=5;
    
    while(count) {
                 for(int i=count; i!=0; i--)
                         cout<<'*';
                 cout << endl;
                 --count;
    }
    
    cout << endl;
    count = 1;
    
    while(count < 6) {
                for(short i=0;i!=count;i++) 
                        cout << '*';
                
                cout << endl;
                ++count;
    }
    
    return 0;
}

Your main probleme is that you have bad design. Try to figure out how to do somthing before you try to code it :).
But, I rewritten your code:

#include <iostream> // use <iostream> instead for <iostream.h>
using namespace std;
int main()
{
    int i=0 ;
    int j=0 ;
    for(i=0;i<5;i++)
    {
        for(j=i;j<5;j++){ // j = 0 changed to j = i
            cout<<"*";
        }
        cout<<endl;//you dont want a newline after every *
    }
    
    for(i=0;i<5;i++)
    {
        for(j=0;j<i+1;j++)//  j < 1 changed to j < i+1 
        {
            cout<<"*";
        }
        cout<<endl;
    }

    for(j=0;j<5;j++)
    {

        for(i=j;i<5;i++) //  i=0;i<1 changed to i=j;i<5
        {
            cout<<"*";
        }
        cout<<endl;
    }   
    
    system("pause");
}

but I recomand you to use function, makes the code look mutch more clean. (im mine eyes at least)
somthing like:

#include <iostream>
using namespace std;

void patern1 ()
{
    for(int j = 0; j < 5; j ++) 
    {
         for (int i = j; i < 5; i++)
         {
             cout << '*';
         }
         cout << endl;         
    }
}

void patern2 ()
{
    for(int j = 0; j < 5; j ++) 
    {
        for (int i = 0; i < j+1; i++)
        {
            cout << '*';
        }
        cout << endl;        
    }     
}

int main()
{
    patern1();
    patern2();
    patern1();
    system("pause");
}

I would recommend that you don't use a system("pause").

I only recommanded that he used function, the system("pause ") was only becuse mixsir used it in his code.

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.