Sunteți pe pagina 1din 21

 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:
view source
print?
01 #include <stdio.h>
02  
03 int main(void) {
04  int i, j;

05  for (i = 0; i < 4; i++) {


06   for (j = 0; j <= i; j++) {
07    if (((i + j) % 2) == 0) {  // Decides on as to which digit to print.
08     printf("0");

09    } else {
10     printf("1");
11    }
12    printf("\t");
13   }
14   printf("\n");
15  }
16  return 0;
17 }

Download Code

Explanation: This is a right angle triangle composed of 0′s and 1′s.


Back to top

End of Question1 Start of Question2

 Write C program to print the following pattern:


0
1 1
2 3 5
8 13 21

Program:

view source
print?
01 #include <stdio.h>
02  
03 int main(void) {
04  int i, j, a = 0, b = 1, temp = 1;
05  for (i = 1; i <= 4; i++) {
06   for (j = 1; j <= i; j++) {
07    if (i == 1 && j == 1) { // Prints the '0' individually first
08     printf("0");
09     continue;
10    }
11    printf("%d ", temp);  // Prints the next digit in the series
12    //Computes the series
13    temp = a + b;
14    a = b;

15    b = temp;
16    if (i == 4 && j == 3) { // Skips the 4th character of the base
17     break;
18    }

19   }
20   printf("\n");
21  }
22  return 0;
23 }

Download Code

Explanation: This prints the Fibonacci series in a right angle triangle formation where the base
has only three characters.
Back to top

End of Question2 Start of Question3

 Write C program to print the following pattern:


1
121
12321
1234321
12321
121
1

Program:

view source
print?
01 #include <stdio.h>
02  
03 void sequence(int x);
04 int main() {
05  /* c taken for columns */
06  int i, x = 0, num = 7;
07  for (i = 1; i <= num; i++) {
08   if (i <= (num / 2) + 1) {

09    x = i;
10   } else {
11    x = 8 - i;
12   }
13   sequence(x);
14   puts("\n");

15  }
16  return 0;
17 }
18  
19 void sequence(int x) {
20  int j;

21  
22  for (j = 1; j < x; j++) {
23   printf("%d", j);
24  }
25  for (j = x; j > 0; j--) {
26   printf("%d", j);
27  }
28 }

Download Code
Back to top
End of Question3 Start of Question4

 Write a C program to print the following pattern:


2
4 5 6
6 7 8 9 10
4 5 6
2

Program:

view source
print?
01 #include <stdio.h>
02  
03 int main(void) {
04  int prnt;
 int i, j, k, r, s, sp, nos = 3, nosp = 2; //nos n nosp controls the spacing
05
factor
06  // Prints the upper triangle
07  for (i = 1; i <= 5; i++) {
08   if ((i % 2) != 0) {
09    for (s = nos; s >= 1; s--) {
10     printf("  ");

11    }
12    for (j = 1; j <= i; j++) {
13     if (i == 5 && j == 5) { //Provides the extra space reqd betn 9 n 10
14      printf(" ");        // as 10 is a 2 digit no.

15     }
16     prnt = i + j;
17     printf("%2d", prnt);
18    }

19   }
20   if ((i % 2) != 0) {
21    printf("\n");
22    nos--;
23   }
24  }
25  // Prints the lower triangle skipin its base..
26  for (k = 3; k >= 1; k--) {

27   if ((k % 2) != 0) {
28    for (sp = nosp; sp >= 1; sp--) {
29     printf("  ");
30    }
31    for (r = 1; r <= k; r++) {
32     prnt = k + r;
33     printf("%2d", prnt);
34    }

35   }
36   if ((k % 2) != 0) {
37    printf("\n");
38    nosp++;
39   }
40  }
41  return 0;
42 }

Download 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
Back to top

End of Question4 Start of Question5

 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:

view source
print?
01 #include <stdio.h>
02  
03 int main(void) {
04  int i, j, k, s, p, q, sp, r, c = 1, nos = 13;
05  for (i = 1; c <= 4; i++) {
06   if ((i % 2) != 0) {   // Filters out the even line nos.
07    for (j = 1; j <= i; j++) { // The upper left triangle
08     printf("%2d", i);

09    }
10    for (s = nos; s >= 1; s--) {  // The spacing factor
11     printf("  ");
12    }
13    for (k = 1; k <= i; k++) { // The upper right triangle
14     printf("%2d", i);

15    }
16    printf("\n");
17    nos = nos - 4;  // Space control
18    ++c;
19   }
20  }
21  nos = 10;  // Space control re intialized
22  c = 1;

23  for (p = 5; (c < 4 && p != 0); p--) {


24   if ((p % 2) != 0) {  // Filters out the even row nos
25    for (q = 1; q <= p; q++) {  // Lower left triangle
26     printf("%2d", p);

27    }
28    for (sp = nos; sp >= 1; sp--) { // Spacing factor
29     printf(" ");
30    }
31    for (r = 1; r <= p; r++) {  // Lower right triangle
32     printf("%2d", p);
33    }
34  
35    printf("\n");
36    --c;
37    nos = nos + 8;  // Spacing control.
38   }
39  }
40  
41  return 0;
42 }

Download 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 .
Back to top

End of Question5 Start of Question6

 Write a C program to print the following pattern:


0
-2-3 0
-4-3-2-1 0
-2-3 0
0

Program:

view source
print?
01 #include <stdio.h>
02  
03 int main(void) {
04  int i, j, k, r, s, sp, nos = 2, nosp = 1;
05  for (i = 1; i <= 5; i++) {
06   if ((i % 2) != 0) {
07    for (s = nos; s >= 1; s--) {  //for the spacing factor.
08     printf("  ");

09    }
10    for (j = 1; j <= i; j++) {
11     printf("%2d", j-i);
12    }

13   }
14   if ((i % 2) != 0) {
15    printf("\n");
16    nos--;
17   }
18  }
19  for (k = 3; k >= 1; k--) {
20   if ((k % 2) != 0) {
21    for (sp = nosp; sp >= 1; sp--) {  // for the spacing factor.
22     printf("  ");

23    }
24    for (r = 1; r <= k; r++) {
25     printf("%2d", r-k);
26    }

27   }
28   if ((k % 2) != 0) {
29    printf("\n");
30    nosp++;
31   }
32  }
33  return 0;
34 }

Download 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
Back to top

End of Question6 Start of Question7

 Write a C program to print the following pattern:


77777777777
7
7
7
7
7
7
7
7
7
7

Program:

view source
print?
01 #include <stdio.h>
02  
03 int main(void) {
04  int i, j;
05  for (i = 11; i >= 1; i--) {
06   for (j = 1; j <= i; j++) {
07    if (i == 11) {
08     printf("7");  // Makes sure the base is printed completely
09     continue;
10    } else if (j == i) { // Hollows the rest
11     printf("7");
12    } else {
13     printf(" ");
14    }

15   }
16   printf("\n");
17  }
18  return 0;
19 }

Download Code

Explanation: This can be seen as a hollow right-angled triangle composed of 7′s


Back to top

End of Question7 Start of Question8


 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:

view source
print?
01 #include <stdio.h>
02  
03 int main(void) {
04     int i,j,k,s,nos=11;
05     for (i=1; i<=7; i++) {
06         for (j=1; j<=i; j++) {
07             if ((j%2)!=0) {   // Applying the condition
08                 printf(" 1");

09             } else {
10                 printf(" 0");
11             }
12         }
13         for (s=nos; s>=1; s--) {  // Space factor
14             printf("  ");

15         }
16         for (k=1; k<=i; k++) {
17             if(i==7 && k==1)  // Skipping the extra 1
18             {
19                 continue;
20             }
21             if ((k%2)!=0) {  // Applying the condition
22                 printf(" 1");
23             } else {
24                 printf(" 0");
25             }
26         }

27         printf("\n");
28         nos=nos-2;  // Space Control
29     }
30      nos=1;
31      for ( i=6; i>=1; i--) {  // It shares the same base
32          for (j=1; j<=i; j++) {

33              if (j%2!=0) {
34                  printf(" 1");
35              } else {
36                  printf(" 0");
37              }
38          }
39          for(s=nos; s>=1; s--)  // Spacing factor
40          {
41              printf("  ");
42          }
43          for (k=1; k<=i; k++) {
44              if (k%2!=0) {
45                  printf(" 1");
46              } else {
47                  printf(" 0");
48              }

49          }
50          printf("\n");
51          nos=nos+2;
52      }
53     return 0;
54 }

Download Code
Back to top
End of Question8 Start of Question9

 Write a C program to print the following pattern:


1
2 4
3 6 9
2 4
1

Program:

view source
print?
01 #include <stdio.h>
02 int main(void) {

03     int i,j;
04     for (i=1; i<=3 ; i++) {
05         for (j=1; j<=i; j++)  {
06             printf("%2d", (i*j));
07         }
08         printf("\n");
09     }
10     for (i=2; i>=1; i--) { // As they share the same base
11         for (j=1; j<=i; j++)  {
12             printf("%2d",i*j);

13         }
14         printf("\n");
15     }
16     return 0;
17 }

Download 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
Back to top

End of Question9 Start of Question10


 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:

view source
print?
01 #include <stdio.h>
02  
03 int main(void) {
04     int i,j;

05     for (i=1; i<=7; i++) {


06         for (j=1; j<=i; j++) {
07             if (j==1) {           // Applying the condition
08                 printf(" 1");

09             } else {
10                 printf(" 0");
11             }
12         }
13         printf("\n");
14     }
15     for (i=6; i>=1; i--) {  //As it shares the same base i=6
16         for (j=1; j<=i; j++) {
17             if (j==1) {   // Applying the condition
18                 printf(" 1");

19             } else {
20                 printf(" 0");
21             }
22         }
23         printf("\n");
24     }
25     return 0;
26 }

Download Code

Explanation: This can be seen as two right angle triangles sharing the same base which is
composed of 0′s n 1′s. The first column is filled with 1′s and rest with 0′s

 Write a program to display the multiplication table of a given number.

Program: Multiplication table of a given number

view source

print?

01 #include <stdio.h>

02 int main() {

03       int num, i = 1;

04       printf("\n Enter any Number:");

05       scanf("%d", &num);

06       printf("Multiplication table of %d: \n", num);

07       while (i <= 10) {

08             printf("\n %d x %d = %d", num, i, num * i);

09             i++;

10       }

11       return 0;

12 }

Download Code

Output:
Enter any Number:5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Explanation: We need to multiply the given number (i.e. the number for which we want the
multiplication table) with value of ‘i’ which increments from 1 to 10.

Back to top

 Write C program to print the following pattern:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Program:

view source

print?

01 #include<stdio.h>

02 int main() {

03     int i, j, k, c = 5;

04     for (i = 1; i <= 5; i++) {

05             /* k is taken for spaces */

06             for (k = 1; k <= c; k++) {

07                   /* blank space */

08                   printf(" ");

09             }

10             for (j = 1; j <= i; j++) {

11                   /* %2d ensures that the number is printed in


12 two spaces for alignment and the numbers are printed in the order. */

13                   printf("%2d", i);

14             }

15             printf("\n");

16             /*c is decremented by 1 */

17             c--;

18       }

19       return 0;

20 }

Download Code

Output:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Explanation: Here ‘i’ loop is used for printing the numbers in the respective rows and ‘k’ loop
is used for providing spaces. ‘j’ loop prints the numbers. ‘c’ is decremented for numbers to be
displayed in alternate columns.

Back to top

 Write C program to print the following pattern:

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

Program:

view source

print?

01 #include<stdio.h>
02 int main() {

03       /* c taken for columns */

04       int i, j, c = 9, m, k;

05       for (i = 1; i <= 5; i++) {

06             /* k is used for spaces */

07             for (k = 1; k <= c; k++) {

08                   printf(" ");

09             }

            for (j = 1; j <= i; j++) {                   printf("%2d",


10
j);             }             for (m = j - 2; m > 0; m--) {

11                   /* %2d ensures that the number

12                    * is printed in two spaces

13                    * for alignment */

14                   printf("%2d", m);

15             }

16             printf("\n");

17             /* c is decremented by 2 */

18             c = c - 2;

19       }

20       return 0;

21 }

Download Code

Output:

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Explanation: Here ‘i’ loop is used for printing numbers in rows and ‘k’ loop is used for
providing spaces. ‘j’ loop is used for printing numbers in increasing order. ‘m’ loop is used for
printing numbers in reverse order.

Back to top

 Write a C program to display the following format:

------
a b
------
1 5
2 4
3 3
4 2
5 1
------

Program:

view source

print?

01 #include<stdio.h>

02 int main() {

03       int i = 1, j = 5;

04       printf("----------\n");

05       printf("a \t b \n");

06       printf("----------\n");

07       /* logic: while loop repeats

08        * 5 times i.e. until

09        * the condition i<=5 fails */

10       while (i <= 5) {

11             /* i and j value printed */

12             printf("%d \t %d\n", i, j);

13             /* i and j value incremented

14              by 1 for every iteration */

15             i++;
16             j--;

17       }

18       printf("----------");

19       return 0;

20 }

Download Code

Output:

------
a b
------
1 5
2 4
3 3
4 2
5 1
------

Explanation: Here, ‘i’ is initialized to least value 1 and ‘j’ initialized to highest value 5. We
keep incrementing the i’ value and decrementing the ‘j’ value until the condition fails. The value
is displayed at each increment and at each decrement. Back to top

 Write a C program to display the following format:

--------
no. sum
--------
1 1
2 3
3 6
4 10
5 15
--------

Program:

view source

print?

01 #include<stdio.h>
02 int main() {

03       int num = 1, sum = 0;

04       printf("-----------\n");

05       printf("num \t sum\n");

06       printf("-----------\n");

07       /* while loop repeats 5 times

08        *  i.e. until the condition

09        *  num <= 5 fails */

10       while (num <= 5) {

11             sum = sum + num;

12             printf("%d \t %d\n", num, sum);

13             /* num incremented by 1

14              * for every time

15              * the loop is executed */

16             num++;

17       }

18       printf("-----------");

19       return 0;

20 }

Download Code

Output:

--------
no. sum
--------
1 1
2 3
3 6
4 10
5 15
--------
Explanation: In the above program we have taken two variables ‘num’ and ‘sum’. ‘num’ is used
to check the condition and to display the numbers up to 5. ‘sum’ is used to add the numbers
which are displayed using variable ‘num’. The ‘sum’ value is initialized to zero. sum is added to
the numbers which are incremented by ‘i’ and displayed.

S-ar putea să vă placă și