Master For Loops in C: Pattern Programs and Practical Logic Problems

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 <stdio.h>

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 = 5True1, 2, 3, 4, 51 <= 5 .........5 <= 5*****
i = 4True1, 2, 3, 41 <= 4..........4 <= 4****
i = 3True1, 2, 31 <= 3..........3 <= 3***
i = 2True1, 21 <= 2..........2 <= 2**
i = 1True11 <= 1*
i = 0False--Terminate

2. Write a C Program to print the Floyd’s Triangle

1

2 3

4 5 6

7 8 9 10

				
					#include <stdio.h>

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
111 <= 1Print 1 then num becomes 21
21, 21 <= 2, 2<= 2Print 2, 3 then num becomes 42, 3
31, 2, 31 <= 3,......3 <= 3Print 4, 5, 6 then num becomes 74, 5, 6
41, 2, 3, 41 <= 4,......4 <= 4Print 7, 8, 9, 10 then num becomes 117, 8, 9, 10

3. Write a C Program to print the pattern below

A

A B

A B C

A B C D

				
					#include <stdio.h>

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 ValueActionRow Output
11'A'Print 'A', ch becomes 'B'A
2'B'2 <= 1 is False -> Reset ch
21'A'Print 'A', ch becomes 'B'A B
2'B'Print 'B', ch becomes 'C'
31'A'Print 'A', ch becomes 'B'A B C
2'B'Print 'B', ch becomes 'C'
3'C'Print 'C', ch becomes 'D'
41'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 <stdio.h>

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
1True11 < = 1*
2True1, 21 <= 2, 2 <= 2**
3True1, 2, 31 <= 3, ......3 <= 3***
4True1, 2, 3, 41 <= 4, ...... 4 <= 4****
5True1, 2, 3, 4, 51 <= 5, .......5 <= 5*****
6False--

5. Read n integers and count how many are positive, negative and zero.

				
					#include <stdio.h>

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 <stdio.h>
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 

				
			
numnum / 2Inner Loop (i)Divisibility (num % i)flagResult
5225 mod 2 = 10Prime (Print 5)
6326 mod 2 = 01Not Prime (Break)
732, 37 mod 2 = 1, 7
7 mod 3 = 1
0Prime (Print 7)
8428 mod 2 = 01Not Prime (Break)
942, 39 mod 2 = 1
9 mod 3 = 0
1Not Prime (Break)
105210 mod 2 = 0
1Not Prime (Break)
1152, 3, 4, 5No divisors equal 00Prime (Print 11)
126212 mod 2 = 01Not Prime (Break)
1362, 3, 4, 5, 6No divisors equal 00Prime (Print 13)
147214 mod 2 = 01Not Prime (Break)

7. Write a C Program to print the multiplication table of the entered number.

				
					#include <stdio.h>
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