I need your help

Discussion in 'C' started by the white soul, Apr 1, 2010.

  1. the white soul

    the white soul New Member

    Joined:
    Apr 1, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    hello guys

    this is my first post in here, I hope you will help me guys


    I tried many time but I couldn't get the required pattern


    write a c program to generate the
    pattern shown using nested for loop

    1
    01
    101
    0101


    and



    0
    12
    345
    6789
     
  2. karthigayan

    karthigayan New Member

    Joined:
    Feb 19, 2010
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    I think this is your home work . Try by your own . If you struck any where then post here .
     
  3. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    Take it step by step try to find some 'pattern' that is there in the output.Try to from a logic on how you could print that than put something over here.Its not that difficult but try yourself.
     
  4. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    this will give you the opposite from what you want in 1)
    do the rest by yourself

    (hint:you must add 1 char and 1 number in order to work as you want...but where?)


    Code:
    #include <stdio.h>
    
    int main(){
        for (int i=0;i<4;i++){
            printf("\n");
            for (int j=0;j<(i+1);j++)
                printf("%d",(i+j)%2);
        }
    getchar();
        return 0;
    }
    

    output
    ---------


    0
    10
    010
    1010



    with this try and 2)
     
  5. the white soul

    the white soul New Member

    Joined:
    Apr 1, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Thank you virxen so much

    I tried the second but I couldn't get the right answer

    Code:
    #include <stdio.h>
    
    int main(){
    	 for (int i=0;i<4;i++){
            printf("\n");
    		  for (int j=0;j<(i+1);j++)
    				printf("%d",(i+j+1));
        }
    getchar();
    	 return 0;
    }
    
    
    and I got the output as

    1
    23
    345
    567

    instead of getting

    1
    23
    345
    6789

    can anyone help me to correct my answer
     
  6. the white soul

    the white soul New Member

    Joined:
    Apr 1, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    sorry this is the code
    Code:
    #include <stdio.h>
    
    int main(){
    	 for (int i=0;i<4;i++){
    		  printf("\n");
    		  for (int j=0;j<(i+1);j++)
    				printf("%d",(i+j));
    	 }
    getchar();
    	 return 0;
    }
    
    

    and the output was
    0
    12
    234
    3456


    instead of getting

    0
    12
    345
    6789


    what's wrong in the code so I can get the right answer
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Your code displays on each line values from i (which increases by 1 each new line) to i+j. But you want each line to begin with the last number displayed, plus one.

    So how about a counter that just increases each time you print something? Then print the counter instead of trying to calculate something from i and j.
     
  8. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    Because you tried to code i will give you the answer for a,b.

    Code:
    #include <stdio.h>
    
    int main(){
        for (int i=0;i<4;i++){
            printf("\n");
            for (int j=0;j<(i+1);j++)
                printf("%d",(i+j+1)%2);
        }
    getchar();
    int count=0;
        for (int i=0;i<4;i++){
            printf("\n");
            for (int j=0;j<(i+1);j++)
                printf("%d",count++);
        }
    getchar();
        return 0;
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice