Not getting requierd output in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amarniit
    New Member
    • Sep 2009
    • 13

    Not getting requierd output in C

    I need to get this out put but i am noit getting please help me...... i need its coding


    1
    01
    101
    0101
    10101
    010101
    1010101



    What i have write it is ............... ......
    #include<stdio. h>
    #include<conio. h>
    void main ()
    {
    int i,j,limit=10,n= 1;
    for(i=1;i<=limi t;i=i+2)
    {
    for(j=1;j<=i;j+ +)
    {
    if(n==1)
    {
    printf("%d",n);
    n=0;
    }
    else
    {
    printf("%d",n);
    n=1;
    }
    }
    printf("\n\n");
    }
    getch();
    clrscr();
    }




    output of this
    1
    010
    10101
    0101010
    101010101
    but i need this

    1
    01
    101
    0101
    10101
    010101
    1010101
    Last edited by amarniit; Feb 18 '10, 04:45 PM. Reason: some program codeing
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I can't give you the code. All I can do is help you out where you are stuck.

    What you need here is a loop and a variable initialized to 1.
    Inside that loop you display a variable as many times as the loop counter. That is, when the loop counter is 3 you display the variable 3 times.

    After you display the variable, check its value. If it is 1 set it to 0 and if it is 0 set it to 1 for the next display.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      To marginally elucidate on the above:
      If you could employ C++ strings you could manipulate them quite simply as follows
      If s1="You are a big brute" and s2="and so am I" then if s3=s1+s2 s3 would read "You are a big brute and so am i"
      In the context of this problem:
      If s0='0' and s1='1' then cout<< s1 prints 1
      if s2=s0+s1 cout<<s2 prints 01
      if s3 =s1+s2 cout<<s3 prints 101
      if s4=s0+s3 cout<<s4 prints 0101
      if s5........and so on.
      If the above was in the body of a for loop and 'i' cold be made to echo the desired s index s[i] would be assigned the value of s0 or s1+ s[i-1]
      Then the only problem left would be how to identify which of the alternating s0 or s1 is to be used.
      This could be arranged by declaring (say) flag = 1 above the for loop and towards the end of the for loop body a statement such as if(flag==1)flag ==0 else flag ==1.
      Now s0 and s1 become s[flag] being equal to either 0 or 1.
      comprenez vous?

      Comment

      • abghosh
        New Member
        • Feb 2010
        • 7

        #4
        Run this loop:
        For(i = 1; i <= Limit; i++)
        for(j = 0; j < i; j++)
        printf("%d", (i - j) % 2);
        printf("\n");
        }

        Hope this help.

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          It might save a lot of angst if you ignore #3 above.
          Instead use:
          getline(cin ,s) to store say(30) 1010101...`s in s.
          or you can generate s with a for loop body containing if(i%2==0)cout< <1;else cout<<0.
          s now holds a string (30) 1010101`s.
          if you cout<<s you get 101010101010101 010101010101010 .
          if you call s.length you get 30
          If you use a for loop with condition i<s.length/2 with cout<<substr(i, i+1) in its body
          you get:
          1
          01
          101
          0101 etc
          much less agro.

          Comment

          Working...