C Programs to print Interesting Patterns

2.7


C Program to print following pattern:
Examples:

Input : 5
Output: 
* * * * *  * * * * *
* * * *      * * * *
* * *          * * *
* *              * *
*                  *
*                  *
* *              * *
* * *          * * *
* * * *      * * * *
* * * * *  * * * * *

This program is divided into four parts.

// C program to print 
// the given pattern

#include<stdio.h>
pattern(int n)
{
	int i,j;

	// This is upper half of pattern
	for (i=1; i<=n; i++)
	{
		for (j=1; j<=(2*n); j++)
		{
			// Left part of pattern
			if (i>(n-j+1))
				printf(" ");
			else
				printf("*");
				
			// Right part of pattern
			if ((i+n)>j)
				printf(" ");
			else
				printf("*");
		}
		printf("\n");
	}
	
	// This is lower half of pattern
	for (i=1; i<=n; i++)
	{
		for (j=1; j<=(2*n); j++)
		{ 
			// Right Part of pattern
			if (i<j)
				printf(" ");
			else
				printf("*");
			
			// Left Part of pattern
			if (i<=((2*n)-j))
				printf(" ");
			else
				printf("*");
		}
		printf("\n");
	}
}

// main function
int main()
{
	pattern(7);
	return 0;
}

Output:


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

C Program to print following pattern:
Examples:

Input : 5
Output: 
*                  *
* *              * *
* * *          * * *
* * * *      * * * *
* * * * *  * * * * *
* * * * *  * * * * *
* * * *      * * * *
* * *          * * *
* *              * *
*                  *

This program is divided into four parts.

// C program to print the 
// given pattern

#include<stdio.h>
pattern(int n)
{
	int i,j;
	
	// This is upper half of pattern
	for (i=1; i<=n; i++)
	{
		for (j=1; j<=(2*n); j++)
		{
			// Left part of pattern
			if (i<j)
				printf(" ");
			else
				printf("*");
			
			// Right part of pattern
			if (i<=((2*n)-j))
				printf(" ");
			else
				printf("*");
		}
		printf("\n");
	}
	
	// This is lower half of pattern
	for (i=1; i<=n; i++)
	{
		for (j=1;j<=(2*n);j++)
		{
			// Left part of pattern
			if (i>(n-j+1))
				printf(" ");
			else
				printf("*");
				
			// Right part of pattern
			if ((i+n)>j)
				printf(" ");
			else
				printf("*");
		}
		printf("\n");
	}
}

// main function
int main()
{
	pattern(7);
	return 0;
}

Output:


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

This article is contributed by Aditya Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



2.7 Average Difficulty : 2.7/5.0
Based on 4 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.