Skip to content

Week5: Looping (Pattern Printing and 1D Array) in C

Q1. Pattern Printing (Type a)

Question:
Write a program to print the following pattern using iterative statements. Your program should ask the user for the pattern size. Use for loop.

Example for pattern type (a):


If user input is 5:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

If user input is 7:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7

````

**Program:**
```c
#include <stdio.h>

int main() {
    int size;
    printf("Enter the pattern size: ");
    scanf("%d", &size);

    for (int i = 1; i <= size; i++) {
        for (int j = 1; j <= i; j++) {
            if (j == 1) {
                printf("%d", i);
            } else {
                printf(" %d", i);
            }
        }
        printf("\n");
    }
    return 0;
}
````

---

## Q2. Pattern Printing (Type b)

**Question:**
Print a pattern where numbers decrease in each row starting from the row number to 1.

Example for pattern type (b):

If user input is 5: 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1


**Program:**

```c
#include <stdio.h>

int main() {
    int size;
    printf("Enter the pattern size: ");
    scanf("%d", &size);

    for (int i = 1; i <= size; i++) {
        for (int j = i; j >= 1; j--) {
            if (j == i) {
                printf("%d", j);
            } else {
                printf(" %d", j);
            }
        }
        printf("\n");
    }
    return 0;
}

Q3. Average Marks for Students

Question: Write a program to enter marks of N students in M subjects and calculate the average using nested loops.

Example:

Enter No of students: 5
Enter No of subjects: 3

Program:

#include <stdio.h>

int main() {
    int i, j, marks, sum, N = 3, M = 5;
    float avg;

    for(i = 1; i <= N; i++) { 
        printf("\n");
        sum = 0; 
        avg = 0.0; 

        for(j = 1; j <= M; j++) { 
            printf("Enter Marks for subject %d: ", j);
            scanf("%d", &marks);
            sum = sum + marks;
        }

        avg = sum / (float)M;
        printf("Average Marks for student %d: %.2f\n", i, avg);
    }

    return 0;
}

Q4. Print Prime Numbers from 1 to N

Question: Write a program to print all the prime numbers from 1 to N (user input).

Example:

If N = 10:
Prime numbers: 2, 3, 5, 7

Program:

#include <stdio.h>

int main() {
    int i, n, c;
    printf("Enter a number: ");
    scanf("%d", &n);

    for(i = 2; i <= n; i++) {
        c = 0;
        for(int j = 2; j <= i / 2; j++) {
            if(i % j == 0) {
                c = 1;
                break;
            }
        }
        if(c == 0) {
            printf("Prime number: %d\n", i);
        }
    }

    return 0;
}

Q5. Print Factors of Numbers from 1 to N

Question: Write a program to print factors of all numbers from 1 to N (user input).

Example:

If N = 5:
Factors of 1 = 1
Factors of 2 = 1 2
Factors of 3 = 1 3
Factors of 4 = 1 2 4
Factors of 5 = 1 5

Program:

#include <stdio.h>

int main() {
    int i, j, n;
    printf("Enter a number: ");
    scanf("%d", &n);

    for(i = 1; i <= n; i++) {
        printf("Factors of %d: ", i);
        for(j = 1; j <= i; j++) {
            if(i % j == 0) {
                printf("%d ", j);
            }
        }
        printf("\n");
    }

    return 0;
}

Comments