Thứ Hai, 1 tháng 9, 2014

Sample pattern program in "C"

Hi...

                     we have to print the below pattern using c.
 
pattern.

Due to complexity in our program am dividing my code into two parts.one for design upper part of pattern ,another for design lower part of my pattern.


Here is the code:

#include<stdio.h>
#include<conio.h>
void forward(int);
void backword(int);
int main()
{
int len;
clrscr();
printf("Enter pattern length");
scanf("%d",&len);
forward(len);
backword(len);
return 0;
}
void forward(int num)
{
    int space,index;
    for(index=1;index<=num;index++)
    {
    for(space=num-index;space>=1;space--)
        printf(" ");
        printf("%d",index);
    for(space=index*2;space>1;space--)
        printf(" ");
    printf("%d",index);
        printf("\n");
    }
}
void backword(int num)
{
    int space,index,currentvalue;
    for(index=1,currentvalue=num-1;index<num;index++,currentvalue--)
    {
        for(space=index;space>=1;space--)
        printf(" ");
        printf("%d",currentvalue);
        for(space=currentvalue*2;space>1;space--)
        printf(" ");
        printf("%d",currentvalue);
        printf("\n");
    }

}


output:
------------------------------






















Không có nhận xét nào:

Đăng nhận xét