Showing posts with label pyramid. Show all posts
Showing posts with label pyramid. Show all posts

Thursday, 9 May 2013

Pattern of strings-2



DESCRIPTION: This program prints the string pattern as below-
                                                                 hello
                                                                 hell
                                                                 hel
                                                                 he
                                                                 h


PROGRAM:


#include<stdio.h>
main()
{
    char str[50];
    int i,j,n;
    printf("enter the string\n");
    scanf("%s",str);
    for(i=0;str[i]!='\0';i++);
    n=i;
    for(i=0;i<n;i++)
    {
        for(j=0;j<i;j++)
        printf(" ");
        for(j=i;j<n;j++)
        printf("%c",str[j]);
        printf("\n");
    }
}



OUTPUT WINDOW:





Pattern of strings-1



DESCRIPTION: This program prints the string pattern as below-
                                                                hello
                                                                  ello
                                                                    llo
                                                                     lo
                                                                      o


PROGRAM:



#include<stdio.h>
main()
{
    char str[50];
    int i,j,n;
    printf("enter the string\n");
    scanf("%s",str);
    for(i=0;str[i]!='\0';i++);
    n=i;
    for(i=0;i<n;i++)
    {
        for(j=0;j<i;j++)
        printf(" ");
        for(j=i;j<n;j++)
        printf("%c",str[j]);
        printf("\n");
    }
}



OUTPUT WINDOW:



Triangle-1



DESCRIPTION: This program prints the triangle as below-
                                                                1
                                                                01
                                                                101
                                                                0101
                                                                10101


PROGRAM:


#include<stdio.h>
main()
{
int i,j,n,a,b;
printf("Enter the number of lines\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
    if(a%2==0)
    {
        a=1;
        b=0;
    }
    else
    {
        a=0;
        b=1;
    }
    for(j=0;j<=i;j++)
    {
        if(j%2==0)
        printf("%d",a);
        else
        printf("%d",b);
    }
    printf("\n");
}
}



OUTPUT WINDOW:



Pyramid-3



DESCRIPTION: This program prints the pyramid as below-
                                                                 1
                                                               121
                                                             12321
                                                           1234321


PROGRAM:


#include<stdio.h>
main()
{
    int i,j,k,n,m;
    printf("enter no. of lines\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(m=0;m<n-i;m++)
        printf(" ");
        for(j=0,k=i;j<=i||k!=0;j++)
        {
            if(j<=i)
            printf("%d",j+1);
            else if(k!=0)
            {
            printf("%d",k--);
            }
        }
        printf("\n");
    }
}



OUTPUT WINDOW:



Pyramid-2


DESCRIPTION: This program prints the pyramid as below-
                                                                 1
                                                               1  1
                                                             1  2  1
                                                           1  3  3  1
                                                         1  4  6  4  1


PROGRAM:


#include<stdio.h>
 fact(int x)
{
    int f=1,i;
    for(i=x;i>0;i--)
    f=f*i;
    return f;
}
main()
{
   int i,j,n,k;
    printf("enter the value of n\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-i;j++)
        printf(" ");
        for(k=0;k<=i;k++)
        printf("%2d",fact(i)/(fact(k)*fact(i-k)));
        printf("\n");
    }
}



OUTPUT WINDOW:


Pyramid-1


DESCRIPTION: This program prints the pyramid as below-
                                                                 1
                                                               232
                                                             34543
                                                           4567654


PROGRAM:


#include<stdio.h>
main()
{
    int p,j,k,l,n;
    printf("enter the value of n\n");
    scanf("%d",&n);
    for(p=0;p<n;p++)
    {
       for(j=0;j<(n-p-1);j++)
       printf(" ");
       for(k=p+1;k<(2*p+2);k++)
       printf("%d",k);
       for(l=2*p;l>p;l--)
       printf("%d",l);
        printf("\n");
    }
 }


OUTPUT WINDOW:


Inverted pyramid-1


DESCRIPTION: To print the inverted pyramid pattern as below-
                                                                 1234321
                                                                   12321
                                                                     121
                                                                       1


PROGRAM:


#include<stdio.h>
main()
{
    int i,j,k,l,n;
    printf("enter the value of n\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(j=n-i;j<n;j++)
        printf(" ");
        for(k=1;k<=n-i;k++)
        printf("%d",k);
        for(l=n-i-1;l>0;l--)
        printf("%d",l);
        printf("\n");
    }
}


OUTPUT WINDOW: