Thread: for loop

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2022
    Posts
    3

    Post for loop

    hello guys,
    I'm trying to do a shape of star.
    I have to do print line 1-4 again in lines 6-9.
    Total 9 lines.

    // line 6 same like line 4
    // line 7 same like line 3
    // line 8 same like line 2
    // line 9 same like line 1


    Can anyone tell me how to do it without back every line again.

    Here's the link to my code :
    GDB online Debugger | Code, Compile, Run, Debug online C, C++

    Thanks a lot

    Code:
    #include <stdio.h>
    
    int
    main ()
    {
      int s, i, j;
    
      // for star
      printf ("Enter size (10-20)\n");
      scanf (" %d", &s);
      for (i = 1; i < 2; i++)
        {
          for (j = 1; j <= 2 * s - 1; j++)
    	{
    	  if (j == s)
    	    printf ("o");
    	  else
    	    printf ("-");
    
    	}
          printf ("\n");
        }
      for (i = 2; i < 3; i++)
        {
          for (j = 1; j <= 2 * s - 1; j++)
    	{
    	  if (j == s + 1 || j == s - 1)
    	    printf ("o");
    	  else
    	    printf ("-");
    	}
          printf ("\n");
        }
      for (i = 3; i < 4; i++)
        {
          for (j = 1; j <= 2 * s - 1; j++)
    	{
    	  printf ("o");
    	}
          printf ("\n");
        }
    
      for (i = 4; i < 5; i++)
        {
          for (j = 1; j <= 2 * s - 1; j++)
    	{
    	  if (j == 2 * s - 2 || j == 2)
    	    printf ("o");
    	  else
    	    printf ("-");
    	}
          printf ("\n");
        }
      for (i = 5; i < 6; i++)
        {
          for (j = 1; j <= 2 * s - 1; j++)
    	{
    	  if (j == 2 * s - 3 || j == 3)
    	    printf ("o");
    	  else
    	    printf ("-");
    	}
          printf ("\n");
        }
      // line 6 same like line 4
      // line 7 same like line 3
      // line 8 same like line 2
      // line 9 same like line 1
    }
    Attached Files Attached Files
    Last edited by Salem; 12-15-2022 at 02:09 PM. Reason: Inlined the code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-22-2016, 09:08 AM
  2. Replies: 4
    Last Post: 11-05-2015, 03:29 PM
  3. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  4. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  5. Replies: 23
    Last Post: 04-05-2011, 03:40 PM

Tags for this Thread