9/5/13
10 Challenging number pattern programs in C | Interview Mantra
10 Challenging number pattern programs in C
by U TS A V B A N E R JE E on N OV E MB E R 8 , 2 009
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.
Also read Print Pattern Programs
1. Write a C program to print the following pattern:
1
01
101
0101
10101
2. Write a C program to print the following pattern:
0
11
235
8 13 21
3. Write a C program to print the following pattern:
1
121
12321
1234321
12321
121
1
4. Write a C program to print the following pattern:
2
456
6 7 8 9 10
456
2
5. Write a C program to print the following pattern:
1
1
333
333
55555
55555
77777777777777
55555
55555
333
333
1
1
6. Write a C program to print the following pattern:
0
-2-3 0
-4-3-2-1 0
-2-3 0
0
7. Write a C program to print the following pattern:
77777777777
7
7
7
7
7
7
7
7
7
[Link]/2009/11/[Link]
1/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
7
8. Write a C program to print the following pattern:
1
1
10
10
101
101
1010
1010
10101
10101
101010 101010
1010101010101
101010 101010
10101
10101
1010
1010
101
101
10
10
1
1
9. Write a C program to print the following pattern:
1
24
369
24
1
10. Write a C program to print the following pattern:
1
10
100
1000
10000
100000
1000000
100000
10000
1000
100
10
1
1. Write a C program to print the following pattern:
1
01
101
0101
10101
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;
}
Dow n loa d Code
Explanation: This is a right angle triangle composed of 0s and 1s.
Ba ck t o t op
End of Question1 Start of Question2
2. Write C program to print the following pattern:
[Link]/2009/11/[Link]
2/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
0
11
235
8 13 21
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;
}
Dow n loa d Code
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
End of Question2 Start of Question3
3. Write C program to print the following pattern:
1
121
12321
1234321
12321
121
1
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);
}
}
[Link]/2009/11/[Link]
3/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
Dow n loa d Code
Ba ck t o t op
End of Question3 Start of Question4
4. Write a C program to print the following pattern:
2
456
6 7 8 9 10
456
2
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;
}
Dow n loa d Code
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
End of Question4 Start of Question5
5. Write a C program to print the following pattern:
1
333
55555
7777777
55555
1
333
55555
7777777
55555
[Link]/2009/11/[Link]
4/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
333
1
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;
}
Dow n loa d Code
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
End of Question5 Start of Question6
6. Write a C program to print the following pattern:
0
-2-3 0
-4-3-2-1 0
-2-3 0
0
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);
}
[Link]/2009/11/[Link]
5/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
}
if ((i % 2) != 0) {
printf("\n");
nos--;
}
}
for (k = 3; k >= 1; k--) {
if ((k % 2) != 0) {
for (sp = nosp; sp >= 1; sp--) { // for the spacing factor.
printf(" ");
}
for (r = 1; r <= k; r++) {
printf("%2d", r-k);
}
}
if ((k % 2) != 0) {
printf("\n");
nosp++;
}
}
return 0;
}
Dow n loa d Code
Explanation: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) -> j-i
where
j= inner loop index
i= outer loop index
Ba ck t o t op
End of Question6 Start of Question7
7. Write a C program to print the following pattern:
77777777777
7
7
7
7
7
7
7
7
7
7
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;
}
Dow n loa d Code
Explanation: This can be seen as a hollow right-angled triangle composed of 7s
Ba ck t o t op
End of Question7 Start of Question8
[Link]/2009/11/[Link]
6/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
8. Write a C program to print the following pattern:
1
1
10
10
101
101
1010
1010
10101
10101
101010 101010
1010101010101
101010 101010
10101
10101
1010
1010
101
101
10
10
1
1
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;
}
Dow n loa d Code
Ba ck t o t op
End of Question8 Start of Question9
9. Write a C program to print the following pattern:
1
24
[Link]/2009/11/[Link]
7/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
369
24
1
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;
}
Dow n loa d Code
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
End of Question9 Start of Question10
10. Write a C program to print the following pattern:
1
10
100
1000
10000
100000
1000000
100000
10000
1000
100
10
1
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;
[Link]/2009/11/[Link]
8/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
}
Dow n loa d Code
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
Over 1600 professionals follow Interview Mantra.
Enter your email address
Send email updates
About t he Aut hor: This post was written by Utsav Banerjee. You can reach Utsav on em ail
at fugitiv eland@gm [Link]
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
program for the below pattern pls
1
121
12321
121
1
24
Reply
t rus har
Share
> guest
2 years ago
if n=3 & ch=A
B
CE
HMU
HC
K
do this pattern using fibo ......
5
S HRE E
Reply
> guest
Share
2 years ago
CAN U GIVE THE CODING FOR THE ABOVE PATTERN PLEASE
URGENT
4
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++) {
[Link]/2009/11/[Link]
9/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
for (i = 1; i <= 5; i++) {
/* k is used for spaces */
for (k = 1; k <= c; k++) {
printf(" ");
}
for (j = 1; j <= i; j++) {
printf("%2d", j);
for (m = j - 2; m > 0; m--) {
/* %2d ensures that the number
* is printed in two spaces
* for alignment */
printf("%2d", m);
}
printf("\n");
/* c is decremented by 2 */
c = c - 2;
}
return 0;
}</stdio.h>
2
Nik unjk heni771
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++ ) {
[Link]( "*" );
}
[Link]( "" );
}
}
}
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
[Link]/2009/11/[Link]
Share
10/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
nivy a
2 years ago
i have a problem to print the following pattern
54321
4321
321
21
1
17
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
> [Link]
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");
[Link]/2009/11/[Link]
11/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
}
getch();
}</conio.h></stdio,h>
Reply
jas preet pannu
Share
2 years ago
please help me print the following pattern ;
123454321
1234 4321
123 321
12 21
11
10
ravi
Reply
Share
3 years ago
pls help me...
how can we make this pattern.
if n=5(n is inputted by user.)
then this pattern will be displayed.
55555
54444
54333
54322
54321
10
k us um
Reply
Share
2 years ago
how to print the pattern
a. 1
23
456
7 8 9 10
b1
11
121
1331
14641
plese help me
9
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
write a program to print the following not using FOR loop
1
12
[Link]/2009/11/[Link]
12/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
12
123
1234
12345
.........
in OPEN SOURCE PROGRAMMING
22
Reply
Omk ar T
Share
> Rahila Vora
a year ago
In open source program in the sense what?
4
Ravik umar
Reply
Share
2 years ago
write a c program to print the numbers in the given below formatt?
4
4
3
3
2
2
1
1
0
4
8
s arany a
Reply
Share
2 years ago
plse help me in this program
1
22
333
4444
55555
8
Reply
Share
Nik hil Nambiar
> 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
Nik hil Nambiar
Share
> saranya
2 years ago
#include<stdio.h>
main()
{
int i,j;</stdio.h>
2
pras hant
Reply
Share
2 years ago
Write a program to print the bellow pattern. . . .
*
**
***
**
*
7
aa
Reply
Share
2 years ago
[Link]/2009/11/[Link]
13/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
* *
*
* *
*
6
Reply
V inod Mis hra
Share
2 years ago
Can you give code for
1
1
1
1
1
1
21
331
4641
and so on,
thank you.
6
Reply
Ram A iran
Share
> Vinod Mishra
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
sir i have this pattern so please help me
1
22
333
4444
55555
6
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>
[Link]/2009/11/[Link]
14/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
NA V E E N
Reply
Share
3 years ago
Write a program to print the following pattern:
1
12
123
1234
12345
6
Reply
Milind Des hk ar
Share
2 years ago
i want to know the solution of this numerical pattern
1
23
456
7 8 9 10
5
Reply
S himul
Share
> Milind Deshkar
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();
[Link]/2009/11/[Link]
15/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
}</conio.h></stdio,h>
n v mahes h
Reply
Share
> Milind Deshkar
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
> Milind Deshkar
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
> Milind Deshkar
2 years ago
plz ans me
C_R_JA T013
Reply
Share
2 years ago
plz give me the code of following
1
2 2
3 3 3
4 4 4
4
7
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++)
[Link]/2009/11/[Link]
16/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
for(k=1;k<=i;k++)
printf("%d",i);
printf("\n");
}
getch();
}
Reply
Share
> C_R_JAT013
bharat
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>
Like
Reply
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
[Link]/2009/11/[Link]
Share
17/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
Craz y 145_romi
2 years ago
give me source code for this pattern
1234 4321
12 3
321
12
21
1
1
6
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
paNK A J LIMB A CHIY A
2 years ago
2
34
567
8910
PLZ SOLVE IT I M FROM BARDOLI SOLVE FAST
3
Reply
B hagy es h
Share
> paNKAJ LIMBACHIYA
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
[Link]/2009/11/[Link]
18/19
9/5/13
10 Challenging number pattern programs in C | Interview Mantra
s anjay k amdar
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
B ans als hubham257
2 years ago
give the coding of
*
* *
* * *
* *
* *
2
t arun
Reply
Share
2 years ago
Hey, can please anyone solve thisin input is 4 then:
1
12
345
12 13 14 15
where next line begins with the sum of previous line's elements and increased by 1.
2
Reply
Share
Load more comments
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
Using Google Docs Template for Resume
[Link]/2009/11/[Link]
19/19