Number problems in C # Write C program to print the following pattern: 1 22 333 4444 55555 Program: view source print? 01 02 03 04 05 06 07 08 09 10 11 12 13 } for (j = 1; j <= i; j++) { /* %2d ensures that the number is printed in two spaces for alig nment and the numbers are printed in the order. */ printf("%2d", i); #include<st dio.h> int main() { int i, j, k, c = 5; for (i = 1; i <= 5; i++) { /* k is taken for spaces */ for (k = 1; k <= c; k++) { /* blank space */ printf(" ");
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Number problems in C# Write C program to print the following pattern:
1 22 333 4444 55555
Program: view source print? 01 02 03 04 05 06 07 08 09 10 11 12 13 } for (j = 1;j <= i; j++) { /* %2d ensures that the number is printed in two spaces for alig
nment and the numbers are printed in the order. */ printf("%2d", i); #include<stdio.h> int main() { int i, j, k, c = 5; for (i = 1; i <= 5; i++) { /* k is takenfor spaces */ for (k = 1; k <= c; k++) { /* blank space */ printf(" ");
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 decrementedr numbers to be displayed in alternate columns.
Program: view source print? 01 02 03 04 05 06 07 08 09 } printf("%2d", j); } #include<stdio.h> int main() { /* c taken for columns */ int i, j, c = 9, m, k; for(i = 1; i <= 5; i++) { /* k is used for spaces */ for (k = 1; k <= c; k++) { printf(" ");
10 for (j = 1; j <= i; j++) { for (m = j - 2; m > 0; m--) { 11 12 13 14 15 16 1718 } printf("\n"); /* c is decremented by 2 */ c = c - 2;
/* %2d ensures that the number * is printed in two spaces * for alignment */ printf("%2d", m);
Explanation: Here ‘i’ loop is used for printing numbers in rows and ‘k’ loop is used forproviding 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:
Program: view source print? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 } printf("----------"); #include<stdio.h> int main() { int i = 1, j = 5; printf("----------\n"); printf("a \t b \n"); printf("----------\n"); /* logic: whileloop repeats * 5 times i.e. until * the condition i<=5 fails */ while (i <= 5) {/* i and j value printed */ printf("%d \t %d\n", i, j); /* i and j value increm
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:
Program: view source print? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 #include<stdio.h> int main() { int num = 1, sum = 0; printf("-----------\n"); printf("num \t sum\n"); printf("-----------\n"); /* while loop repeats 5 times * i.e.until the condition * num <= 5 fails */ while (num <= 5) { sum = sum + num; printf("%d \t %d\n", num, sum); /* num incremented by 1 * for every time * the loop
Explanation: In the above program we have taken two variables ‘num’ and ‘sum’. ‘num’ is usto check the condition and to display the numbers up to 5. ‘sum’ is used to add thenumbers 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.
10 most challanging pattern problems in C1. Write a C program to print the following pattern:
57 } 58 nos = nos + 2; // space control 59 printf("\n"); 60 } 61 return 0; 62}
Download Code
Explanation: This can be seen as an inverted diamond composed of stars. It can b
e noted that the composition of this figure follows sequential pattern of consecutive stars and spaces.
In case of odd row number, the odd column positions will be filled up with ‘*’, elsea space will be spaced and vice-versa in case of even numbered row.
In order to achieve this we will construct four different right angle triangles
aligned as per the requirement. Back to top
4. Write a C program to print the following pattern:
Program: view source print? 01/* 02 03 04*/ 05#include <stdio.h> 06// This function controls the inner loop and the spacing 07// factor guided by the outer loopindex and the spacing index. 08int triangle(int nos, int i) { 09 char prnt =
*
; 10 int s, j; 11 for (s = nos; s >= 1; s--) { 12 printf(" "); 13 } 14 for (j =1; j <= i; j++) { 15 printf("%2c", prnt); 16 } 17 return 0; 18} 19 20int main()
{ //The inner loop // Spacing factor This can be seen as two right angle triangles sharing the same base which is modified by adding few extra shifting spaces
21 int i, nos = 5; 22 //draws the upper triangle 23 for (i = 1; i <= 4; i++) { 24 triangle(nos, i); 25 nos++; 26 printf("\n"); } 27nos = 7; //Draws the lower triangle skipping its base. 28for (i = 3; i >= 1; i--) { 29 int j = 1; 30 triangle(nos, i); // Inner loop construction 31 nos = nos - j; 32 printf("\n"); 33 } 34return 0; 35} // Spacing factor //Inner loop construction // Increments the spacing factor
Download Code Back to top
7. Write a C program to print the following pattern:
; 05 int i, j, k, s, nos = -1; 06 for (i = 5; i >= 1; i--) { 07 for (j = 1;j <= i; j++) { 08 printf("%2c", prnt); 09} 10for (s = nos; s >= 1; s--) { 11 printf(" ");
12 } 13 for (k = 1; k <= i; k++) { 14 15 16 17 if (i == 5 && k == 5) { continue;} printf("%2c", prnt);
01#include <stdio.h> 02/* 03 * nos = Num. of spaces required in the triangle. 04* i = Counter for the num. of charcters to print in each row 05 * skip= A flagfor checking whether to 06 * 07 * 08 */ 09int triangle(int nos, int i, int skip){ 10 char prnt =
*
; 11 int s, j; 12 for (s = nos; s >= 1; s--) { 13 printf(""); 14 } 15 for (j = 1; j <= i; j++) { 16 if (skip != 0) { 17 18 19 if (i == 4 && j == 1) { continue; } skip a character in a row.
27 int i, nos = 4; 28 for (i = 1; i <= 7; (i = i + 2)) { 29 triangle(nos, i, 0);30 nos--; 31 printf("\n"); 32 } 33 nos = 5; 34 for (i = 1; i <= 4; i++) { 35triangle(1, i, 0); //one space needed in each case of the formation 36triangle(nos,i, 1); //skip printing one star in the last row. 37nos = nos - 2; 38printf("\n"); 39} 40nos = 1; 41for (i = 3; i >= 1; i--) { 42 triangle(1, i, 0); 43 triangle(nos, i, 0); 44 nos = nos + 2; 45 printf("\n"); 46 } 47 nos = 1; 48 for (i = 7;i >= 1; (i = i - 2)) { 49 triangle(nos, i, 0); 50 nos++; 51 printf("\n"); 52 }
Program: view source print? 01#include <stdio.h> 02 03/* 04 * nos = Num. of spaces required in the triangle. 05 * i = Counter for the num. of characters to print in each row
06 * skip= A flag for check whether to 07 * 08 * 09 */ 10 11int triangle(int nos, int i, int skip) { 12 char prnt =
*
; 13 int s, j; 14 for (s = nos; s >= 1; s--) { 15 printf(" "); 16 } 17 for (j = 1; j <= i; j++) { 18 if (skip != 0) { 19if (i == 9 && j == 1) { 20 continue; skip a character in a row.
individually first printf("0"); continue; } printf("%d ", temp); // Prints the next digit in the series //Computes theseries temp = a + b; a = b; b = temp; if (i == 4 && j == 3) { // Skips the 4thcharacter of the base break; } } printf("\n"); } return 0; }
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:
Program: view source print? 01 02 03 04 int main(void) { int prnt; #include <stdio.h>
05 int i, j, k, r, s, sp, nos = 3, nosp = 2; //nos n nosp controls the spacing factor 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Prints the uppertriangle 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) { //Provide
s the extra space reqd betn 9 n 10 printf(" "); } prnt = i + j; printf("%2d", prnt); } } if ((i % 2) != 0) { printf("\n"); nos--; } // as 10 is a 2 digit no.
} // 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; }
Download Code
Explanation: This is a diamond formation composed of numbers. The numbers are inthe 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
End of Question4 Start of Question5 # Write a C program to print the following pattern:
1 333 55555
1 333 55555
7777777 7777777 55555 333 1 55555 333 1
Program: view source print? 01 02 03 04 05 06 07 08 09 10 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 spacingfactor #include <stdio.h>
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 fact
or printf(" "); } for (r = 1; r <= p; r++) { // Lower right triangle printf("%2d", p); }
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:
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); }
Explanation:This can be seen as a diamond composed of numbers. If we use the con
ventional 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:
for (i=2; i>=1; i--) { // As they share the same base for (j=1; j<=i; j++) { pri
ntf("%2d",i*j);
printf("\n");
return 0;
Download Code
Explanation: This can be seen as two right angle triangles sharing th same baseThe numbers are following the following function f(x) = i *j where i = Index ofthe Outer loop j = Index of the inner loop Back to top
End of Question9 Start of Question10 # Write a C program to print the followingpattern:
for (i=6; i>=1; i--) { //As it shares the same base i=6 for (j=1; j<=i; j++) { i
f (j==1) { // Applying the condition printf(" 1"); } else { printf(" 0"); }
printf("\n");
return 0;
Download Code
Explanation: This can be seen as two right angle triangles sharing the same basewhich is composed of 0′s n 1′s. The first column is filled with 1′s and rest with 0′s