Creating Patterns with C Programs


This post is for C Programmers - 

When I first studied "C" programming language, I was really fascinated by its power as its very light-weight and simple.
Then when I started my carrier as software programmer, I learned C++, Java, Dr.Scheme (as it was part of training curriculum), and python (by accident...& I am still a novice in..) but today when I skimmed through my notebook of C-programs which I did, just out of curiosity.
Here is a sample pattern program,
1) Pattern Program 1 -


Requirements : - 


User will enter the no of lines limit & corresponding pattern should be produced.

e.g. If User inputs 5, then output pattern should be - 

Output Pattern is - 

    1
   2 2
  3 3 3
 4 4 4 4
5 5 5 5 5


C Code -


/*
For Output Pattern -

					1
				2		2
			3		3		3
		4		4		4		4
			.           .
			.           .
			.           .
n	...  	n		n		n	...	n
By - Pramod Khare
*/
//Include stdio.h and conio.h header files
#include ;
#include ;

void main(){

int i,j,x,msp=0,sp;
clrscr();
printf("\nEnter Line Limit - ");
scanf("%d",&x);

//sp - starting space before first digit is printed
//msp -middle space - space between digits
sp = x-1;

for(i=0;i<=x;i++){
	for(;(sp);sp--)
		printf(" ");

	msp = i-1;

	for(j=1;j<=i;j++){
		printf("%d",i);
		if(msp){
			printf(" ");
		}
		msp--;
	}
	printf("\n");
	sp = x-i-1;
}
getch();
}



 Same way you can modify for output... 
     1
    2 3
   4 5 6
 7 8 9 10
......

Comments

Popular posts from this blog

How to install / Configure Mantis For Apache-PHP-PostgreSQL combination

Modified ExtJS LOVCombo (List of Values) ux plugin - with select all, deselect all, text filter, bubble up and bubble down of selection

Randomized Probabilistic Bag of Objects Data-Structure using Map