Number Pattern Challenges
Number Pattern Challenges
Number Pattern Challenges
39 Votes
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.
1
01
101
0101
10101
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
1/19
9/5/13
Program:
#include <stdio.h>
int main(void) {
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j <= i; j++) {
if (((i + j) % 2) == 0) { // Decides on as to which digit to print.
printf("0");
} else {
printf("1");
}
printf("\t");
}
printf("\n");
}
return 0;
}
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
2/19
9/5/13
Program:
#include <stdio.h>
int main(void) {
int i, j, a = 0, b = 1, temp = 1;
for (i = 1; i <= 4; i++) {
for (j = 1; j <= i; j++) {
if (i == 1 && j == 1) { // Prints the '0' individually first
printf("0");
continue;
}
printf("%d ", temp); // Prints the next digit in the series
//Computes the series
temp = a + b;
a = b;
b = temp;
if (i == 4 && j == 3) { // Skips the 4th character of the base
break;
}
}
printf("\n");
}
return 0;
}
Explanation: This prints the Fibonacci series in a right angle triangle formation where the
base has only three characters.
Ba ck t o t op
Program:
#include <stdio.h>
void sequence(int x);
int main() {
/* c taken for columns */
int i, x = 0, num = 7;
for (i = 1; i <= num; i++) {
if (i <= (num / 2) + 1) {
x = i;
} else {
x = 8 - i;
}
sequence(x);
puts("\n");
}
return 0;
}
void sequence(int x) {
int j;
for (j = 1; j < x; j++) {
printf("%d", j);
}
for (j = x; j > 0; j--) {
printf("%d", j);
}
}
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
3/19
9/5/13
Program:
#include <stdio.h>
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 <= 5; i++) {
if ((i % 2) != 0) {
for (s = nos; s >= 1; s--) {
printf(" ");
}
for (j = 1; j <= i; j++) {
if (i == 5 && j == 5) { //Provides the extra space reqd betn 9 n 10
printf(" ");
// as 10 is a 2 digit no.
}
prnt = i + j;
printf("%2d", prnt);
}
}
if ((i % 2) != 0) {
printf("\n");
nos--;
}
}
// Prints the lower triangle skipin its base..
for (k = 3; k >= 1; k--) {
if ((k % 2) != 0) {
for (sp = nosp; sp >= 1; sp--) {
printf(" ");
}
for (r = 1; r <= k; r++) {
prnt = k + r;
printf("%2d", prnt);
}
}
if ((k % 2) != 0) {
printf("\n");
nosp++;
}
}
return 0;
}
Explanation: This is a diamond formation composed of numbers. The numbers are in the
following order next_no=i+j where
next_no = The next no to be printed
i = index of the outer for loop
j = index of the inner for loop
Ba ck t o t op
1
333
55555
7777777
55555
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
4/19
9/5/13
333
1
Program:
#include <stdio.h>
int main(void) {
int i, j, k, s, p, q, sp, r, c = 1, nos = 13;
for (i = 1; c <= 4; i++) {
if ((i % 2) != 0) { // Filters out the even line nos.
for (j = 1; j <= i; j++) { // The upper left triangle
printf("%2d", i);
}
for (s = nos; s >= 1; s--) { // The spacing factor
printf(" ");
}
for (k = 1; k <= i; k++) { // The upper right triangle
printf("%2d", i);
}
printf("\n");
nos = nos - 4; // Space control
++c;
}
}
nos = 10; // Space control re intialized
c = 1;
for (p = 5; (c < 4 && p != 0); p--) {
if ((p % 2) != 0) { // Filters out the even row nos
for (q = 1; q <= p; q++) { // Lower left triangle
printf("%2d", p);
}
for (sp = nos; sp >= 1; sp--) { // Spacing factor
printf(" ");
}
for (r = 1; r <= p; r++) { // Lower right triangle
printf("%2d", p);
}
printf("\n");
--c;
nos = nos + 8; // Spacing control.
}
}
return 0;
}
Explanation: 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 .
Ba ck t o t op
Program:
#include <stdio.h>
int main(void) {
int i, j, k, r, s, sp, nos = 2, nosp = 1;
for (i = 1; i <= 5; i++) {
if ((i % 2) != 0) {
for (s = nos; s >= 1; s--) { //for the spacing factor.
printf(" ");
}
for (j = 1; j <= i; j++) {
printf("%2d", j-i);
}
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
5/19
9/5/13
Program:
#include <stdio.h>
int main(void) {
int i, j;
for (i = 11; i >= 1; i--) {
for (j = 1; j <= i; j++) {
if (i == 11) {
printf("7"); // Makes sure the base is printed completely
continue;
} else if (j == i) { // Hollows the rest
printf("7");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
6/19
9/5/13
Program:
#include <stdio.h>
int main(void) {
int i,j,k,s,nos=11;
for (i=1; i<=7; i++) {
for (j=1; j<=i; j++) {
if ((j%2)!=0) { // Applying the condition
printf(" 1");
} else {
printf(" 0");
}
}
for (s=nos; s>=1; s--) { // Space factor
printf(" ");
}
for (k=1; k<=i; k++) {
if(i==7 && k==1) // Skipping the extra 1
{
continue;
}
if ((k%2)!=0) { // Applying the condition
printf(" 1");
} else {
printf(" 0");
}
}
printf("\n");
nos=nos-2; // Space Control
}
nos=1;
for ( i=6; i>=1; i--) { // It shares the same base
for (j=1; j<=i; j++) {
if (j%2!=0) {
printf(" 1");
} else {
printf(" 0");
}
}
for(s=nos; s>=1; s--) // Spacing factor
{
printf(" ");
}
for (k=1; k<=i; k++) {
if (k%2!=0) {
printf(" 1");
} else {
printf(" 0");
}
}
printf("\n");
nos=nos+2;
}
return 0;
}
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
7/19
9/5/13
Program:
#include <stdio.h>
int main(void) {
int i,j;
for (i=1; i<=3 ; i++) {
for (j=1; j<=i; j++) {
printf("%2d", (i*j));
}
printf("\n");
}
for (i=2; i>=1; i--) { // As they share the same base
for (j=1; j<=i; j++) {
printf("%2d",i*j);
}
printf("\n");
}
return 0;
}
Explanation: This can be seen as two right angle triangles sharing th same base
The numbers are following the following function f(x) = i *j
where
i = Index of the Outer loop
j = Index of the inner loop
Ba ck t o t op
Program:
#include <stdio.h>
int main(void) {
int i,j;
for (i=1; i<=7; i++) {
for (j=1; j<=i; j++) {
if (j==1) {
// Applying the condition
printf(" 1");
} else {
printf(" 0");
}
}
printf("\n");
}
for (i=6; i>=1; i--) { //As it shares the same base i=6
for (j=1; j<=i; j++) {
if (j==1) { // Applying the condition
printf(" 1");
} else {
printf(" 0");
}
}
printf("\n");
}
return 0;
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
8/19
9/5/13
Explanation: This can be seen as two right angle triangles sharing the same base which is
composed of 0s n 1s. The first column is filled with 1s and rest with 0s
Ba ck t o t op
End of Question10
About t he Aut hor: This post was written by Utsav Banerjee. You can reach Utsav on em ail
at fugitiv eland@gm ail.com
Tagged as: pattern program s
180 comments
Leave a message...
Best
Share
Community
mut hprabha
2 years ago
*
**
***
****
19
1
gues t
Reply
Share
> muthprabha
simple c++
2 years ago
1
121
12321
121
1
24
Reply
t rus har
Share
> guest
2 years ago
B
CE
HMU
HC
K
do this pattern using fibo ......
5
S HRE E
Reply
> guest
Share
2 years ago
Neet hu
Reply
> guest
Share
2 years ago
#include<stdio.h>
int main() {
/* c taken for columns */
int i, j, c = 9, m, k;
for (i = 1; i <= 5; i++) {
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
9/19
9/5/13
Reply
Share
> muthprabha
2 years ago
void main()
{
int i,j;
for(i=1; i<=4; i++)
{
for(j=1; j<=i; j++)
{
printf("*");
}
printf("\n");
}
getch();
}
3
S hery lC.
Reply
Share
> muthprabha
2 years ago
public class (insert file name here) <-- no parenthesis... but you knew that
already ;) {
public static void main( String[] args ) {
for( int x=1; x<=4; x++ ) {
for( int y=1; y<=x; y++ ) {
System.out.print( "*" );
}
System.out.println( "" );
}
}
}
1
Reply
A rihant Jain
Share
> muthprabha
4 months ago
#include <stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf(" * ");
}
printf("\n");
}
getch();
}</conio.h></stdio.h>
Reply
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
Share
10/19
9/5/13
2 years ago
s ourav
Reply
Share
> nivya
8 months ago
#include
#include
void main()
{
int i,j,n;
clrscr();
int k=1;
printf("Enter no of line : ");
scanf("%d",&n);
for(i=0;i<n;i++) \t",k);="" for(j="0;j<=i;j++)" getch();="" k++;=""
printf("%d="" printf("\n");="" {="" }=""></n;i++)>
1
S . V . Ramana
Reply
Share
2 years ago
hi
jainam
good morning
this is code for ur output
printing like
1
23
456
7 8 9 10
#include
#include
void main()
{
int i,j,n;
clrscr();
int k=1;
printf("Enter no of line : ");
10
see more
Reply
n v mahes h
Share
> S.V.Ramana
2 months ago
#include<stdio,h>
#incldue<conio.h>
void main()
{
int i,j,k=0;
clrscr();
for(i=1;i<5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",k++);
}
printf("/n");
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
11/19
9/5/13
Reply
Share
2 years ago
ravi
Reply
Share
3 years ago
k us um
Reply
Share
2 years ago
Reply
s hwet a
Share
> kusum
11 months ago
#include <stdio.h>
int main()
{
int n, i, c, a = 1;
printf("Enter the number of rows of Floyd's triangle to print\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
printf("%d ",a);
a++;
}
printf("\n");
}
return 0;
}</stdio.h>
1
Rahila V ora
Reply
Share
2 years ago
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
12/19
9/5/13
Reply
Omk ar T
Share
a year ago
Ravik umar
Reply
Share
2 years ago
4
3
3
2
2
1
1
0
4
8
s arany a
Reply
Share
2 years ago
Reply
Share
> saranya
2 years ago
main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=i;j<=i;j++)
{
printf("%d",i);
printf("\n");
}
}
}
4
Reply
Share
> saranya
2 years ago
#include<stdio.h>
main()
{
int i,j;</stdio.h>
2
pras hant
Reply
Share
2 years ago
aa
Reply
Share
2 years ago
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
13/19
9/5/13
Reply
Share
2 years ago
1
21
331
4641
and so on,
thank you.
6
Reply
Ram A iran
Share
2 years ago
#include<stidio.h>
void main()
{
int num=1, rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
do
{
printf("%d\n",num);
num*=11;
}while(rows>0);
getch();
}</stidio.h>
2
joel
Reply
Share
3 years ago
Reply
n v mahes h
Share
> joel
2 months ago
#include<stdio,h>
#incldue<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<6;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
}
getch();
}</conio.h></stdio,h>
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
14/19
9/5/13
NA V E E N
Reply
Share
3 years ago
Reply
Milind Des hk ar
Share
2 years ago
Reply
S himul
Share
2 years ago
#include<stdio.h>
#include<conio.h>
int main()
{
int i,a,j,b=1;
printf("Enter a Size = ");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
for(j=1;j<=i;j++)
{
printf("%d", b);
b++;
}
printf("\n");
}
getch();
}
</conio.h></stdio.h>
Reply
n v mahes h
Share
> Shimul
2 months ago
#include<stdio,h>
#incldue<conio.h>
void main()
{
int i,j,k=0;
clrscr();
for(i=1;i<5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",k++);
}
printf("\n");
}
getch();
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
15/19
9/5/13
n v mahes h
Reply
Share
2 months ago
#include<stdio,h>
#incldue<conio.h>
void main()
{
int i,j,k=0;
clrscr();
for(i=1;i<5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",k++);
}
}
getch();
}</conio.h></stdio,h>
Reply
B hagy es h
Share
2 years ago
int main()
{
int i,j,a=1;
for(i=1 i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",a)
a++;
}
printf("\n");
}
}
Reply
Hardee_k h93
Share
2 years ago
plz ans me
C_R_JA T013
Reply
Share
2 years ago
Reply
B ala S aidulu
1
2 2
3 3 3
4 4 4 4
Share
> C_R_JAT013
2 years ago
main()
{
int i,j,n,k;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=4-i;j++)
printf(" ");
for(k=1;k<=i;k++)
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
16/19
9/5/13
for(k=1;k<=i;k++)
printf("%d",i);
printf("\n");
}
getch();
}
Reply
Share
> C_R_JAT013
bharat
2 years ago
Reply
Naveen1728
Share
> C_R_JAT013
2 years ago
main()
{
int i,j,n,k;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=4-i;j++)
printf(" ");
for(k=1;k<=i;k++)
printf("%d",i);
printf("\n");
}
getch();
}
m0na
Reply
Share
> C_R_JAT013
a year ago
i m not getting the result like this .. plz help me out soon
Tas heo
Reply
Share
2 years ago
program which will ask the user for a two numbers (How wide and how high) and then
generate an XO pattern.
like if user enters...3 and 4
XOX
OXO
XOX
OXO
4
Reply
Share
Omk ar T
> Tasheo
a year ago
#include<stdio.h>
void main()
{
int i, j, r, c;
int p=1;
clrscr();
printf(" \nEnter rows(r):")
scanf("%d",&r); // 4
printf("\nEnter cols(c):");
scanf("%d",&c); //3
for(i=1 ; i<=r; i++) //rows
{
for(j=1 ; j<=c ; j++) //cols
if( p%2 != 0 )
{
printf("X");
p++;
1
see more
Reply
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
Share
17/19
9/5/13
2 years ago
Reply
S . V . Ramana
Share
2 years ago
hi
jay
i have code 4 your output
let it check
#include
#include
void main()
{
int i,j,n;
clrscr();
int k=1;
printf("Enter no of line : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<4;j++)
{
printf("%d \t",k);
k++;
}
printf("\n");
}
getch();
}
3
Reply
Share
2 years ago
2
34
567
8910
PLZ SOLVE IT I M FROM BARDOLI SOLVE FAST
3
Reply
B hagy es h
Share
2 years ago
int main()
{
int i,j,n=4,a=2;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d", a);
a++;
}
printf("\n");
}
}
1
priy ank a
Reply
Share
2 years ago
1
23
456
7 8 9 10
3
Reply
Share
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
18/19
9/5/13
2 years ago
how i print following output... plz immediately solve my problem guys... i have an exam
tomorrow...
1
23
456
78
9
5
Reply
Share
2 years ago
t arun
Reply
Share
2 years ago
Reply
Share
C o m m e n t fe e d
P R E V I OU S P OS T:
You may be working for the Sun, but you are no exception
N E X T P OS T:
Su b s cri b e vi a e m a i l
www.interviewmantra.net/2009/11/10-number-pattern-programs.html
19/19