In the previous section we discussed the basics of C for loops. Now let us take a look at some real-life application Problems.
1. Write a C Program to print the following pattern
*****
****
***
**
*
#include
int main() {
int n = 5;
for(int i = n; i >= 1; i--) {
for(int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
// *****
// ****
// ***
// **
// *
| Row Iteration (i) | Condition (i >= 1) | Inner Loop (j) | Inner Condition (j <= i) | Row Output |
|---|---|---|---|---|
| i = 5 | True | 1, 2, 3, 4, 5 | 1 <= 5 .........5 <= 5 | ***** |
| i = 4 | True | 1, 2, 3, 4 | 1 <= 4..........4 <= 4 | **** |
| i = 3 | True | 1, 2, 3 | 1 <= 3..........3 <= 3 | *** |
| i = 2 | True | 1, 2 | 1 <= 2..........2 <= 2 | ** |
| i = 1 | True | 1 | 1 <= 1 | * |
| i = 0 | False | - | - | Terminate |
2. Write a C Program to print the Floyd’s Triangle
1
2 3
4 5 6
7 8 9 10
#include
int main() {
int n = 4, num = 1;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
printf("%d ", num++);
}
printf("\n");
}
return 0;
}
// 1
// 2 3
// 4 5 6
// 7 8 9 10
| Row (i) | Inner (j) | Condition (j <= i) | num++ | Row Output |
|---|---|---|---|---|
| 1 | 1 | 1 <= 1 | Print 1 then num becomes 2 | 1 |
| 2 | 1, 2 | 1 <= 2, 2<= 2 | Print 2, 3 then num becomes 4 | 2, 3 |
| 3 | 1, 2, 3 | 1 <= 3,......3 <= 3 | Print 4, 5, 6 then num becomes 7 | 4, 5, 6 |
| 4 | 1, 2, 3, 4 | 1 <= 4,......4 <= 4 | Print 7, 8, 9, 10 then num becomes 11 | 7, 8, 9, 10 |
3. Write a C Program to print the pattern below
A
A B
A B C
A B C D
#include
int main() {
int n = 4;
for(int i = 1; i <= n; i++) {
char ch = 'A';
for(int j = 1; j <= i; j++) {
printf("%c ", ch++);
}
printf("\n");
}
return 0;
}
// A
// A B
// A B C
// A B C D
| Row (i) | Inner (j) | ch Value | Action | Row Output |
|---|---|---|---|---|
| 1 | 1 | 'A' | Print 'A', ch becomes 'B' | A |
| 2 | 'B' | 2 <= 1 is False -> Reset ch | ||
| 2 | 1 | 'A' | Print 'A', ch becomes 'B' | A B |
| 2 | 'B' | Print 'B', ch becomes 'C' | ||
| 3 | 1 | 'A' | Print 'A', ch becomes 'B' | A B C |
| 2 | 'B' | Print 'B', ch becomes 'C' | ||
| 3 | 'C' | Print 'C', ch becomes 'D' | ||
| 4 | 1 | 'A' | Print 'A', ch becomes 'B' | A B C D |
| 2 | 'B' | Print 'B', ch becomes 'C' | ||
| 3 | 'C' | Print 'C', ch becomes 'D' | ||
| 4 | 'D' | Print 'D', ch becomes 'E' |
4. Write a C Program to print the below pattern
*
**
***
****
*****
#include
int main() {
int n = 5;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
/ *
// **
// ***
// ****
// *****
| Row (i) | Condition (i <= 5) | Inner (j) | Condition (j <= i) | Row Output |
|---|---|---|---|---|
| 1 | True | 1 | 1 < = 1 | * |
| 2 | True | 1, 2 | 1 <= 2, 2 <= 2 | ** |
| 3 | True | 1, 2, 3 | 1 <= 3, ......3 <= 3 | *** |
| 4 | True | 1, 2, 3, 4 | 1 <= 4, ...... 4 <= 4 | **** |
| 5 | True | 1, 2, 3, 4, 5 | 1 <= 5, .......5 <= 5 | ***** |
| 6 | False | - | - |
5. Read n integers and count how many are positive, negative and zero.
#include
int main() {
int n, num;
int pos = 0, neg = 0, zero = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter several numbers including positive, negative and zero :");
for(int i = 1; i <= n; i++) {
scanf("%d", &num);
if(num > 0)
pos++;
else if(num < 0)
neg++;
else
zero++;
}
printf("Positive: %d\nNegative: %d\nZero: %d\n", pos, neg, zero);
return 0;
}
// Enter number of elements: 5
// Enter several numbers including positive, negative and zero :5 -8 6 0 -7
// Positive: 2
// Negative: 2
// Zero: 1
6. Write a C Program to print all prime numbers in a given range
#include
int main() {
int a, b, flag;
printf("Enter range: ");
scanf("%d %d", &a, &b);
for(int num = a; num <= b; num++) {
if(num < 2) continue;
flag = 0;
for(int i = 2; i <= num / 2; i++) {
if(num % i == 0) {
flag = 1;
break;
}
}
if(flag == 0)
printf("%d ", num);
}
return 0;
}
// Enter range: 5 80
// 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79
| num | num / 2 | Inner Loop (i) | Divisibility (num % i) | flag | Result |
|---|---|---|---|---|---|
| 5 | 2 | 2 | 5 mod 2 = 1 | 0 | Prime (Print 5) |
| 6 | 3 | 2 | 6 mod 2 = 0 | 1 | Not Prime (Break) |
| 7 | 3 | 2, 3 | 7 mod 2 = 1, 7 7 mod 3 = 1 | 0 | Prime (Print 7) |
| 8 | 4 | 2 | 8 mod 2 = 0 | 1 | Not Prime (Break) |
| 9 | 4 | 2, 3 | 9 mod 2 = 1 9 mod 3 = 0 | 1 | Not Prime (Break) |
| 10 | 5 | 2 | 10 mod 2 = 0 | 1 | Not Prime (Break) |
| 11 | 5 | 2, 3, 4, 5 | No divisors equal 0 | 0 | Prime (Print 11) |
| 12 | 6 | 2 | 12 mod 2 = 0 | 1 | Not Prime (Break) |
| 13 | 6 | 2, 3, 4, 5, 6 | No divisors equal 0 | 0 | Prime (Print 13) |
| 14 | 7 | 2 | 14 mod 2 = 0 | 1 | Not Prime (Break) |
7. Write a C Program to print the multiplication table of the entered number.
#include
int main() {
int num;
printf("Enter the number: ");
scanf("%d", &num);
printf("\nMultiplication Table of %d\n", num);
for(int j = 1; j <= 10; j++) {
printf("%d x %d = %d\n", num, j, num * j);
}
return 0;
}
// Enter the number: 5
// Multiplication Table of 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