Thread: nested loops

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    6

    nested loops

    I don't know much about programming but I was trying to teach myself some from my sisters notes and there is an assignment where you use nested loops to get this

    1
    24
    135
    2468
    13579
    24681012

    i can not seem to figure it out i have completed similar ones like

    2
    24
    246
    2468

    and

    13579
    1357
    135
    13
    1

    can someone help me with it please?

  2. #2
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    Quote Originally Posted by flysurfer View Post
    i have completed similar ones like

    2
    24
    246
    2468

    and

    13579
    1357
    135
    13
    1
    Good start, lets refine your code whatever you have already tried a little bit. Try changing the patter from

    2
    24
    246
    2468

    to

    24
    2468
    24681012

    and then try the same for creating the pattern

    1
    135
    13579

    And then try to combine those two logic, you will get your required pattern.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Yep, you should be able to build on what you did before. I guess that
    2
    24
    246
    2468

    looked something like:
    Code:
       int i, j;
       for (i = 1; i <= 4; i++)
       {
          printf("\n");
          for (j = 2; j <= i*2; j+=2)
          {
             printf("%d", j);
          }
       }
    The other one is just the same, with the added fun of having to alternate between 1 and 2 for starting numbers. That can be done by using the modulo operator when initialising the inner loop counter.

    There are probably a bunch of ways of doing this - you could for instance have 2 inner for loops and switch between them depending on which row you're on. You can get it all done with 2 for loops, but it is a bit fiddly to read. I think trying to combine your previous 2 exercises is a good idea.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    Code:
    main()
    int a,b,i,j;
    for(a=1,i=4;i<=12;i=i+4,a=a+4)
    {
    for(b=1;b<=a;b=b+2)
    {
    printf("%d",b);
    }
    printf("\n");
    for(j=2;j<=i;j=j+2)
    {
    printf("%d",j);
    }
    printf("\n");
    }
    getch();
    }

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    hey thanks for the help i will try it was thinking of using something like a if else for it but wasn't to sure about it

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Here's how I did it:

    Code:
    	for (j = 0; j < 6; j++)
    	{
    		printf("\n");
    		for (i = (j % 2)+1; i <= (j+1)*2; i+=2)
    		{
    			printf("%d", i);
    		}
    	}

    I would probably use an if/else.

    Code:
    	for (j = 1; j < 7; j++)
            {
                 if ((j % 2) == 1)
                    // loop for starting with a 1
                 else
                    // loop for starting with a 2
            }
    Last edited by smokeyangel; 10-19-2012 at 05:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested For loops
    By Dagda in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2012, 01:45 PM
  2. nested four loops
    By begginer in forum C Programming
    Replies: 12
    Last Post: 02-25-2011, 11:21 PM
  3. Help nested for loops
    By dals2002 in forum C Programming
    Replies: 14
    Last Post: 03-14-2008, 01:18 AM
  4. nested for loops
    By akub3 in forum C Programming
    Replies: 2
    Last Post: 04-01-2004, 06:21 AM
  5. nested for loops/
    By o0o in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 10:19 AM