<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Interview Mantra &#187; pattern programs</title>
	<atom:link href="http://www.interviewmantra.net/tag/pattern-programs/feed" rel="self" type="application/rss+xml" />
	<link>http://www.interviewmantra.net</link>
	<description>Your mantra to job success</description>
	<lastBuildDate>Thu, 29 Jul 2010 06:38:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>10 Challenging char pattern programs</title>
		<link>http://www.interviewmantra.net/2010/01/10-challenging-char-pattern-programs.html</link>
		<comments>http://www.interviewmantra.net/2010/01/10-challenging-char-pattern-programs.html#comments</comments>
		<pubDate>Wed, 13 Jan 2010 14:00:36 +0000</pubDate>
		<dc:creator>Utsav Banerjee</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[pattern programs]]></category>

		<guid isPermaLink="false">http://www.interviewmantra.net/?p=652</guid>
		<description><![CDATA[Here are 10 pattern type problems statements with solutions. All the programs are written in C language.  This post is a part of the pattern programs series. Write a C program to print the following pattern: A B A C B A D C B A E D C B A Write a C program [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_694" class="wp-caption alignright" style="width: 310px"><a href="http://www.interviewmantra.net/wp-content/uploads/2010/01/char-pattern2.png"><img class="size-medium wp-image-694" title="Char pattern program" src="http://www.interviewmantra.net/wp-content/uploads/2010/01/char-pattern2-300x204.png" alt="Output of a char pattern program" width="300" height="204" /></a><p class="wp-caption-text">Output of a char pattern program</p></div>
<p>Here are 10 pattern type problems statements with solutions. All the programs are written in C language.  This post is a part of the <a href="http://www.interviewmantra.net/tag/pattern-programs">pattern programs series</a>.</p>
<p><!-- List of Questions in this post --></p>
<ol>
<li><a href="#ques1">Write a C program to print the following pattern: </a>
<pre><a href="#ques1">
A
B A
C B A
D C B A
E D C B A</a></pre>
</li>
<p><span id="more-652"></span></p>
<li><a href="#ques2">Write a C program to print the following pattern: </a>
<pre><a href="#ques2">A B C D E
B C D E
C D E
D E
E
</a></pre>
</li>
<li><a href="#ques3">Write a C program to print the following pattern: </a>
<pre><a href="#ques3">         A
a   a
B   B   B
b   b   b   b
C   C   C   C   C
c   c   c   c
D   D   D
d   d
E
</a></pre>
</li>
<li><a href="#ques4">Write a C program to print the following pattern: </a>
<pre><a href="#ques4">
A
Z
B   C
Y   X
D   E   F
W   V   U
G   H   I   J
T   S   R   Q
K   L   M   N   O
</a></pre>
</li>
<li><a href="#ques5">Write a C program to print the following pattern: </a>
<pre><a href="#ques5"> A   B   C   D   E
Z   Y   X   W
A   B   C   D
Z   Y   X
A   B   C
Z   Y
A   B
Z
A
Z
A   B
Z   Y
A   B   C
Z   Y   X
A   B   C   D
Z   Y   X   W
A   B   C   D   E
</a></pre>
</li>
<li><a href="#ques6">Write a C program to print the following pattern: </a>
<pre><a href="#ques6">A B C D E D C B A
A B C D   D C B A
A B C       C B A
A B           B A
A               A
A B           B A
A B C       C B A
A B C D   D C B A
A B C D E D C B A

</a></pre>
</li>
<li><a href="#ques7">Write a C program to print the following pattern: </a>
<pre><a href="#ques7">           A
B C D
E F G H I
J K L M N O P
Q R S T U V W X Y
Z
Y X W V U T S R Q
P O N M L K J
I H G F E
D C B
A
</a></pre>
</li>
<li><a href="#ques8">Write a C program to print the following pattern: </a>
<pre><a href="#ques8">N     N
N N   N
N   N N
N     N
</a></pre>
</li>
<li><a href="#ques9">Write a C program to print the following pattern: </a>
<pre><a href="#ques9"> X               X
X           X
X       X
X   X
X
X   X
X       X
X           X
X               X
</a></pre>
</li>
<li><a href="#ques10">Write a C program to print the following pattern: </a>
<pre><a href="#ques10">K             K
K           K
K         K
K       K
K     K
K   K
K K
K
K K
K   K
K     K
K       K
K         K
K           K
K             K
</a></pre>
</li>
</ol>
<ol><!-- Start of Question1 --><a name="ques1"> </a><span class="ques"> </span></p>
<li>Write a C program to print the following pattern:</li>
<pre>A
B A
C B A
D C B A
E D C B A</pre>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;

int main() {
	char prnt = 'A';
	int i, j;
	for (i = 1; i &lt;= 5; i++) { // Prints the right angle triangle
		for (j = 1; j &lt;= i; j++) {
			printf(&quot;%2c&quot;, ((prnt + (i - j))));  // Prints the characters in the required order
		}
		printf(&quot;\n&quot;);
	}
	return 0;
}
</pre>
<p><a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question1 --><!-- Start of Question2 --><a name="ques2"> </a><span class="ques"> </span></p>
<li> Write C program to print the following pattern:
<pre>A B C D E
  B C D E
    C D E
      D E
        E</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include&lt;stdio.h&gt;
int main() {
	int i, j;
	char c = 'A';
	for (i = 0; i &lt; 5; i++) {
		for (j = 0; j &lt; 5; j++) {
			if (j &lt; i) {
				printf(&quot; &quot;);
			} else {
				printf(&quot;%c&quot;, c + j);  // The order of the alphabets are increasing in terms of j
			}
			printf(&quot; &quot;);
		}
		printf(&quot;\n&quot;);
	}
	return 0;
}
</pre>
<p><a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question2 --><!-- Start of Question3 --><a name="ques3"> </a><span class="ques"> </span></p>
<li>Write C program to print the following pattern:
<pre>        A
      a   a
    B   B   B
  b   b   b   b
C   C   C   C   C
  c   c   c   c
    D   D   D
      d   d
        E</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include&lt;stdio.h&gt;
int main() {
	char prnt_even = 'a';
	char prnt_odd = 'A';
	int i, j, s, nos = 4,line=0;  //line tracks if the row is odd or even
	for (i = 1; i &lt;= 9; (i = i + 2)) {
		for (s = nos; s &gt;= 1; s--) {  // For the required spacings
			printf(&quot;  &quot;);
		}
		for (j = 1; j &lt;= i; j++) {
			if(line==0){
				if ((i % 2) != 0 &amp;&amp; (j % 2) != 0) {
					printf(&quot;%2c&quot;, prnt_odd);   // Prints capital letter if l=0
					} else {
						printf(&quot;  &quot;);
					}
				}
				else{
					if ((i % 2) != 0 &amp;&amp; (j % 2) != 0) {
						printf(&quot;%2c&quot;, prnt_even);  //else prints a small letter
						} else {
							printf(&quot;  &quot;);
						}
					}
				}
/* After one column the value of line is interchanged and prnt_even or prnt_odd is incremented accordingly */
				if(line==0){
					prnt_odd++;
					line=1;
				}
				else{
					prnt_even++;
					line=0;
				}
			nos--;  //no of space decremented by 1
			printf(&quot;\n&quot;);
		}
	nos = 1;
	/* Second half of the diamond. This one skips its first row rest goes pretty much the same */
	for (i = 7; i &gt;= 1; (i = i - 2)) {
		for (s = nos; s &gt;= 1; s--) {
			printf(&quot;  &quot;);
		}
		for (j = 1; j &lt;= i; j++) {
			if(line==0){
				if ((i % 2) != 0 &amp;&amp; (j % 2) != 0) {
					printf(&quot;%2c&quot;, prnt_odd);
					} else {
						printf(&quot;  &quot;);
					}
				}
				else{
					if ((i % 2) != 0 &amp;&amp; (j % 2) != 0) {
						printf(&quot;%2c&quot;, prnt_even);
						} else {
							printf(&quot;  &quot;);
						}
					}
				}
				if(line==0){
					prnt_odd++;
					line=1;
				}
				else{
					prnt_even++;
					line=0;
				}
				nos++;  //no of spaces is incremented by one
				printf(&quot;\n&quot;);
			}
			return 0;
		}
</pre>
<p><strong>Explanation:</strong><br />
This is a diamond formation where all the even places are hollowed. In case of odd row no a capital letter is printed and a small case alphabet otherwise. Both the capital letters n the small letters increment by one starting from &#8216;A&#8217; or &#8216;a&#8217;.<br />
<a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question3 --><!-- Start of Question4 --><a name="ques4"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre> A
   Z
 B   C
   Y   X
 D   E   F
   W   V   U
 G   H   I   J
   T   S   R   Q
 K   L   M   N   O</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
int main() {
	char prnt_odd = 'A';
	char prnt_even = 'Z';
	int i, j;
	// The right angle has nine rows
	for (i = 1; i &lt;= 9; i++) {
		for (j = 1; j &lt;= i; j++) {
			//checks for the column printing restrictions..
			if ((i % 2) != 0 &amp;&amp; (j % 2) != 0) {
				printf(&quot;%2c&quot;, prnt_odd);
				prnt_odd++;
			} else if ((i % 2) == 0 &amp;&amp; (j % 2) == 0) {
				printf(&quot;%2c&quot;, prnt_even);
				prnt_even--;
			} else {
				printf(&quot;  &quot;);
			}
		}
		printf(&quot;\n&quot;);
	}
	return 0;
}
</pre>
<p><strong>Explanation:</strong><br />
This is a right angle triangle of alphabets, having two sets of alphabets. One for the odd rows which starts from &#8216;A&#8217; and increments by one. And another one for the even rows which starts from &#8216;Z&#8217; and decrements by 1. The restriction in the columns are: if the row no is odd only the odd places are printed else if the row no is even only the even places are filled, rest are skipped.<br />
<a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question4 --><!-- Start of Question5 --><a name="ques5"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre> A   B   C   D   E
   Z   Y   X   W
 A   B   C   D
   Z   Y   X
 A   B   C
   Z   Y
 A   B
   Z
 A
   Z
 A   B
   Z   Y
 A   B   C
   Z   Y   X
 A   B   C   D
   Z   Y   X   W
 A   B   C   D   E</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
//This prints the columns with guidance of the row number supplied.
int triangle(int i)
{
	char prnt_odd = 'A';
	char prnt_even= 'Z';
	int j;
	for(j=1; j&lt;=i; j++)
	{
		//Checks for the restrictions
		if((i%2)!=0 &amp;&amp; (j%2)!=0)
		{
			printf(&quot;%2c&quot;,prnt_odd);
			prnt_odd++;
		}
		else if((i%2)==0 &amp;&amp; (j%2)==0)
		{
			printf(&quot;%2c&quot;,prnt_even);
			prnt_even--;
		}
		else
		{
			printf(&quot;  &quot;);
		}
	}
	return 0;
}

int main()
{
	int i;
	//Prints the first right angle triangle with 9 rows
	for(i=9; i&gt;=1; i--)
	{
		triangle(i);
		printf(&quot;\n&quot;);
	}
	// This prints the second right angle triangle skipping its tip.
	for(i=2; i&lt;=9; i++)
	{
		triangle(i);
		printf(&quot;\n&quot;);
	}
	return 0;
}
</pre>
<p><strong>Explanation:</strong><br />
This pattern can be seen as two right angle triangles of alphabets, having the same tip with two sets of alphabets. One for the odd rows which starts from &#8216;A&#8217; and increments by one. And another one for the even rows which starts from &#8216;Z&#8217; and decrements by 1. The restriction in the columns are: if the row number is odd, only the odd places are printed. Else if the row number is even only the even places are filled, rest are skipped.<br />
<a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question5 --><!-- Start of Question6 --><a name="ques6"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre>A B C D E D C B A
A B C D   D C B A
A B C       C B A
A B           B A
A               A
A B           B A
A B C       C B A
A B C D   D C B A
A B C D E D C B A</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
/*
 nos   = Number of Spaces required.
 i     = Number of columns
 skip  = The row in which we have to skip the first character
 form  = Whether the alphabets has to printed in Incremental or Decremental order
 */
int triangle(int nos, int i, int skip, int form) {
	char prnt[] = &quot;ABCDE&quot;;
	int j, s;
	for (s = nos; s &gt;= 1; s--) {   // Controls the required spacings
		printf(&quot;  &quot;);
	}
	for (j = 0; j &lt;= i; j++) {     // prints the characters row wise
		if (skip != 1) {           // If we need to skip a row.
			if (i == skip &amp;&amp; j == 0) {
				continue;
			}
		}
		if (form == 0) { /* Decides whether the characters are in incremental or decremental order. */
			printf(&quot;%2c&quot;, prnt[j]);
		} else {
			printf(&quot;%2c&quot;, prnt[i - j]);
		}
	}
	return 0;
}

int main() {
	int i, nos = -1;
	for (i = 4; i &gt;= 0; i--) {
		triangle(0, i, 1, 0);    // Prints the first triangle
		triangle(nos, i, 4, 1);  // Prints the second triangle
		nos = nos + 2;    // Spacing factor
		printf(&quot;\n&quot;);
	}
	nos = 5;
	for (i = 1; i &lt;= 4; i++) {
		triangle(0, i, 0, 0);   // Prints the third triangle
		triangle(nos, i, 4, 1); // Prints the fourth triangle
		nos = nos - 2;     //Spacing factor.
		printf(&quot;\n&quot;);
	}
	return 0;
}
</pre>
<p><strong>Explanation:</strong><br />
This pattern can be treated as four individual right angle triangles composed of alphabets in a sequential manner.<br />
<a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question6 --><!-- Start of Question7 --><a name="ques7"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre>           A
         B C D
       E F G H I
     J K L M N O P
   Q R S T U V W X Y
                     Z
                       Y X W V U T S R Q
                         P O N M L K J
                           I H G F E
                             D C B
                               A</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
int main() {
	char prnt = 'A';
	int i, j, s, nos = 5;
	for (i = 1; i &lt;= 9; (i = i + 2)) {
		for (s = nos; s &gt;= 1; s--) {
			printf(&quot;  &quot;);
		}
		for (j = 1; j &lt;= i; j++) {
			printf(&quot;%2c&quot;, prnt);
			++prnt;           //Increments the alphabet
			if (i == 9 &amp;&amp; j == 9) //for the extra Z
			{
				printf(&quot;\f%2c&quot;, prnt);
			}
		}
		nos--;
		//for the continuation
		if (i != 9) {
			printf(&quot;\n&quot;);
		}
	}
	printf(&quot;\f&quot;); //Line Feed
	for (i = 9; i &gt;= 1; (i = i - 2)) {
		//Maintaining the required Spaces
		if (i == 9) {
			nos = 0;
		} else if (i == 7) {
			nos = 12;
		}
		for (s = nos; s &gt;= 1; s--) {
			printf(&quot;  &quot;);
		}
		for (j = 1; j &lt;= i; j++) {
			--prnt;   //decrements the alphabet before printing it.
			printf(&quot;%2c&quot;, prnt);
		}
		nos++;
		printf(&quot;\n&quot;);
	}
	return 0;
}
</pre>
<p><strong>Explanation:</strong><br />
This can be seen as two isosceles triangle made up of alphabets incrementing by one starting from &#8216;A&#8217;. The connecting Z can be considered additionally.<br />
<a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question7 --><!-- Start of Question8 --><a name="ques8"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre>N     N
N N   N
N   N N
N     N</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
int main() {
	char prnt = 'N';
	int i, j, k;
	for (i = 1; i &lt;= 4; i++) {
		//First right-angle triangle.
		for (j = 1; j &lt;= i; j++) {
			if (j == 1 || j == i) {    // This Ensures an empty baseless triangle
				printf(&quot;%2c&quot;, prnt);   //Prints the character after 2 spaces.
			} else {
				printf(&quot;  &quot;);
			}
		}
		//Second right-angle triangle
		for (k = 3; k &gt;= i; k--) {
			if (k == i) {    // k=0 has to skipped as it shares the same hypotenuse
				if (i == 4) { //For the joining point
					break;
				}
				printf(&quot;%2c&quot;, prnt);
			} else {
				printf(&quot;  &quot;);
			}
		}
		printf(&quot;\n&quot;);
	}
	return 0;
}
</pre>
<p><strong>Explanation:</strong><br />
This pattern can be viewed as two baseless right-angle triangles arranged in such a way that they share a common hypotenuse.<br />
<a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question8 --><!-- Start of Question9 --><a name="ques9"> </a><span class="ques"> </span></p>
<li>Write a C program to print the following pattern:
<pre> X               X
   X           X
     X       X
       X   X
         X
       X   X
     X       X
   X           X
 X               X</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;
int main() {
	char prnt = 'X';
	int i, j, s, nos = 0;  //nos controls the spacing.
	//1st triangle
	for (i = 9; i &gt;= 1; (i = i - 2)) {
		for (s = nos; s &gt;= 1; s--) {  //Spacing control
			printf(&quot;  &quot;);
		}
		for (j = 1; j &lt;= i; j++) {
			if (j == 1 || j == i) { //This hollows the triangle
				printf(&quot;%2c&quot;, prnt);
			} else {
				printf(&quot;  &quot;);
			}
		}
		printf(&quot;\n&quot;);
		nos++;  // In the upper triangle the space increments by 1.
	}
/* Since both the triangles have the same peak point, while printing the second triangle we'll ignore its peak point. Thus i starts from 3 and the nos from 3.*/
	nos = 3;
	for (i = 3; i &lt;= 9; (i = i + 2)) {
		for (s = nos; s &gt;= 1; s--) {
			printf(&quot;  &quot;);
		}
		for (j = 1; j &lt;= i; j++) {
			if (j == 1 || j == i) {
				printf(&quot;%2c&quot;, prnt);
			} else {
				printf(&quot;  &quot;);
			}
		}
		printf(&quot;\n&quot;);
		nos--; //The spaces are in a decrementing order.
	}
	return 0;
}
</pre>
<p><a style="font-size: 0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question9 --><!-- Start of Question10 --><a name="ques10"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre>K             K
K           K
K         K
K       K
K     K
K   K
K K
K
K K
K   K
K     K
K       K
K         K
K           K
K             K</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
int main() {
	char prnt = 'K';
	int i, j;
	//First right-angle triangle
	for (i = 7; i &gt;= 1; i--) {
		for (j = 1; j &lt;= i; j++) {
			if (j == 1 || j == i) {  //This hollows the triangle
				printf(&quot;%2c&quot;, prnt);
			} else {
				printf(&quot;  &quot;);
			}
		}
		if (i != 1) {
			printf(&quot;\n&quot;);
		}
	}
	// Second triangle
	for (i = 1; i &lt;= 7; i++) {
		for (j = 1; j &lt;= i; j++) {
			if (j == 1 || j == i) {
				printf(&quot;%2c&quot;, prnt);
			} else {
				printf(&quot;  &quot;);
			}
		}
		printf(&quot;\n&quot;);
	}
	return 0;
}
</pre>
<p><strong>Explanation:</strong><br />
This pattern can be be seen as an inverted right triangle placed over a normal right-angled triangle. In addition to the triangles, there is an extra character in the intersection point.</p>
<p>Since both the triangles share the same tip and in the figure and we have an extra character in the point of intersection, we can start the second right angle triangle from the same line where the first one ends.<br />
<a style="font-size: 0.8em;" href="#top">Back to top</a><br />
<!-- End of Question10 --></ol>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2010/01/10-challenging-char-pattern-programs.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>10 Challenging number pattern programs in C</title>
		<link>http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html</link>
		<comments>http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#comments</comments>
		<pubDate>Sun, 08 Nov 2009 05:50:00 +0000</pubDate>
		<dc:creator>Utsav Banerjee</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[pattern programs]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/2009/11/10-challenging-number-pattern-programs-in-c/</guid>
		<description><![CDATA[This is a continuation to the series of challenging pattern C programs in Interview Mantra. This set of 10 puzzling programs are of type number patterns. Also read Print Pattern Programs Write a C program to print the following pattern: 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://1.bp.blogspot.com/_bYjWHxiThyA/SvZVat02XtI/AAAAAAAABjE/eBUPDY7A5C0/s1600-h/number-patter-output.png"><img id="BLOGGER_PHOTO_ID_5401598720543710930" style="margin: 0pt 0pt 10px 10px; float: right; cursor: pointer; width: 180px; height: 185px;" src="http://1.bp.blogspot.com/_bYjWHxiThyA/SvZVat02XtI/AAAAAAAABjE/eBUPDY7A5C0/s320/number-patter-output.png" border="0" alt="" /></a>This is a continuation to the series of challenging pattern C programs in Interview Mantra. This set of 10 puzzling programs are of type number patterns.</p>
<h3>Also read <a href="../2009/04/print-number-pattern-c-programs.html">Print Pattern Programs</a></h3>
<ol>
<li><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques1">Write a C program to print the following pattern: </a>
<pre><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques1">1
0 1
1 0 1
0 1 0 1
1 0 1 0 1</a><span id="more-327"></span></pre>
</li>
<li><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques2">Write a C program to print the following pattern: </a>
<pre><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques2">0
1 1
2 3 5
8 13 21</a></pre>
</li>
<li><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques3">Write a C program to print the following pattern: </a>
<pre><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques3">1
121
12321
1234321
12321
121
1</a></pre>
</li>
<li><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques4">Write a C program to print the following pattern: </a>
<pre><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques4">       2
     4 5 6
   6 7 8 9 10
     4 5 6
       2</a></pre>
</li>
<li><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques5">Write a C program to print the following pattern: </a>
<pre><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques5"> 1                         1
 3 3 3                 3 3 3
 5 5 5 5 5         5 5 5 5 5
 7 7 7 7 7 7 7 7 7 7 7 7 7 7
 5 5 5 5 5         5 5 5 5 5
 3 3 3                 3 3 3
 1                         1</a></pre>
</li>
<li><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques6">Write a C program to print the following pattern: </a>
<pre><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques6">    0
 -2-3 0
-4-3-2-1 0
 -2-3 0
    0</a></pre>
</li>
<li><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques7">Write a C program to print the following pattern: </a>
<pre><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques7">77777777777
         7
        7
       7
      7
     7
    7
   7
  7
 7
7</a></pre>
</li>
<li><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques8">Write a C program to print the following pattern: </a>
<pre><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques8"> 1                       1
 1 0                   1 0
 1 0 1               1 0 1
 1 0 1 0           1 0 1 0
 1 0 1 0 1       1 0 1 0 1
 1 0 1 0 1 0   1 0 1 0 1 0
 1 0 1 0 1 0 1 0 1 0 1 0 1
 1 0 1 0 1 0   1 0 1 0 1 0
 1 0 1 0 1       1 0 1 0 1
 1 0 1 0           1 0 1 0
 1 0 1               1 0 1
 1 0                   1 0
 1                       1</a></pre>
</li>
<li><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques9">Write a C program to print the following pattern: </a>
<pre><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques9">1
2 4
3 6 9
2 4
1</a></pre>
</li>
<li><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques10">Write a C program to print the following pattern: </a>
<pre><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#ques10"> 1
 1 0
 1 0 0
 1 0 0 0
 1 0 0 0 0
 1 0 0 0 0 0
 1 0 0 0 0 0 0
 1 0 0 0 0 0
 1 0 0 0 0
 1 0 0 0
 1 0 0
 1 0
 1</a></pre>
</li>
</ol>
<ol><!-- Start of Question1 --><a name="ques1"> </a><span></p>
<li>Write a C program to print the following pattern:</li>
<pre>1
0 1
1 0 1
0 1 0 1
1 0 1 0 1</pre>
<p></span><strong>Program:</strong></p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;

int main(void) {
 int i, j;
 for (i = 0; i &lt; 4; i++) {
  for (j = 0; j &lt;= i; j++) {
   if (((i + j) % 2) == 0) {  // Decides on as to which digit to print.
    printf(&quot;0&quot;);
   } else {
    printf(&quot;1&quot;);
   }
   printf(&quot;\t&quot;);
  }
  printf(&quot;\n&quot;);
 }
 return 0;
}</pre>
<p><a style="font-size: 0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f35292e7">Download Code</a></p>
<p><strong>Explanation:</strong> This is a right angle triangle composed of 0&#8242;s and 1&#8242;s.<br />
<a style="font-size: 0.8em;" href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#top">Back to top</a></p>
<p>End of Question1  Start of Question2 <a name="ques2"> </a><span> </span></p>
<li> Write C program to print the following pattern:
<pre>0
1 1
2 3 5
8 13 21</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;

int main(void) {
 int i, j, a = 0, b = 1, temp = 1;
 for (i = 1; i &lt;= 4; i++) {
  for (j = 1; j &lt;= i; j++) {
   if (i == 1 &amp;&amp; j == 1) { // Prints the '0' individually first
    printf(&quot;0&quot;);
    continue;
   }
   printf(&quot;%d &quot;, temp);  // Prints the next digit in the series
   //Computes the series
   temp = a + b;
   a = b;
   b = temp;
   if (i == 4 &amp;&amp; j == 3) { // Skips the 4th character of the base
    break;
   }
  }
  printf(&quot;\n&quot;);
 }
 return 0;
}</pre>
<p><a style="font-size: 0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f433eeb7f">Download Code</a></p>
<p><strong>Explanation:</strong> This prints the Fibonacci series in a right angle triangle formation where the base has only three characters.<br />
<a style="font-size: 0.8em;" href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#top">Back to top</a></p>
<p>End of Question2  Start of Question3 <a name="ques3"> </a><span> </span></p>
<li> Write C program to print the following pattern:
<pre>1
121
12321
1234321
12321
121
1</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;

void sequence(int x);
int main() {
 /* c taken for columns */
 int i, x = 0, num = 7;
 for (i = 1; i &lt;= num; i++) {
  if (i &lt;= (num / 2) + 1) {
   x = i;
  } else {
   x = 8 - i;
  }
  sequence(x);
  puts(&quot;\n&quot;);
 }
 return 0;
}

void sequence(int x) {
 int j;

 for (j = 1; j &lt; x; j++) {
  printf(&quot;%d&quot;, j);
 }
 for (j = x; j &gt; 0; j--) {
  printf(&quot;%d&quot;, j);
 }
}</pre>
<p><a style="font-size: 0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f71c38351">Download Code</a><br />
<a style="font-size: 0.8em;" href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#top">Back to top</a></p>
<p>End of Question3  Start of Question4 <a name="ques4"> </a><span> </span></p>
<li> Write a C program to print the following pattern:
<pre>       2
     4 5 6
   6 7 8 9 10
     4 5 6
       2</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;

int main(void) {
 int prnt;
 int i, j, k, r, s, sp, nos = 3, nosp = 2; //nos n nosp controls the spacing factor
 // Prints the upper triangle
 for (i = 1; i &lt;= 5; i++) {
  if ((i % 2) != 0) {
   for (s = nos; s &gt;= 1; s--) {
    printf(&quot;  &quot;);
   }
   for (j = 1; j &lt;= i; j++) {
    if (i == 5 &amp;&amp; j == 5) { //Provides the extra space reqd betn 9 n 10
     printf(&quot; &quot;);        // as 10 is a 2 digit no.
    }
    prnt = i + j;
    printf(&quot;%2d&quot;, prnt);
   }
  }
  if ((i % 2) != 0) {
   printf(&quot;\n&quot;);
   nos--;
  }
 }
 // Prints the lower triangle skipin its base..
 for (k = 3; k &gt;= 1; k--) {
  if ((k % 2) != 0) {
   for (sp = nosp; sp &gt;= 1; sp--) {
    printf(&quot;  &quot;);
   }
   for (r = 1; r &lt;= k; r++) {
    prnt = k + r;
    printf(&quot;%2d&quot;, prnt);
   }
  }
  if ((k % 2) != 0) {
   printf(&quot;\n&quot;);
   nosp++;
  }
 }
 return 0;
}</pre>
<p><a style="font-size: 0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f39493d3d">Download Code</a></p>
<p><strong>Explanation:</strong> This is a diamond formation composed of numbers. The numbers are in the following order next_no=i+j where<br />
next_no = The next no to be printed<br />
i = index of the outer for loop<br />
j = index of the inner for loop<br />
<a style="font-size: 0.8em;" href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#top">Back to top</a></p>
<p>End of Question4  Start of Question5 <a name="ques5"> </a><span> </span></p>
<li> Write a C program to print the following pattern:
<pre> 1                           1
 3 3 3                   3 3 3
 5 5 5 5 5           5 5 5 5 5
 7 7 7 7 7 7 7   7 7 7 7 7 7 7
 5 5 5 5 5           5 5 5 5 5
 3 3 3                   3 3 3
 1                           1</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;

int main(void) {
 int i, j, k, s, p, q, sp, r, c = 1, nos = 13;
 for (i = 1; c &lt;= 4; i++) {
  if ((i % 2) != 0) {   // Filters out the even line nos.
   for (j = 1; j &lt;= i; j++) { // The upper left triangle
    printf(&quot;%2d&quot;, i);
   }
   for (s = nos; s &gt;= 1; s--) {  // The spacing factor
    printf(&quot;  &quot;);
   }
   for (k = 1; k &lt;= i; k++) { // The upper right triangle
    printf(&quot;%2d&quot;, i);
   }
   printf(&quot;\n&quot;);
   nos = nos - 4;  // Space control
   ++c;
  }
 }
 nos = 10;  // Space control re intialized
 c = 1;
 for (p = 5; (c &lt; 4 &amp;&amp; p != 0); p--) {
  if ((p % 2) != 0) {  // Filters out the even row nos
   for (q = 1; q &lt;= p; q++) {  // Lower left triangle
    printf(&quot;%2d&quot;, p);
   }
   for (sp = nos; sp &gt;= 1; sp--) { // Spacing factor
    printf(&quot; &quot;);
   }
   for (r = 1; r &lt;= p; r++) {  // Lower right triangle
    printf(&quot;%2d&quot;, p);
   }

   printf(&quot;\n&quot;);
   --c;
   nos = nos + 8;  // Spacing control.
  }
 }

 return 0;
}</pre>
<p><a style="font-size: 0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f7968cb0a">Download Code</a></p>
<p><strong>Explanation:</strong> Here we are printing only the odd row nos along with thier respective line number. This structure can divided into four identical right angle triangles which are kind of twisted and turned placed in a particular format .<br />
<a style="font-size: 0.8em;" href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#top">Back to top</a></p>
<p>End of Question5  Start of Question6 <a name="ques6"> </a><span> </span></p>
<li> Write a C program to print the following pattern:
<pre>    0
 -2-3 0
-4-3-2-1 0
 -2-3 0
    0</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;

int main(void) {
 int i, j, k, r, s, sp, nos = 2, nosp = 1;
 for (i = 1; i &lt;= 5; i++) {
  if ((i % 2) != 0) {
   for (s = nos; s &gt;= 1; s--) {  //for the spacing factor.
    printf(&quot;  &quot;);
   }
   for (j = 1; j &lt;= i; j++) {
    printf(&quot;%2d&quot;, j-i);
   }
  }
  if ((i % 2) != 0) {
   printf(&quot;\n&quot;);
   nos--;
  }
 }
 for (k = 3; k &gt;= 1; k--) {
  if ((k % 2) != 0) {
   for (sp = nosp; sp &gt;= 1; sp--) {  // for the spacing factor.
    printf(&quot;  &quot;);
   }
   for (r = 1; r &lt;= k; r++) {
    printf(&quot;%2d&quot;, r-k);
   }
  }
  if ((k % 2) != 0) {
   printf(&quot;\n&quot;);
   nosp++;
  }
 }
 return 0;
}</pre>
<p><a style="font-size: 0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f7cac74ce">Download Code</a></p>
<p><strong>Explanation:</strong>This can be seen as a diamond composed of numbers. If we use the conventional nested for loop for its construction the numbers can be seen to flowing the following function f(x) -&gt; j-i<br />
where<br />
j= inner loop index<br />
i= outer loop index<br />
<a style="font-size: 0.8em;" href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#top">Back to top</a></p>
<p>End of Question6  Start of Question7 <a name="ques7"> </a><span> </span></p>
<li> Write a C program to print the following pattern:
<pre>77777777777
         7
        7
       7
      7
     7
    7
   7
  7
 7
7</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;

int main(void) {
 int i, j;
 for (i = 11; i &gt;= 1; i--) {
  for (j = 1; j &lt;= i; j++) {
   if (i == 11) {
    printf(&quot;7&quot;);  // Makes sure the base is printed completely
    continue;
   } else if (j == i) { // Hollows the rest
    printf(&quot;7&quot;);
   } else {
    printf(&quot; &quot;);
   }
  }
  printf(&quot;\n&quot;);
 }
 return 0;
}</pre>
<p><a style="font-size: 0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f2b227ed1">Download Code</a></p>
<p><strong>Explanation:</strong> This can be seen as a hollow right-angled triangle composed of 7&#8242;s<br />
<a style="font-size: 0.8em;" href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#top">Back to top</a></p>
<p>End of Question7  Start of Question8 <a name="ques8"> </a><span> </span></p>
<li> Write a C program to print the following pattern:
<pre> 1                       1
 1 0                   1 0
 1 0 1               1 0 1
 1 0 1 0           1 0 1 0
 1 0 1 0 1       1 0 1 0 1
 1 0 1 0 1 0   1 0 1 0 1 0
 1 0 1 0 1 0 1 0 1 0 1 0 1
 1 0 1 0 1 0   1 0 1 0 1 0
 1 0 1 0 1       1 0 1 0 1
 1 0 1 0           1 0 1 0
 1 0 1               1 0 1
 1 0                   1 0
 1                       1</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;

int main(void) {
    int i,j,k,s,nos=11;
    for (i=1; i&lt;=7; i++) {
        for (j=1; j&lt;=i; j++) {
            if ((j%2)!=0) {   // Applying the condition
                printf(&quot; 1&quot;);
            } else {
                printf(&quot; 0&quot;);
            }
        }
        for (s=nos; s&gt;=1; s--) {  // Space factor
            printf(&quot;  &quot;);
        }
        for (k=1; k&lt;=i; k++) {
            if(i==7 &amp;&amp; k==1)  // Skipping the extra 1
            {
                continue;
            }
            if ((k%2)!=0) {  // Applying the condition
                printf(&quot; 1&quot;);
            } else {
                printf(&quot; 0&quot;);
            }
        }
        printf(&quot;\n&quot;);
        nos=nos-2;  // Space Control
    }
     nos=1;
     for ( i=6; i&gt;=1; i--) {  // It shares the same base
         for (j=1; j&lt;=i; j++) {
             if (j%2!=0) {
                 printf(&quot; 1&quot;);
             } else {
                 printf(&quot; 0&quot;);
             }
         }
         for(s=nos; s&gt;=1; s--)  // Spacing factor
         {
             printf(&quot;  &quot;);
         }
         for (k=1; k&lt;=i; k++) {
             if (k%2!=0) {
                 printf(&quot; 1&quot;);
             } else {
                 printf(&quot; 0&quot;);
             }
         }
         printf(&quot;\n&quot;);
         nos=nos+2;
     }
    return 0;
}</pre>
<p><a style="font-size: 0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f77a3bf9c">Download Code</a><br />
<a style="font-size: 0.8em;" href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#top">Back to top</a></p>
<p>End of Question8  Start of Question9 <a name="ques9"> </a><span> </span></p>
<li> Write a C program to print the following pattern:
<pre>1
2 4
3 6 9
2 4
1</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;
int main(void) {
    int i,j;
    for (i=1; i&lt;=3 ; i++) {
        for (j=1; j&lt;=i; j++)  {
            printf(&quot;%2d&quot;, (i*j));
        }
        printf(&quot;\n&quot;);
    }
    for (i=2; i&gt;=1; i--) { // As they share the same base
        for (j=1; j&lt;=i; j++)  {
            printf(&quot;%2d&quot;,i*j);
        }
        printf(&quot;\n&quot;);
    }
    return 0;
}</pre>
<p><a style="font-size: 0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f3c3466d5">Download Code</a></p>
<p><strong>Explanation:</strong> This can be seen as two right angle triangles sharing th same base<br />
The numbers are following the following function f(x) = i *j<br />
where<br />
i = Index of the Outer loop<br />
j = Index of the inner loop<br />
<a style="font-size: 0.8em;" href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#top">Back to top</a></p>
<p>End of Question9  Start of Question10 <a name="ques10"> </a><span> </span></p>
<li> Write a C program to print the following pattern:
<pre> 1
 1 0
 1 0 0
 1 0 0 0
 1 0 0 0 0
 1 0 0 0 0 0
 1 0 0 0 0 0 0
 1 0 0 0 0 0
 1 0 0 0 0
 1 0 0 0
 1 0 0
 1 0
 1</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;

int main(void) {
    int i,j;
    for (i=1; i&lt;=7; i++) {
        for (j=1; j&lt;=i; j++) {
            if (j==1) {           // Applying the condition
                printf(&quot; 1&quot;);
            } else {
                printf(&quot; 0&quot;);
            }
        }
        printf(&quot;\n&quot;);
    }
    for (i=6; i&gt;=1; i--) {  //As it shares the same base i=6
        for (j=1; j&lt;=i; j++) {
            if (j==1) {   // Applying the condition
                printf(&quot; 1&quot;);
            } else {
                printf(&quot; 0&quot;);
            }
        }
        printf(&quot;\n&quot;);
    }
    return 0;
}</pre>
<p><a style="font-size: 0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f57b955fd">Download Code</a></p>
<p><strong>Explanation:</strong> This can be seen as two right angle triangles sharing the same base which is composed of 0&#8242;s n 1&#8242;s. The first column is filled with 1&#8242;s and rest with 0&#8242;s<br />
<a style="font-size: 0.8em;" href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html#top">Back to top</a> End of Question10</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>10 Challenging star pattern programs in C</title>
		<link>http://www.interviewmantra.net/2009/10/10-star-pattern-programs.html</link>
		<comments>http://www.interviewmantra.net/2009/10/10-star-pattern-programs.html#comments</comments>
		<pubDate>Mon, 19 Oct 2009 10:11:00 +0000</pubDate>
		<dc:creator>Utsav Banerjee</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[pattern programs]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/2009/10/10-challenging-star-pattern-programs-in-c/</guid>
		<description><![CDATA[Computer languages are not as easy as human languages, they need you to become a computer yourself, think like a computer and write an algorithm. Computer programs are fun if you take them up as a challenge, as unsolved puzzles. There is lot of fun in taking up the challenge, understanding the problem, writing an [...]]]></description>
			<content:encoded><![CDATA[<p><img style="margin: 0px 0px 10px 10px; width: 250px; height: 196px; float: right;" src="http://sites.google.com/site/sriavr/Home/pattern-screenshot.png" border="0" alt="star pattern screenshot" />Computer languages are not as easy as human languages, they need you to become a computer yourself, think like a computer and write an algorithm. Computer programs are fun if you take them up as a challenge, as unsolved puzzles. There is lot of fun in taking up the challenge, understanding the problem, writing an algorithm,  writing a program and most importantly running the program and obtaining required output.<span id="more-321"></span></p>
<p>Here are few challenging C program questions for you. Let&#8217;s see how many of them you would be able to write without seeing the program solution given below. These questions are to print patterns using asterisk(star) character. Comment below if you have tougher pattern questions.</p>
<h3>Also read <a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html">Number Pattern Programs</a></h3>
<p><!-- List of Questions in this post --></p>
<ol>
<li><a href="#ques1">Write a C program to print the following pattern: </a>
<pre><a href="#ques1">         *
      *  *
   *  *  *
*  *  *  *
</a></pre>
</li>
<li><a href="#ques2">Write a C program to print the following pattern: </a>
<pre><a href="#ques2">*                   *
* * *           * * *
* * * * *   * * * * *
* * * * * * * * * * *
</a></pre>
</li>
<li><a href="#ques3">Write a C program to print the following pattern: </a>
<pre><a href="#ques3">*                         *
   *                   *
*     *             *     *
   *     *       *     *
*     *      *      *     *
   *     *       *     *
*     *             *     *
   *                   *
*                         *
</a></pre>
</li>
<li><a href="#ques4">Write a C program to print the following pattern: </a>
<pre><a href="#ques4">*   *   *   *   *
  *   *   *   *
    *   *   *
      *   *
        *
      *   *
    *   *   *
  *   *   *   *
*   *   *   *   *
</a></pre>
</li>
<li><a href="#ques5">Write a C program to print the following pattern: </a>
<pre><a href="#ques5">          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
    * * * * * * *
      * * * * *
        * * *
          *
        * * *
      * * * * *
</a></pre>
</li>
<li><a href="#ques6">Write a C program to print the following pattern: </a>
<pre><a href="#ques6">     *
       * *
         * * *
           * * * *
         * * *
       * *
     *
</a></pre>
</li>
<li><a href="#ques7">Write a C program to print the following pattern: </a>
<pre><a href="#ques7">* * * * * * * * *
* * * *   * * * *
* * *       * * *
* *           * *
*               *
* *           * *
* * *       * * *
* * * *   * * * *
* * * * * * * * *
</a></pre>
</li>
<li><a href="#ques8">Write a C program to print the following pattern: </a>
<pre><a href="#ques8">* * * * * * * * * * * * * * * * *
  * * * * * * *   * * * * * * *
    * * * * *       * * * * *
      * * *           * * *
        * * * * * * * * *
          * * * * * * *
            * * * * *
              * * *
                *
</a></pre>
</li>
<li><a href="#ques9">Write a C program to print the following pattern: </a>
<pre><a href="#ques9">      *
    * * *
  * * * * *
* * * * * * *
*           *
* *       * *
* * *   * * *
* * * * * * *
* * *   * * *
* *       * *
*           *
* * * * * * *
  * * * * *
    * * *
      *
</a></pre>
</li>
<li><a href="#ques10">Write a C program to print the following pattern: </a>
<pre><a href="#ques10">* * * * * * * * * * * * * * * * * * * * * * * * *
  *           *   *           *   *           *
    *       *       *       *       *       *
      *   *           *   *           *   *
        *               *               *
      *   *           *   *           *   *
    *       *       *       *       *       *
  *           *   *           *   *           *
* * * * * * * * * * * * * * * * * * * * * * * * *
</a></pre>
</li>
</ol>
<ol><!-- Start of Question1 --><a name="ques1"> </a><span class="ques"> </span></p>
<li>Write a C program to print the following pattern:</li>
<pre>         *
      *  *
   *  *  *
*  *  *  *</pre>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">/* This is a simple mirror-image of a right angle triangle */

#include &lt;stdio.h&gt;
int main() {
 char prnt = '*';
 int i, j, nos = 4, s;
 for (i = 1; i &lt;= 5; i++) {
for (s = nos; s &gt;= 1; s--) {  // Spacing factor
   printf(&quot;  &quot;);
  }
  for (j = 1; j &lt;= i; j++) {
   printf(&quot;%2c&quot;, prnt);
  }
  printf(&quot;\n&quot;);
  --nos;   // Controls the spacing factor
 }
 return 0;
}</pre>
<p><a style="font-size:0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f440afe84">Download Code</a></p>
<p><a style="font-size:0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question1 --><!-- Start of Question2 --><a name="ques2"> </a><span class="ques"> </span></p>
<li> Write C program to print the following pattern:
<pre>*                   *
* * *           * * *
* * * * *   * * * * *
* * * * * * * * * * *</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include&lt;stdio.h&gt;
int main() {
 char prnt = '*';
 int i, j, k, s, c = 1, nos = 9;
 for (i = 1; c &lt;= 4; i++) {
     // As we want to print the columns in odd sequence viz. 1,3,5,.etc
  if ((i % 2) != 0) {
   for (j = 1; j &lt;= i; j++) {
 printf(&quot;%2c&quot;, prnt);
}
for (s = nos; s &gt;= 1; s--) { //The spacing factor
    if (c == 4 &amp;&amp; s == 1) {
     break;
    }
    printf(&quot;  &quot;);
   }
   for (k = 1; k &lt;= i; k++) {
    if (c == 4 &amp;&amp; k == 5) {
     break;
    }
    printf(&quot;%2c&quot;, prnt);
   }
   printf(&quot;\n&quot;);
   nos = nos - 4;  // controls the spacing factor
   ++c;
  }
 }
 return 0;
}</pre>
<p><a style="font-size:0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f3d273466">Download Code</a></p>
<p><a style="font-size:0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question2 --><!-- Start of Question3 --><a name="ques3"> </a><span class="ques"> </span></p>
<li>Write C program to print the following pattern:
<pre>*                         *
   *                   *
*     *             *     *
   *     *       *     *
*     *      *      *     *
   *     *       *     *
*     *             *     *
   *                   *
*                         *</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include&lt;stdio.h&gt;
int main() {
 char prnt = '*';
 int i, j, k, s, p, r, nos = 7;

 for (i = 1; i &lt;= 5; i++) {
  for (j = 1; j &lt;= i; j++) {
 if ((i % 2) != 0 &amp;&amp; (j % 2) != 0) {
printf(&quot;%3c&quot;, prnt);
}
else if ((i % 2) == 0 &amp;&amp; (j % 2) == 0) {
printf(&quot;%3c&quot;, prnt);
}
else {
printf(&quot;   &quot;);
}
}
for (s = nos; s &gt;= 1; s--) {  // for the spacing factor
   printf(&quot;   &quot;);
  }
  for (k = 1; k &lt;= i; k++) { //Joining seperate figures
if (i == 5 &amp;&amp; k == 1) {
 continue;
}
if ((k % 2) != 0) {
printf(&quot;%3c&quot;, prnt);
}
else {
printf(&quot;   &quot;);
}
}
printf(&quot;\n&quot;);
nos = nos - 2;   // space control
}  nos = 1;  // remaining half..
for (p = 4; p &gt;= 1; p--) {
  for (r = 1; r &lt;= p; r++) {
if ((p % 2) != 0 &amp;&amp; (r % 2) != 0) {
printf(&quot;%3c&quot;, prnt);
}
else if ((p % 2) == 0 &amp;&amp; (r % 2) == 0) {
printf(&quot;%3c&quot;, prnt);
}
else {
printf(&quot;   &quot;);
}
}
for (s = nos; s &gt;= 1; s--) {
   printf(&quot;   &quot;);
  }
  for (k = 1; k &lt;= p; k++) {
   if ((k % 2) != 0) {
    printf(&quot;%3c&quot;, prnt);
   }
else {
    printf(&quot;   &quot;);
   }
  }
  nos = nos + 2;  // space control
  printf(&quot;\n&quot;);
 }
 return 0;
}</pre>
<p><a style="font-size:0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=fdf2385b">Download Code</a></p>
<p><strong>Explanation:</strong><br />
This can be seen as an inverted diamond composed of stars. It can be noted that the composition of this figure follows sequential pattern of consecutive stars and spaces.</p>
<p>In case of odd row number, the odd column positions will be filled up with &#8216;*&#8217;, else a space will be spaced and vice-versa in case of even numbered row.</p>
<p>In order to achieve this we will construct four different right angle triangles</p>
<p>aligned as per the requirement.<br />
<a style="font-size:0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question3 --><!-- Start of Question4 --><a name="ques4"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre>*   *   *   *   *
  *   *   *   *
    *   *   *
      *   *
        *
      *   *
    *   *   *
  *   *   *   *
*   *   *   *   *</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include&lt;stdio.h&gt;
int main() {
 char prnt = '*';
 int i, j, s, nos = 0;
 for (i = 9; i &gt;= 1; (i = i - 2)) {
  for (s = nos; s &gt;= 1; s--) {
   printf(&quot;  &quot;);
  }
  for (j = 1; j &lt;= i; j++) {
   if ((i % 2) != 0 &amp;&amp; (j % 2) != 0) {
    printf(&quot;%2c&quot;, prnt);
   } else {
    printf(&quot;  &quot;);
   }
  }
  printf(&quot;\n&quot;);
  nos++;
 }
 nos = 3;
 for (i = 3; i &lt;= 9; (i = i + 2)) {
for (s = nos; s &gt;= 1; s--) {
   printf(&quot;  &quot;);
  }
  for (j = 1; j &lt;= i; j++) {

   if ((i % 2) != 0 &amp;&amp; (j % 2) != 0) {
    printf(&quot;%2c&quot;, prnt);
   } else {
    printf(&quot;  &quot;);
   }
  }
  nos--;
  printf(&quot;\n&quot;);
 }
 return 0;
}</pre>
<p><a style="font-size:0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f512c0854">Download Code</a><br />
<a style="font-size:0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question4 --><!-- Start of Question5 --><a name="ques5"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre>          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
    * * * * * * *
      * * * * *
        * * *
          *
        * * *
      * * * * *</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include&lt;stdio.h&gt;
int main() {
 char prnt = '*';
 int i, j, k, s, nos = 4;
 for (i = 1; i &lt;= 5; i++) {
for (s = nos; s &gt;= 1; s--) {
   printf(&quot;  &quot;);
  }
  for (j = 1; j &lt;= i; j++) {
   printf(&quot;%2c&quot;, prnt);
  }
  for (k = 1; k &lt;= (i - 1); k++) {
if (i == 1) {     continue;
}
printf(&quot;%2c&quot;, prnt);
}
 printf(&quot;\n&quot;);   nos--;
}
 nos = 1;
for (i = 4; i &gt;= 1; i--) {
  for (s = nos; s &gt;= 1; s--) {
   printf(&quot;  &quot;);
  }
  for (j = 1; j &lt;= i; j++) {
   printf(&quot;%2c&quot;, prnt);
  }
  for (k = 1; k &lt;= (i - 1); k++) {
   printf(&quot;%2c&quot;, prnt);
  }
  nos++;
  printf(&quot;\n&quot;);
 }
 nos = 3;
 for (i = 2; i &lt;= 5; i++) {
if ((i % 2) != 0) {
for (s = nos; s &gt;= 1; s--) {
    printf(&quot;  &quot;);
   }
   for (j = 1; j &lt;= i; j++) {
    printf(&quot;%2c&quot;, prnt);
   }
  }
  if ((i % 2) != 0) {
   printf(&quot;\n&quot;);
   nos--;
  }
 }
 return 0;
}</pre>
<p><a style="font-size:0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f3b8aa1f5">Download Code</a><br />
<a style="font-size:0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question5 --><!-- Start of Question6 --><a name="ques6"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre>     *
       * *
         * * *
           * * * *
         * * *
       * *
     *</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">/*
    This can be seen as two right angle triangles sharing the same base
    which is modified by adding few extra shifting spaces
*/
#include &lt;stdio.h&gt;
// This function controls the inner loop and the spacing
// factor guided by the outer loop index and the spacing index.
int triangle(int nos, int i) {
 char prnt = '*';
 int s, j;
 for (s = nos; s &gt;= 1; s--) {    // Spacing factor
  printf(&quot;  &quot;);
 }
 for (j = 1; j &lt;= i; j++) {      //The inner loop
  printf(&quot;%2c&quot;, prnt);
 }
 return 0;
}

int main() {
 int i, nos = 5;
 //draws the upper triangle
 for (i = 1; i &lt;= 4; i++) {
 triangle(nos, i);    //Inner loop construction
 nos++;              // Increments the spacing factor
 printf(&quot;\n&quot;);  }
nos = 7;  //Draws the lower triangle skipping its base.
for (i = 3; i &gt;= 1; i--) {
  int j = 1;
  triangle(nos, i);  // Inner loop construction
  nos = nos - j;     // Spacing factor
  printf(&quot;\n&quot;);
 }
 return 0;
}</pre>
<p><a style="font-size:0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f5fd7fd1e">Download Code</a><br />
<a style="font-size:0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question6 --><!-- Start of Question7 --><a name="ques7"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre>* * * * * * * * *
* * * *   * * * *
* * *       * * *
* *           * *
*               *
* *           * *
* * *       * * *
* * * *   * * * *
* * * * * * * * *</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;

int main() {
 char prnt = '*';
 int i, j, k, s, nos = -1;
 for (i = 5; i &gt;= 1; i--) {
  for (j = 1; j &lt;= i; j++) {
 printf(&quot;%2c&quot;, prnt);
}
for (s = nos; s &gt;= 1; s--) {
   printf(&quot;  &quot;);
  }
  for (k = 1; k &lt;= i; k++) {
   if (i == 5 &amp;&amp; k == 5) {
    continue;
   }
   printf(&quot;%2c&quot;, prnt);
  }
  nos = nos + 2;
  printf(&quot;\n&quot;);
 }
 nos = 5;
 for (i = 2; i &lt;= 5; i++) {
  for (j = 1; j &lt;= i; j++) {
 printf(&quot;%2c&quot;, prnt);
}
for (s = nos; s &gt;= 1; s--) {
   printf(&quot;  &quot;);
  }
  for (k = 1; k &lt;= i; k++) {
   if (i == 5 &amp;&amp; k == 5) {
    break;
   }
   printf(&quot;%2c&quot;, prnt);
  }
  nos = nos - 2;
  printf(&quot;\n&quot;);
 }
 return 0;
}</pre>
<p><a style="font-size:0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f4ed30c03">Download Code</a><br />
<a style="font-size:0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question7 --><!-- Start of Question8 --><a name="ques8"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre>* * * * * * * * * * * * * * * * *
  * * * * * * *   * * * * * * *
    * * * * *       * * * * *
      * * *           * * *
        * * * * * * * * *
          * * * * * * *
            * * * * *
              * * *
                *</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;
int main() {
 char prnt = '*';
 int i, j, k, s, sp, nos = 0, nosp = -1;
 for (i = 9; i &gt;= 3; (i = i - 2)) {
  for (s = nos; s &gt;= 1; s--) {
   printf(&quot;  &quot;);
  }
  for (j = 1; j &lt;= i; j++) {
printf(&quot;%2c&quot;, prnt);
}
for (sp = nosp; sp &gt;= 1; sp--) {
   printf(&quot;  &quot;);
  }
  for (k = 1; k &lt;= i; k++) {
 if (i == 9 &amp;&amp; k == 1) {
continue;
}
printf(&quot;%2c&quot;, prnt);
}
nos++;
nosp = nosp + 2;
printf(&quot;\n&quot;);
}
nos = 4;
for (i = 9; i &gt;= 1; (i = i - 2)) {
  for (s = nos; s &gt;= 1; s--) {
   printf(&quot;  &quot;);
  }
  for (j = 1; j &lt;= i; j++) {
   printf(&quot;%2c&quot;, prnt);
  }
  nos++;
  printf(&quot;\n&quot;);
 }

 return 0;
}</pre>
<p><a style="font-size:0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f51daa1d">Download Code</a><br />
<a style="font-size:0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question8 --><!-- Start of Question9 --><a name="ques9"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre>      *
    * * *
  * * * * *
* * * * * * *
*           *
* *       * *
* * *   * * *
* * * * * * *
* * *   * * *
* *       * *
*           *
* * * * * * *
  * * * * *
    * * *
      *</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;
/*
 * nos = Num. of spaces required in the triangle.
 * i   = Counter for the num. of charcters to print in each row
 * skip= A flag for checking whether to
 *       skip a character in a row.
 *
 */
int triangle(int nos, int i, int skip) {
 char prnt = '*';
 int s, j;
 for (s = nos; s &gt;= 1; s--) {
  printf(&quot;  &quot;);
 }
 for (j = 1; j &lt;= i; j++) {
  if (skip != 0) {
   if (i == 4 &amp;&amp; j == 1) {
    continue;
   }
  }
  printf(&quot;%2c&quot;, prnt);
 }
 return 0;
}

int main() {
 int i, nos = 4;
 for (i = 1; i &lt;= 7; (i = i + 2)) {
  triangle(nos, i, 0);
  nos--;
  printf(&quot;\n&quot;);
 }
 nos = 5;
 for (i = 1; i &lt;= 4; i++) {
triangle(1, i, 0); //one space needed in each case of the formation
triangle(nos, i, 1); //skip printing one star in the last row.
nos = nos - 2;
printf(&quot;\n&quot;);
}
nos = 1;
for (i = 3; i &gt;= 1; i--) {
  triangle(1, i, 0);
  triangle(nos, i, 0);
  nos = nos + 2;
  printf(&quot;\n&quot;);
 }
 nos = 1;
 for (i = 7; i &gt;= 1; (i = i - 2)) {
  triangle(nos, i, 0);
  nos++;
  printf(&quot;\n&quot;);
 }
 return 0;
}</pre>
<p><a style="font-size:0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f4a523771">Download Code</a><br />
<a style="font-size:0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question9 --><!-- Start of Question10 --><a name="ques10"> </a><span class="ques"> </span></p>
<li> Write a C program to print the following pattern:
<pre>* * * * * * * * * * * * * * * * * * * * * * * * *
  *           *   *           *   *           *
    *       *       *       *       *       *
      *   *           *   *           *   *
        *               *               *
      *   *           *   *           *   *
    *       *       *       *       *       *
  *           *   *           *   *           *
* * * * * * * * * * * * * * * * * * * * * * * * *</pre>
</li>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;

/*
 * nos = Num. of spaces required in the triangle.
 * i   = Counter for the num. of characters to print in each row
 * skip= A flag for check whether to
 *       skip a character in a row.
 *
 */

int triangle(int nos, int i, int skip) {
 char prnt = '*';
 int s, j;
 for (s = nos; s &gt;= 1; s--) {
  printf(&quot;  &quot;);
 }
 for (j = 1; j &lt;= i; j++) {
 if (skip != 0) {
 if (i == 9 &amp;&amp; j == 1) {
   continue;
 }
 }
if (i == 1 || i == 9) {
 printf(&quot;%2c&quot;, prnt);
}
else if (j == 1 || j == i) {
 printf(&quot;%2c&quot;, prnt);
 } else {
 printf(&quot;  &quot;);
}  }
return 0; }
int main() {
int i, nos = 0, nosp = -1, nbsp = -1;
for (i = 9; i &gt;= 1; (i = i - 2)) {
  triangle(nos, i, 0);
  triangle(nosp, i, 1);
  triangle(nbsp, i, 1);
  printf(&quot;\n&quot;);
  nos++;
  nosp = nosp + 2;
  nbsp = nbsp + 2;
 }
 nos = 3, nosp = 5, nbsp = 5;
 for (i = 3; i &lt;= 9; (i = i + 2)) {
  triangle(nos, i, 0);
  triangle(nosp, i, 1);
  triangle(nbsp, i, 1);
  printf(&quot;\n&quot;);
  nos--;
  nosp = nosp - 2;
  nbsp = nbsp - 2;
 }
 return 0;
}</pre>
<p><a style="font-size:0.8em;" href="http://nodalo.pastebin.com/pastebin.php?dl=f19dd1bb8">Download Code</a><br />
<a style="font-size:0.8em;" href="#top">Back to top</a></p>
<p><!-- End of Question10 --></ol>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2009/10/10-star-pattern-programs.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>C Interview Programs to print number patterns</title>
		<link>http://www.interviewmantra.net/2009/04/print-number-pattern-c-programs.html</link>
		<comments>http://www.interviewmantra.net/2009/04/print-number-pattern-c-programs.html#comments</comments>
		<pubDate>Tue, 07 Apr 2009 19:13:00 +0000</pubDate>
		<dc:creator>Madhulika Reddy</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[c programs]]></category>
		<category><![CDATA[pattern programs]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/2009/04/c-interview-programs-to-print-number-patterns/</guid>
		<description><![CDATA[This post has few of basic number pattern programs in C. Question.56(multiplication table) tests the ability to convert an elementary algorithm into a program. Question.57, Question.58 are programs that test the ability of candidate to use nested while/for loops to print special number patterns. Question.59, Question.60 test ability of using simple while/for loops and basics [...]]]></description>
			<content:encoded><![CDATA[<p><img style="margin: 0px 0px 10px 10px; float: right; height: 140px; width: 140px;" src="http://sites.google.com/site/sriavr/Home/c-interview-questions.png" border="0" alt="c interview questions" /><span style="font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 11px; line-height: 16px;"> This post has few of basic number pattern programs in C. <a href="#ques1">Question.56</a>(multiplication table) tests the ability to convert an elementary algorithm into a program. <a href="#ques2">Question.57</a>, <a href="#ques3">Question.58</a> are programs that test the ability of candidate to use nested while/for loops to print special number patterns. <a href="#ques4">Question.59</a>, <a href="#ques5">Question.60</a> test ability of using simple while/for loops and basics of formatting using printf statement.  <a href="http://interviewmantra.net/2008/11/list-of-all-c-interview-questions-in.html">See all C interview questions in this blog in one page</a>. </span></p>
<h3>Also Read:</p>
<ul>
<li><a href="http://www.interviewmantra.net/2009/10/10-star-pattern-programs.html">Star Pattern Programs</a></li>
<li><a href="http://www.interviewmantra.net/2009/11/10-number-pattern-programs.html">Number Pattern Programs</a></li>
</ul>
</h3>
<p>Showing <strong>5</strong> out of <a href="http://interviewmantra.net/2008/11/list-of-all-c-interview-questions-in.html"><strong>65</strong> C Interview Questions.</a></p>
<div class="Nav"><a class="Next" href="http://interviewmantra.net/2009/02/fibonacci-strcmp-strcat-programs.html">&lt; Previous Questions(51-55)</a> |  <a href="http://interviewmantra.net/2008/11/list-of-all-c-interview-questions-in.html">All</a> | <a class="Next" href="http://interviewmantra.net/2009/04/main-getchar-scanf-break-continue.html">Next Questions(61-65) &gt;</a></div>
<p><a name="top"> </a></p>
<div class="QuestionList">
<div class="QuestionsHeader">C Language Interview Questions (56 to 60)</div>
<ol>
<li><a href="#ques1">Write a program to display the multiplication table of a given number.</a></li>
<li><a href="#ques2">Write C program to print the following pattern:</a>
<pre>         1
        2 2
       3 3 3
      4 4 4 4
     5 5 5 5 5</pre>
</li>
<li><a href="#ques3">Write C program to print the following pattern: </a>
<pre>         1
       1 2 1
     1 2 3 2 1
   1 2 3 4 3 2 1
 1 2 3 4 5 4 3 2 1</pre>
</li>
<li><a href="#ques4">Write a C program to display the following format: </a>
<pre>------
  a b
------
  1 5
  2 4
  3 3
  4 2
  5 1
 ------</pre>
</li>
<li><a href="#ques5">Write a C program to display the following format: </a>
<pre>--------
 no. sum
--------
  1  1
  2  3
  3  6
  4  10
  5  15
 --------</pre>
</li>
</ol>
</div>
<div class="Nav"><a class="Next" href="http://interviewmantra.net/2009/02/fibonacci-strcmp-strcat-programs.html">&lt; Previous Questions(51-55)</a> |  <a href="http://interviewmantra.net/2008/11/list-of-all-c-interview-questions-in.html">All</a> | <a class="Next" href="http://interviewmantra.net/2009/04/main-getchar-scanf-break-continue.html">Next Questions(61-65) &gt;</a></div>
<ol><a name="ques1"> </a></p>
<li> Write a program to display the multiplication table of a given number.</li>
<p><strong>Program:</strong> Multiplication table of a given number</p>
<pre class="brush: cpp;">#include &lt;stdio.h&gt;
int main() {
      int num, i = 1;
      printf(&quot;\n Enter any Number:&quot;);
      scanf(&quot;%d&quot;, &amp;num);
      printf(&quot;Multiplication table of %d: \n&quot;, num);
      while (i &lt;= 10) {
            printf(&quot;\n %d x %d = %d&quot;, num, i, num * i);
            i++;
      }
      return 0;
}</pre>
<p><a href="http://nodalo.pastebin.com/pastebin.php?dl=f21306c4b">Download Code</a></p>
<p><strong>Output:</strong></p>
<div class="code">
<pre>Enter any Number:5
 5 x 1 = 5
 5 x 2 = 10
 5 x 3 = 15
 5 x 4 = 20
 5 x 5 = 25
 5 x 6 = 30
 5 x 7 = 35
 5 x 8 = 40
 5 x 9 = 45
 5 x 10 = 50</pre>
</div>
<p><strong>Explanation:</strong> We need to multiply the given number (i.e. the number for which we want the multiplication table) with value of &#8216;i&#8217; which increments from 1 to 10.</p>
<p><a href="#top">Back to top</a></p>
<p><a name="ques2"> </a></p>
<li> Write C program to print the following pattern:</li>
<pre>         1
        2 2
       3 3 3
      4 4 4 4
     5 5 5 5 5</pre>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include&lt;stdio.h&gt;
int main() {
    int i, j, k, c = 5;
    for (i = 1; i &lt;= 5; i++) {
            /* k is taken for spaces */
            for (k = 1; k &lt;= c; k++) {
                  /* blank space */
                  printf(&quot; &quot;);
            }
            for (j = 1; j &lt;= i; j++) {
                  /* %2d ensures that the number is printed in
two spaces for alignment and the numbers are printed in the order. */
                  printf(&quot;%2d&quot;, i);
            }
            printf(&quot;\n&quot;);
            /*c is decremented by 1 */
            c--;
      }
      return 0;
}</pre>
<p><a href="http://nodalo.pastebin.com/pastebin.php?dl=f3e890e3c">Download Code</a></p>
<p><strong>Output:</strong></p>
<div class="code">
<pre>         1
        2 2
       3 3 3
      4 4 4 4
     5 5 5 5 5</pre>
</div>
<p><strong>Explanation:</strong> Here &#8216;i&#8217; loop is used for printing the numbers in the respective rows and &#8216;k&#8217; loop is used for providing spaces. &#8216;j&#8217; loop prints the numbers. &#8216;c&#8217; is decremented for numbers to be displayed in alternate columns.</p>
<p><a href="#top">Back to top</a></p>
<p><a name="ques3"> </a></p>
<li> Write C program to print the following pattern:</li>
<pre>         1
       1 2 1
     1 2 3 2 1
   1 2 3 4 3 2 1
 1 2 3 4 5 4 3 2 1</pre>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include&lt;stdio.h&gt;
int main() {
      /* c taken for columns */
      int i, j, c = 9, m, k;
      for (i = 1; i &lt;= 5; i++) {
            /* k is used for spaces */
            for (k = 1; k &lt;= c; k++) {
                  printf(&quot; &quot;);
            }
            for (j = 1; j &lt;= i; j++) {                   printf(&quot;%2d&quot;, j);             }             for (m = j - 2; m &gt; 0; m--) {
                  /* %2d ensures that the number
                   * is printed in two spaces
                   * for alignment */
                  printf(&quot;%2d&quot;, m);
            }
            printf(&quot;\n&quot;);
            /* c is decremented by 2 */
            c = c - 2;
      }
      return 0;
}</pre>
<p><a href="http://nodalo.pastebin.com/pastebin.php?dl=f2cbd9a37">Download Code</a></p>
<p><strong>Output:</strong></p>
<div class="code">
<pre>         1
       1 2 1
     1 2 3 2 1
   1 2 3 4 3 2 1
 1 2 3 4 5 4 3 2 1</pre>
</div>
<p><strong>Explanation:</strong> Here &#8216;i&#8217; loop is used for printing numbers in rows and &#8216;k&#8217; loop is used for providing spaces. &#8216;j&#8217; loop is used for printing numbers in increasing order. &#8216;m&#8217; loop is used for printing numbers in reverse order.</p>
<p><a href="#top">Back to top</a></p>
<p><a name="ques4"> </a></p>
<li> Write a C program to display the following format:</li>
<pre>------
  a b
 ------
  1 5
  2 4
  3 3
  4 2
  5 1
 ------</pre>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include&lt;stdio.h&gt;
int main() {
      int i = 1, j = 5;
      printf(&quot;----------\n&quot;);
      printf(&quot;a \t b \n&quot;);
      printf(&quot;----------\n&quot;);
      /* logic: while loop repeats
       * 5 times i.e. until
       * the condition i&lt;=5 fails */
      while (i &lt;= 5) {
            /* i and j value printed */
            printf(&quot;%d \t %d\n&quot;, i, j);
            /* i and j value incremented
             by 1 for every iteration */
            i++;
            j--;
      }
      printf(&quot;----------&quot;);
      return 0;
}</pre>
<p><a href="http://nodalo.pastebin.com/pastebin.php?dl=f6196f1f9">Download Code</a></p>
<p><strong>Output:</strong></p>
<div class="code">
<pre>------
  a b
 ------
  1 5
  2 4
  3 3
  4 2
  5 1
 ------</pre>
</div>
<p><strong>Explanation:</strong> Here, &#8216;i&#8217; is initialized to least value 1 and &#8216;j&#8217; initialized to highest value 5. We keep incrementing the i&#8217; value and decrementing the &#8216;j&#8217; value until the condition fails. The value is displayed at each increment and at each decrement.       <a href="#top">Back to top</a></p>
<p><a name="ques5"> </a></p>
<li> Write a C program to display the following format:</li>
<pre>--------
 no. sum
 --------
  1  1
  2  3
  3  6
  4  10
  5  15
 --------</pre>
<p><strong>Program:</strong></p>
<pre class="brush: cpp;">#include&lt;stdio.h&gt;
int main() {
      int num = 1, sum = 0;
      printf(&quot;-----------\n&quot;);
      printf(&quot;num \t sum\n&quot;);
      printf(&quot;-----------\n&quot;);
      /* while loop repeats 5 times
       *  i.e. until the condition
       *  num &lt;= 5 fails */
      while (num &lt;= 5) {
            sum = sum + num;
            printf(&quot;%d \t %d\n&quot;, num, sum);
            /* num incremented by 1
             * for every time
             * the loop is executed */
            num++;
      }
      printf(&quot;-----------&quot;);
      return 0;
}</pre>
<p><a href="http://nodalo.pastebin.com/pastebin.php?dl=f30866165">Download Code</a></p>
<p><strong>Output:</strong></p>
<div class="code">
<pre>--------
 no. sum
 --------
  1  1
  2  3
  3  6
  4  10
  5  15
 --------</pre>
</div>
<p><strong>Explanation:</strong> In the above program we have taken two variables &#8216;num&#8217; and &#8216;sum&#8217;. &#8216;num&#8217; is used to check the condition and to display the numbers up to 5. &#8216;sum&#8217; is used to add the numbers which are displayed using variable &#8216;num&#8217;. The &#8216;sum&#8217; value is initialized to zero.  sum is added to the numbers which are incremented by &#8216;i&#8217; and displayed.</p>
<p><a href="#top">Back to top</a></p>
<div>Also read <a href="http://www.interviewmantra.net/2009/10/10-star-pattern-programs.html">10 Challenging Pattern Programs</a></div>
</ol>
<table id="nav" border="0" align="center">
<tbody>
<tr valign="top">
<td title="Q.No(50-55)"><a href="http://interviewmantra.net/2009/02/fibonacci-strcmp-strcat-programs.html">Previous</a></td>
<td><a href="http://interviewmantra.net/2006/06/c-c-interview-question.html">1</a></td>
<td><a href="http://interviewmantra.net/2008/10/c-interview-questions-with-solutions-1.html">2</a></td>
<td><a href="http://interviewmantra.net/2008/10/c-interview-questions-with-solutions-1_26.html">3</a></td>
<td><a href="http://interviewmantra.net/2008/10/c-interview-questions-with-solutions-2.html">4</a></td>
<td><a href="http://interviewmantra.net/2008/10/c-interview-questions-with-solutions-2_26.html">5</a></td>
<td><a href="http://interviewmantra.net/2008/10/c-interview-questions-with-solutions-3.html">6</a></td>
<td><a href="http://interviewmantra.net/2008/10/c-interview-questions-with-solutions-3_26.html">7</a></td>
<td><a href="http://interviewmantra.net/2008/12/c-interview-questions-with-solutions-4.html">8</a></td>
<td><a href="http://interviewmantra.net/2008/12/c-interview-questions-with-solutions-4.html">9</a></td>
<td><a href="http://interviewmantra.net/2009/01/c-interview-questions-answers-5-1.html">10</a></td>
<td><a href="http://interviewmantra.net/2009/02/fibonacci-strcmp-strcat-programs.html">11</a></td>
<td>12</td>
<td><a href="http://interviewmantra.net/2009/04/main-getchar-scanf-break-continue.html">13</a></td>
<td title="Q.No(61-65)"><a href="http://interviewmantra.net/2009/04/main-getchar-scanf-break-continue.html">Next</a></td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2009/04/print-number-pattern-c-programs.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
