<?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; Utsav Banerjee</title>
	<atom:link href="http://www.interviewmantra.net/author/utsav-banerjee/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>Answers to Twitter Quiz -2 on C language</title>
		<link>http://www.interviewmantra.net/2009/09/twitter-c-quiz-2-answers.html</link>
		<comments>http://www.interviewmantra.net/2009/09/twitter-c-quiz-2-answers.html#comments</comments>
		<pubDate>Fri, 04 Sep 2009 10:03:00 +0000</pubDate>
		<dc:creator>Utsav Banerjee</dc:creator>
				<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/2009/09/answers-to-twitter-quiz-2-on-c-language/</guid>
		<description><![CDATA[This quiz is a past event on Twitter. Twitter Quiz is unique format of quiz conducted by Interview Mantra. It is a short quiz of 10 questions. This quiz was on C Language. Read more about Interview Mantra Twitter Quiz&#62; The Quiz questions, answers and their explanations have been consolidated as a blog post for [...]]]></description>
			<content:encoded><![CDATA[<p>This quiz is a past event on Twitter. Twitter Quiz is unique format of quiz conducted by Interview Mantra. It is a short quiz of 10 questions. This quiz was on <strong>C Language</strong>. Read more about <a href="http://interviewmantra.net/2009/07/online-quiz-3-on-c-language.html">Interview Mantra Twitter Quiz&gt;</a> <a href="http://twitter.com/sriavr"> </a></p>
<div><a href="http://twitter.com/sriavr"><img title="Follow Interview Mantra on Twitter" src="http://sites.google.com/site/sriavr/Home/followme.png" alt="Follow Interview Mantra on twitter" /> </a></div>
<p>The Quiz questions, answers and their explanations have been consolidated as a blog post for your convenience.    <span></p>
<ol>
<li>
<div><span>Question1:</span><br />
Running the C code snippet:</p>
<pre>main()
{
 printf("Hello\n");
}</pre>
<p>a)prints Hello b)compilation error c)run time error</p>
<p><span>Answer:</span> a) prints Hello</div>
</li>
<li>
<div><span>Question2:</span><br />
In C, &#8216;double&#8217; is a data type. True or False?</p>
<p><span>Answer:</span> True.<br />
C has the following basic data types: char, int, double, float.</div>
</li>
<li>
<div><span>Question3:</span><br />
The size of the basic data type, &#8216;int&#8217; in C is :  a)1 byte, b)4 bytes, c)machine dependent?</p>
<p><span>Answer:</span> c) machine dependent<br />
C compiler is free to choose the size of data types appropriate for its hardware.</div>
</li>
<li>
<div><span>Question4:</span><br />
The escape sequence &#8216;\t&#8217; when used in printf statement<br />
a) prints a new line b) prints a tab c) does nothing ?</p>
<p><span>Answer:</span> b) Prints a tab<br />
Escape sequence &#8216;\t&#8217; is used for padding a tab space</div>
</li>
<li>
<div><span>Question5:</span><br />
The default return value of a function in C (when return type not specified) is a)int, b)char, c)void ?</p>
<p><span>Answer:</span> a) int</div>
</li>
<li>
<div><span>Question6:</span><br />
In C statement,</p>
<pre>if((a==5)||(a&gt;7)),</pre>
<p>there are 2 expressions (a==5), (a&gt;7). Which of them is evaluated first? a) a==5 b) a&gt;7</p>
<p><span>Answer:</span> a) a==5</div>
</li>
<li>
<div><span>Question7:</span><br />
In C, the size of an array, once declared is constant, True or False?</p>
<p><span>Answer:</span> True</div>
</li>
<li>
<div><span>Question8:</span><br />
In a C program, there can be multiple functions with same name. True or False ?</p>
<p><span>Answer:</span> False<br />
Having same name for mulitple functions with different parameters (function overloading) is not allowed in C but is allowed in C++.</div>
</li>
<li>
<div><span>Question9:</span><br />
C functions use: a)Call by value b)Call by reference ?</p>
<p><span>Answer:</span> a) Call by value<br />
Call or Pass by reference does not exist in C, whereas C++ uses call by reference. <a href="http://bit.ly/YG9ea"> Read More</a></div>
</li>
<li>
<div><span>Question10:</span><br />
A string in C is a char array ending with a special character &#8216;\0&#8242;. True or False?</p>
<p><span>Answer:</span> True<br />
char arr[] = {&#8216;H&#8217;, &#8216;E&#8217;, &#8216;L&#8217;, &#8216;L&#8217;, &#8216;O&#8217;}; This character array &#8216;arr&#8217;, when appended with &#8216;\0&#8242; becomes a string. Example of a string, char str[]= {&#8216;H&#8217;, &#8216;E&#8217;, &#8216;L&#8217;, &#8216;L&#8217;, &#8216;O&#8217;,'\0&#8242;};</div>
</li>
</ol>
<p></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2009/09/twitter-c-quiz-2-answers.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Answers to Twitter Quiz -1 on C language</title>
		<link>http://www.interviewmantra.net/2009/08/twitter-c-quiz-1-answers.html</link>
		<comments>http://www.interviewmantra.net/2009/08/twitter-c-quiz-1-answers.html#comments</comments>
		<pubDate>Wed, 26 Aug 2009 11:49:00 +0000</pubDate>
		<dc:creator>Utsav Banerjee</dc:creator>
				<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/2009/08/answers-to-twitter-quiz-1-on-c-language/</guid>
		<description><![CDATA[This quiz is a past event on Twitter. Twitter Quiz is an innovative brain child of Interview Mantra. It is a short quiz of 10 questions. This quiz was on C Language. Read more about Interview Mantra Twitter Quiz&#62; The Quiz questions, answers and their explanations have been consolidated as a blog post for your [...]]]></description>
			<content:encoded><![CDATA[<p>This quiz is a past event on Twitter. Twitter Quiz is an innovative brain child of Interview Mantra. It is a short quiz of 10 questions. This quiz was on <strong>C Language</strong>. Read more about <a href="http://interviewmantra.net/2009/07/online-quiz-3-on-c-language.html">Interview Mantra Twitter Quiz&gt;</a> <a href="http://twitter.com/sriavr"> </a></p>
<div><a href="http://twitter.com/sriavr"><img title="Follow Interview Mantra on Twitter" src="http://sites.google.com/site/sriavr/Home/followme.png" alt="Follow Interview Mantra on twitter" /> </a></div>
<p>The Quiz questions, answers and their explanations have been consolidated as a blog post for your convenience.    <span> </span></p>
<ol>
<li>
<div><span>Question1:</span><br />
Declare an array of 3 function pointers, where each function receives  two integers and returns float.</p>
<p><span>Answer:</span> float (*fn[3])(int, int);</div>
</li>
<li>
<div><span>Question2:</span><br />
Global variables have program scope (True / False)?</p>
<p><span>Answer:</span> True</div>
</li>
<li>
<div><span>Question3:</span><br />
Size of an int variable in C is a) 2 bytes b) 4 bytes  c) machine dependent (Choose one answer)</p>
<p><span>Answer:</span> c) machine dependent</div>
</li>
<li>
<div><span>Question4:</span><br />
getchar() function does not require a return key to record. (True / False)?</p>
<p><span>Answer:</span> a) True.<br />
getchar() returns the character entered. It returns EOF in case of an error. It is recommended to use getchar instead of scanf. Using getchar() function in C, continuous stream of characters can be recorded without use of the return key.</div>
</li>
<li>
<div><span>Question5:</span><br />
break and continue statements are associated with the nearest  loop in which they are present. (True / False)?</p>
<p><span>Answer:</span> True<br />
Consider a code snippet -</p>
<pre>while(1){
 if(i&lt;5){
 break;
 }
}</pre>
<p>break statement is associated with the &#8216;while&#8217; loop and not the &#8216;if&#8217;. break and continue statements are always associated only with the nearest loop in which they are present.</p></div>
</li>
<li>
<div><span>Question6:</span><br />
static keyword limits the scope of global variables and functions to file scope (True / False)?</p>
<p><span>Answer:</span> True<br />
File scope in C means that the variable or function is visible or accessible inside a file and not among other files in same program. A C program may contain multiple source files. So, file scope is not same as program scope.</p>
<p>A program in C comprises of all the source files, header files and libraries involved. Scope means visibility to the compiler. Default scope of global variables and functions is program scope. It is limited to file scope when static keyword is prefixed.</p></div>
</li>
<li>
<div><span>Question7:</span><br />
Which of the following header files is to be included to access function: int atoi(char *s);<br />
a) stdlib.h b) ctype.h c) math.h</p>
<p><span>Answer:</span> b) ctype.h<br />
ctype.h declares some very useful character manipulation functions such as isdigit, isalpha, islower, isupper, tolower, toupper. <a href="http://en.wikipedia.org/wiki/Ctype.h">Read more about ctype.h</a>, a C standard library header file.</div>
</li>
<li>
<div><span>Question8:</span><br />
The function isdigit(9); returns non-zero? (True / False) ?</p>
<p><span>Answer:</span> b)False<br />
Note that in C, 0 is not same as &#8217;0&#8242;. 0 is an integer constant where as &#8217;0&#8242; is a character constant  that equals ASCII value of 0. isdigit function returns non-zero, only if the input lies between &#8217;0&#8242; and &#8217;9&#8242;. Else returns 0.</div>
</li>
<li>
<div><span>Question9:</span><br />
In pass by value, changes made to arguments in the called function have no effect on the values of those in the calling function. a)True or b)False?</p>
<p><span>Answer:</span> a)True<br />
In Pass by value, a copy of arguments is sent to the called function. So changes to them has no effect on the actual arguments.</div>
</li>
<li>
<div><span>Question10:</span><br />
A switch statement can be input with a pointer expression and matched against pointer case values. (True / False) ?</p>
<p><span>Answer:</span> False<br />
Switch statement can take only char or int as input. It does not entertain  pointers or non-integer expressions as inputs or cases.</div>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2009/08/twitter-c-quiz-1-answers.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Answers to Twitter Live quiz-3 on C</title>
		<link>http://www.interviewmantra.net/2009/07/c-twitter-quiz-3-answers.html</link>
		<comments>http://www.interviewmantra.net/2009/07/c-twitter-quiz-3-answers.html#comments</comments>
		<pubDate>Thu, 09 Jul 2009 16:25:00 +0000</pubDate>
		<dc:creator>Utsav Banerjee</dc:creator>
				<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/2009/07/answers-to-twitter-live-quiz-3-on-c/</guid>
		<description><![CDATA[This is a recorded twitter quiz. @ajlopez, won this quiz. More Details about this quiz Question1: In C statement, int _new_var; The variable name is not valid and it results in a compilation error. a)True or b)False? Answer: b)False In C language, variable names can start with _(underscore) or a letter but not a number. [...]]]></description>
			<content:encoded><![CDATA[<p><em>This is a recorded <a href="http://twitter.com/#search?q=%23cquiz3">twitter quiz</a>.<br />
<a href="http://twitter.com/ajlopez">@ajlopez</a>, won this quiz. <a href="http://interviewmantra.net/2009/07/online-quiz-3-on-c-language.html">More Details about this quiz</a></em> <a href="http://twitter.com/sriavr"> <img title="Follow Interview Mantra on Twitter" src="http://sites.google.com/site/sriavr/Home/followme.png" alt="Follow Interview Mantra on twitter" /> </a> <span class="headr"> </span></p>
<ol class="statuses">
<li>
<div><span class="headr">Question1:</span><br />
In C statement,<br />
int _new_var;<br />
The variable name is not valid and it results in a compilation error.<br />
a)True or b)False?</p>
<p><span class="headr">Answer:</span> b)False<br />
In C language, variable names can start with _(underscore) or a letter but not a number. Library routines are usually named with _(underscore) as first letter, so it is generally not recommended to start a variable name with underscore.</div>
</li>
<li>
<div><span class="headr">Question2:</span><br />
In C, variable name hello is same as HELLO.<br />
a)True or b)False?</p>
<p><span class="headr">Answer:</span> b)False<br />
C language is case sensitive, so variable name &#8216;hello&#8217; is not same as &#8216;Hello&#8217; or &#8216;HELLO&#8217;.</div>
</li>
<li>
<div><span class="headr">Question3:</span> Mathematically,<br />
sizeof(short) &lt;= sizeof(int) &lt;= sizeof(long)<br />
a)True or b)False ?</p>
<p><span class="headr">Answer:</span> a)True<br />
In C, there is a restriction to the compiler that shorts and ints are at least 16 bits, longs are at least 32 bits. And short is no longer than int, which is no longer than long. <a href="http://home.att.net/%7Ejackklein/c/inttypes.html#limits">Read more</a></div>
</li>
<li>
<div><span class="headr">Question4:</span> C statement,<br />
#define MAX 500<br />
is evaluated in<br />
a)compile time or b)run time ?</p>
<p><span class="headr">Answer:</span> a)compile time.<br />
#define is called a pre-processor directive, which means that even before compilation, it gets evaluated and replacement text 500 is replaced with MAX. <a href="http://en.wikipedia.org/wiki/C_preprocessor">Read more</a></div>
</li>
<li>
<div><span class="headr">Question5:</span><br />
In C, &#8216;x&#8217; is same as &#8220;x&#8221;.<br />
a)True or b)False ?</p>
<p><span class="headr">Answer:</span> b)False<br />
In C, &#8216;x&#8217; is a character constant, whereas &#8220;x&#8221; is a string literal. &#8220;x&#8221; can be thought of as an array containing characters &#8216;x&#8217; and &#8216;\0&#8242; .</div>
</li>
<li>
<div><span class="headr">Question6:</span> In C, default value of a static variable is<br />
a)0 or b)garbage value?</p>
<p><span class="headr">Answer:</span> a)0<br />
In C, static variables and external variables are initialized to 0 by default. <a href="http://interviewmantra.net/2008/10/c-interview-questions-with-solutions-2.html#staticExtern">Read more</a></div>
</li>
<li>
<div><span class="headr">Question7:</span><br />
In C, modulus operator(%) can be applied to float variables.<br />
a)True or b)False?</p>
<p><span class="headr">Answer:</span> b)False<br />
In C, modulus operator can only be applied to char or int data types. It can&#8217;t be applied to float or double.</div>
</li>
<li>
<div><span class="headr">Question8:</span><br />
The C statement,<br />
enum number{ ONE=1, UNO=1 };<br />
fails compilation as the enum values are not distinct.<br />
a)True or b)False ?</p>
<p><span class="headr">Answer:</span> b)False<br />
In enum, values need not be distinct. But names in enum need to be distinct. <a href="http://interviewmantra.net/2008/10/c-interview-questions-with-solutions-1_26.html#Enumeration">Read more about enum</a></div>
</li>
<li>
<div><span class="headr">Question9:</span><br />
In C, * (multiplication operator) has higher precedence than + or &#8211; operators.<br />
a)True or b)False?</p>
<p><span class="headr">Answer:</span> a)True<br />
The operators + and &#8211; have the same precedence, which is lower than that of *, /, % <a href="http://www.difranco.net/cop2220/op-prec.htm">Read more about precedence of operators in C</a></div>
</li>
<li>
<div><span class="headr">Question10:</span><br />
The default value of an automatic variable in C is<br />
a)0 or b)garbage value?</p>
<p><span class="headr">Answer:</span> b)garbage value<br />
Automatic variables are also called local variables. Their default value is always garbage value. <a href="http://interviewmantra.net/2008/10/c-interview-questions-with-solutions-2_26.html#auto">Read more</a>.</div>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2009/07/c-twitter-quiz-3-answers.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Twitter Live Quiz on C language -3</title>
		<link>http://www.interviewmantra.net/2009/07/online-quiz-3-on-c-language.html</link>
		<comments>http://www.interviewmantra.net/2009/07/online-quiz-3-on-c-language.html#comments</comments>
		<pubDate>Thu, 09 Jul 2009 10:05:00 +0000</pubDate>
		<dc:creator>Utsav Banerjee</dc:creator>
				<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/2009/07/twitter-live-quiz-on-c-language-3/</guid>
		<description><![CDATA[What: Interview Mantra Quiz-3 is a live, interactive quiz on twitter and facebook. 10 multiple choice questions would be asked one after another. Answer to each question would be published within 5 minutes of posting. You need to reply to the question before the answer is published by the quiz master. Topics: C language &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p><strong>What:</strong> Interview Mantra Quiz-3 is a live, interactive quiz on twitter and facebook. 10 multiple choice questions would be asked one after another. Answer to each question would be published within 5 minutes of posting. You need to reply to the question before the answer is published by the quiz master.</p>
<p><strong>Topics:</strong> C language &#8211; Variable names, Data Types and sizes, Constants, Declarations, Arithmetic Operators</p>
<p><strong>Where:</strong> <a href="http://www.twitter.com/sriavr">www.twitter.com/sriavr</a> or <a href="http://www.facebook.com/sridhar.j">www.facebook.com/sridhar.j</a></p>
<p><strong>When:</strong> July 9, 2009. <a href="http://bit.ly/tWFwN">See in your local time.</a></p>
<p><strong>Usefulness:</strong> At the end of publication of each answer, the answer would be explained along with related concepts. This is a fun way to learn C!</p>
<p>Be there to take up the challenge!</p>
<p><strong>Sample Question:</strong><br />
<img src="http://sites.google.com/site/sriavr/Home/Question.png" alt="" width="423" height="79" /></p>
<p><strong>Sample Answer:</strong><br />
<img src="http://sites.google.com/site/sriavr/Home/Answer.png" alt="" width="423" height="65" /></p>
<p><em>Update:This quiz is no longer live, it is recorded. Twitter user <a href="http://twitter.com/ajlopez">@ajlopez</a>, won the quiz with 4 correct answers out of 5 attempts. See the <a href="http://interviewmantra.net/2009/07/c-twitter-quiz-3-answers.html">follow up post on questions and answers in C quiz-3</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.interviewmantra.net/2009/07/online-quiz-3-on-c-language.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
