Example HTML page

C Program for printing patterns (Model 1)

In this article, i am going to write the program to print the patterns of the following shape.

*

**

***

****

*****

Here, in the first row, one *, second row two stars **, third row three stars *** and so on..

First take a look at the following two programs.

#include<stdio.h>
int main()
{
	
	int i,j;
	
	for(i=0;i<5;i++)
	{
		
			printf("*");
		
	//	printf("\n");
	}
}
Example 1

The output of the above program is

*****

Now if i use a ‘\n’ in the program we get the following output.

#include<stdio.h>
int main()
{
	
	int i,j;
	
	for(i=0;i<5;i++)
	{
		
			printf("*");
		
	printf("\n");
	}
}
Example 2

Output of the above program is

*
*
*
*
*

Now take a look at the following program, which takes two loops with a new line.

#include<stdio.h>
int main()
{
	
	int i,j;
	
	for(i=0;i<5;i++)
	{
		for(j=0;j<=5;j++)  
		{
			printf("*");
		}
		printf("\n");
	}
}
Pattern 1

Output:

Command Prompt

******
******
******
******
******

From here on words i will show the output of the program and later i will write the “C” program to get such kind of pattern.

Pattern 2:

Output:

Command Prompt

*
**
***
****
*****

Hint: if you change the condition in the second for loop ‘j<=5’ to ‘j<=i’ we get this output.

it means that we are printing stars up to i; means first row one star, second row two starts and so on..

#include<stdio.h>
int main()
{
	
	int i,j;
	
	for(i=0;i<5;i++)
	{
		for(j=0;j<=i;j++)  
		{
			printf("*");
		}
		printf("\n");
	}
}
Pattern 2

 

Pattern 3:

Output:

Command Prompt

0
11
222
3333
44444

Hint: This pattern is almost same as the pattern 2. The different is printing. Instead of printing “*” if you print the value of ‘i’ you get this output. you are printing ‘i’ value means you have to use “%d” in the printf() statement.

#include<stdio.h>
int main()
{
	
	int i,j;
	
	for(i=0;i<5;i++)
	{
		for(j=0;j<=i;j++)  
		{
			printf("%d",i);
		}
		printf("\n");
	}
}
Pattern 3

 

Pattern 4:

Output:

Command Prompt

0
01
012
0123
01234

Hint: instead of printing ‘i’ value print ‘j’ value to get this output.

#include<stdio.h>
int main()
{
	
	int i,j;
	
	for(i=0;i<5;i++)
	{
		for(j=0;j<=i;j++)  
		{
			printf("%d",j);
		}
		printf("\n");
	}
}
Pattern 4

 

Pattern 5:

Output:

Command Prompt

A
BB
CCC
DDDD
EEEEE

Hint: This program is similar to Pattern 3, here we have to print characters.

The value of ‘i’ and ‘j’ are initialized to 65, since the ASCII value of ‘i’ is 65.

#include<stdio.h>
int main()
{
	
	int i,j;
	
	for(i=65;i<70;i++)
	{
		for(j=65;j<=i;j++)  
		{
			printf("%c",i);
		}
		printf("\n");
	}
}
Pattern 5

 

Pattern 6:

Output:

Command Prompt

A
AB
ABC
ABCD
ABCDE

Hint: if you change the ‘i’ to ‘j’ in printf() statement of the above program you will get this output.

#include<stdio.h>
int main()
{
	
	int i,j;
	
	for(i=65;i<70;i++)
	{
		for(j=65;j<=i;j++)  
		{
			printf("%c",j);
		}
		printf("\n");
	}
}
Pattern 6

 

Pattern 7:

Output:

Command Prompt

C
CC
CCC
CCCC
CCCCC

Hint: if you want to print the same character to be printed, then initialize the character to a char variable and print in side the loop.

#include<stdio.h>
int main()
{
	
	int i,j;
	char p='C';
	
	for(i=0;i<5;i++)
	{
		for(j=0;j<=i;j++)  
		{
			printf("%c",p);
		}
		printf("\n");
	}
}
Pattern 7

 

Pattern 8:

Output:

Command Prompt

55
55 55
55 55 55
55 55 55 55
55 55 55 55 55

Hint: This is similar to above program. Assign 55 to integer variable and print it using %d.

#include<stdio.h>
int main()
{
	
	int i,j;
	int p=55;
	
	for(i=0;i<5;i++)
	{
		for(j=0;j<=i;j++)  
		{
			printf("%d ",p);
		}
		printf("\n");
	}
}
Pattern 8

 

Pattern 9:

Output:

Command Prompt

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Hint: Take a count variable and initialize it to ‘1’ and increment it inside the loop.

#include<stdio.h>
int main()
{
	
	int i,j;
	int count=1;
	
	for(i=0;i<5;i++)
	{
		for(j=0;j<=i;j++)  
		{
			printf("%d ",count);
			count++;
		}
		printf("\n");
	}
}
Pattern 9

 

Pattern 10:

Output:

Command Prompt

A
B C
D E F
G H I J
K L M N O

Hint: This is similar to above program. ‘count’ is initialized with ’65’ since the ASCII value is 65.

#include<stdio.h>
int main()
{
	
	int i,j;
	int count=65;
	
	for(i=0;i<5;i++)
	{
		for(j=0;j<=i;j++)  
		{
			printf("%c ",count);
			count++;
		}
		printf("\n");
	}
}
Pattern 10
Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest