GGK Technologies Coding Question-1

Program to print following pattern:


Input : 5
Output:
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *


// Java program to print
// the given pattern
import java.io.*;

class x {

static void pattern(int n)
{
int i, j;

// This is upper half of pattern
for (i = 1; i <= n; i++) {
for (j = 1; j <= (2 * n); j++) {

// Left part of pattern
if (i > (n – j + 1))
System.out.print(” “);
else
System.out.print(“*”);

// Right part of pattern
if ((i + n) > j)
System.out.print(” “);
else
System.out.print(“*”);
}

System.out.println(“”);
}

// This is lower half of pattern
for (i = 1; i <= n; i++) {
for (j = 1; j <= (2 * n); j++) {

// Right Part of pattern
if (i < j)
System.out.print(” “);
else
System.out.print(“*”);

// Left Part of pattern
if (i <= ((2 * n) – j))
System.out.print(” “);
else
System.out.print(“*”);
}

System.out.println(“”);
}
}

// main function
public static void main(String[] args)
{
pattern(7);
}
}

One comment on “GGK Technologies Coding Question-1”


  • kamalakar reddy

    /******************************************************************************

    Online C Compiler.
    Code, Compile, Run and Debug C program online.
    Write your code in this editor and press “Run” button to compile and execute it.

    *******************************************************************************/

    #include

    int main()
    {
    int i,j;
    for(i=1;i<=5;i++)
    {
    {for(j=1;j<=10;j++)
    if((j<=12-2*i))
    {printf("*");}
    else
    {printf(" ");}
    }
    printf("\n");}
    //printf("Hello World");
    for(i=1;i<=5;i++)
    {
    {for(j=1;j<=10;j++)
    if((j<=2*i))
    {printf("*");}
    else
    {printf(" ");}
    }
    printf("\n");}
    return 0;
    }