Home > front end >  How to print this pattern? can anyone explain logic?
How to print this pattern? can anyone explain logic?

Time:09-22

Star pattern

Anyone can make logic for this pattern? I tried using 2 for loops also. I just had an interview where I was asked to create star pattern as shown in picture.

#include <stdio.h>
int main()
{
   char f;
    int n;
    printf("Enter the number:");
    scanf("%d", &n);
    int i, j, s, k, l, m;
    for (s = 1; s <= n; s  ) //printing
    {
        for (k = 1; k <= n; k  )
        {
            for (i = 1; i <= n; i  ) //pattern
            {
                for (j = 1; j <= n; j  )
                {
                    if ((i   j == n   1)) //star positions
                    {
                        printf("* ");
                    }
                    else
                    {
                        printf(" ");
                    }                
                }
                printf("\n");   //next line
            }            
        }        
    }
    return 0;
}

CodePudding user response:

C Code for printing this patteren using two for-loop would be

#include<stdio.h>
int main()
{
    int n;
    printf("Enter pattern length :");
    scanf("%d",&n);
    int matrix=n*n;
    for(int i=1;i<=matrix;i  )
    {

        for(int j=1;j<=matrix;j  )
        {
           if((i j-1)%n==0)
           {
               printf("*");
           }
           else
           {
              printf(" ");
           }
        }
        printf("\n");
    }
}

CodePudding user response:

You can use printf() to pad the string for you with the required amount of spaces while printing the asterisk (*) character. That way, you don't need to worry about spacing it yourself:

#include <stdio.h>
#include <string.h>

int main(void) {
    int n;
    printf("Enter the number: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i  ) {
        char pat[n   1];
        memset(pat, 0, sizeof pat);
        pat[0] = '*';

        for (int j = 1; j <= n; j  ) {
            for (int k = 1; k <= n; k  )
                printf("%*s", n, pat);
            printf("\n");
            pat[j] = ' ';
        }
    }
}

CodePudding user response:

Why go for two loops when you can go for one? If you observe the sequence of characters printed, including newlines, you may see that the last character printed in every n*n 1 sequence is a newline, and the last character in every n sequence is a '*'. The rest are spaces.

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char **argv)
{
  if (argc != 2)
    {
      fprintf(stderr, "usage: star_pattern num\n");
      exit(EXIT_FAILURE);
    }

  int n = atoi(argv[1]);
  int dim = n*n;

  for (int i = 0; i < (dim 1)*dim; i  )
  {
    if (i%(dim 1) == dim)
      fputc('\n', stdout);
    else if (i % n == n-1)
      fputc('*', stdout);
    else
      fputc(' ', stdout);
  }
}

CodePudding user response:

Java1 solution:

private static void stars(int n) {
    for (var i = 0; i < n; i  ) {
        for (var j = 0; j < n; j  ) {
            var p = " ".repeat(n-j-1)   '*'   " ".repeat(j);
            System.out.println(p.repeat(n));
        }
    }
}

or a bit optimized (?)

private static void stars(int n) {
    var p = (" ".repeat(n-1)   '*').repeat(n 1);
    var l = n*n;
    for (var i = 0; i < l; i  ) {
        System.out.println(p.substring(i%n, l i%n));
    }
}

Output for n = 2:

 * *
* * 
 * *
* * 

and n = 3:

  *  *  *
 *  *  * 
*  *  *  
  *  *  *
 *  *  * 
*  *  *  
  *  *  *
 *  *  * 
*  *  *  

1 - question was tagged with Java

CodePudding user response:

This problem can solve using 2 for loop.

#include<stdio.h>
    int main()
    {
        int num;
        printf("Enter pattern length :");
        scanf("%d",&num);
        
        int matrix=num*num;
        for(int i=0;i<matrix;i  )
        {
    
            for(int j=0;j<matrix;j  )
            {
               if((i j 1)%num==0)
               {
                   printf("*");
               }
               else
               {
                  printf(" ");
               }
            }
            printf("\n");
        }
    }
  • Related