Sunday, January 1, 2012

Print following pattern. 1 01 101 0101 in 'C'


1
01
101
0101

Example

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("Enter the value of n:");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=1;j<i;j++)
{
      if((i+j)%2==0)
      {
printf("0");
      }
else
{
printf("1");
}
}
printf("\n");
}
getch();
}
/*=====================================================================
OUTPUT
Enter the value of n:5

1
01
101
01010
*/

9 comments:

  1. just after scanf do one thing.. increse the value of n by 1...like just write n++.. and it will run 5 times

    ReplyDelete
  2. Thank u for helping us vasanta & aparna
    Raipur CHHATTISGARH

    ReplyDelete
  3. in 1st loop use i<=n+1 for correct times loop
    such as if u enter 5 then 5 lines will print

    ReplyDelete
  4. Java code:

    package _1_10;
    import java.util.Scanner; ;
    public class Bin {
    static Scanner in = new Scanner(System.in);
    public static void main(String[] args) {
    int n = in.nextInt() ;
    for( int i = 1 ; i <= n ; i ++ )
    {
    for( int j = 1 ; j <=i ; j ++ )
    {
    if((i+j)%2==0)
    System.out.print(1+" ");
    else
    System.out.print(0+" ");
    }
    System.out.println();
    }
    }

    }

    ReplyDelete
  5. Thank you for your help at the right moments

    ReplyDelete