Friday, January 10, 2020

Printing Pattern using Loops - Hacker Rank Solution

Printing Pattern using Loops - Hacker Rank Solution
PENDING

7 comments:

  1. #include
    #include
    #include
    #include

    int main()
    {

    int n,i,j,n1,k,m;
    int a[50][50];
    scanf("%d", &n);
    n1=n*2-1;
    k=0;
    m=n1-1;
    while(n!=0)
    {
    for (i=k;i<=m;i++)
    {
    for(j=k;j<=m;j++)
    {
    if(i==k ||i==m||j==k|| j==m)
    a[i][j]=n;
    }
    }
    ++k;
    --m;
    --n;
    }
    for (i=0;i<n1;i++)
    {
    for(j=0;j<n1;j++)
    {
    printf("%d " ,a[i][j]);
    }
    printf("\n");
    }

    return 0;
    }

    ReplyDelete
  2. #include
    #include
    #include
    #include

    int main()
    {

    int n,i,j,n1,k,m;
    scanf("%d", &n);
    n1=n*2-1;
    int **a = (int **)malloc(n1 * sizeof(int *));
    for (i=0; i<n1; i++)
    a[i] = (int *)malloc(n1 * sizeof(int));
    k=0;
    m=n1-1;
    while(n!=0)
    {
    for (i=k;i<=m;i++)
    {
    for(j=k;j<=m;j++)
    {
    if(i==k ||i==m||j==k|| j==m)
    a[i][j]=n;
    }
    }
    ++k;
    --m;
    --n;
    }
    for (i=0;i<n1;i++)
    {
    for(j=0;j<n1;j++)
    {
    printf("%d " ,a[i][j]);
    }
    printf("\n");
    }

    return 0;
    }

    ReplyDelete
  3. #include
    int main()
    {
    int n;
    printf("Enter Value of n\t");
    scanf("%d",&n);for(int i=n;i>=1;i--)
    {
    int k=n;
    for(int sp=n;sp>i;sp--,k--)
    {
    printf("%d ",k);
    }
    for(int j=1;j<=2*i-1;j++)
    {
    printf("%d ",i);
    }
    int l=i+1;
    for(int sp=n;sp>i;sp--,l++)
    {
    printf("%d ",l);
    }
    printf("\n");
    }
    for(int i=1;i<n;i++)
    {
    int k=n;
    for(int sp=0;sp<n-i-1;sp++,k--)
    {
    printf("%d ",k);
    }
    for(int j=1;j<=2*(i+1)-1;j++)
    {
    printf("%d ",i+1);
    }
    int m=i+2;
    for(int sp=0;sp<n-i-1;sp++,m++)
    {

    printf("%d ",m);
    }

    printf("\n");
    }
    }

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  4. #include
    #include
    #include
    #include

    int main()
    {

    int n;
    scanf("%d", &n);
    int i,j,k;
    // Complete the code to print the pattern.
    for(i=0;ii+1;k--)
    {
    printf("%d ",k);
    }
    for(j=0;j<(2*i)+1;j++)
    {
    printf("%d ",(i+2));
    }
    for(k=i+2;k<=n;k++)
    {
    printf("%d ",k);
    }
    printf("\n");

    }
    return 0;
    }

    ReplyDelete
  5. #include
    int main(int argc, char const *argv[])
    {

    int num,rows,i=0,j=0;
    scanf("%d",&num);

    rows = num*2 - 1;

    for(i = 0; i < rows; i++){

    for(j = 0; j < rows; j++){

    if(i < (rows / 2) + 1){

    if(j >= i && j < rows - i){
    printf("%d ",num);
    }
    else if(j < i){

    printf("%d ",num);
    num--;
    }
    else if(j >= (rows - i)){
    num++;
    printf("%d ",num);
    }

    }
    else{

    if(j <= i && j > rows - i-2){
    printf("%d ",num);

    }else if(j > i){

    num++;
    printf("%d ",num);


    }else if(j < (rows - i)){
    printf("%d ",num );
    num--;
    }


    }
    }

    printf("\n");

    }



    return 0;
    }

    ReplyDelete

Powered by Blogger.