some number pattern C programs

  1. Write a C program to print the following pattern:
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
Program:
#include 
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;
}
  1. Write C program to print the following pattern:
0
1 1
2 3 5
8 13 21
Program:
#include  
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;
}
  1. Write C program to print the following pattern:
1
121
12321
1234321
12321
121
1
Program:
#include 
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);
}
}
  1. Write a C program to print the following pattern:
2
4 5 6
6 7 8 9 10
4 5 6
2
Program:
#include  
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;
}
  1. Write a C program to print the following pattern:
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
Program:
#include 
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;
}
  1. Write a C program to print the following pattern:
0
-2-3 0
-4-3-2-1 0
-2-3 0
0
Program:
#include  
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);
}
}
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;
}
  1. Write a C program to print the following pattern:
77777777777
7
7
7
7
7
7
7
7
7
7
Program:
#include 
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;
}
Explanation: This can be seen as a hollow right-angled triangle composed of 7s
  1. Write a C program to print the following pattern:
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
Program:
#include  
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;
}
  1. Write a C program to print the following pattern:
1
2 4
3 6 9
2 4
1
Program:
#include 
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
  1. Write a C program to print the following pattern:
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
Program:
#include 
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;
}
This entry was posted in C Programs and tagged . Bookmark the permalink.