Program to print number sequence in C++ (2)

 /*  Program to print sequence

                                 1
                                101
                               10101
                              1010101
                               10101
                                101
                                 1
     */

#include<stdio.h>
#include<iostream>
main()
    {
        int i,p=4,a=1,k,j;
        for(i=1;i<=4;i++)
        {
            a=1;
            for(k=1;k<=p;k++)
                std::cout<<" ";
            p--;
            for(j=1;j<=i;j++)
            {
                std::cout<<a;
                if(a==0)
                    a=1;
                else
                    a=0;
            }
            for(j=i-1;j>=1;j--)
            {
                std::cout<<a;
                if(a==0)
                    a=1;
                else
                    a=0;
            }
            std::cout<<"\n";
        }
        p=2;
        for(i=3;i>=1;i--)
        {
            a=1;
            for(k=1;k<=p;k++)
                std::cout<<" ";
            p++;
            for(j=1;j<=i;j++)
            {
                std::cout<<a;
                if(a==0)
                    a=1;
                else
                    a=0;
            }
            for(j=i-1;j>=1;j--)
            {
                std::cout<<a;
                if(a==0)
                    a=1;
                else
                    a=0;
            }
            std::cout<<"\n";
        }
    }        

Top