0
1.0kviews
Write a program to generate following patterns.

enter image description here


Mumbai university > FE > SEM 2 > Structured Programming Approach

Marks: 10M

Year: Dec 2013

1 Answer
0
2views

Pattern 1:

  • Since in pattern 1 the number starts with 5 and goes on decreasing in end to 1.

  • Given pattern is in decreasing order i.e. 5 then 4 4, 3 3 3 and so on.

  • So we will use 2 for loop for i and j.

  • Pattern is in decreasing order so we use i-- and j-- i.e. i and j decrement.

Program:

int main() {
    int i, j;
    for(i=5; i>=1; i--){
        for(j=5; j>=i; j--) {
            printf("%d", i);
        }
        printf("\n");
    }
}

Output

enter image description here

Pattern 2:

  • Since in pattern 2 the number starts with 1 and goes on increasing till 5.

  • Given pattern is in increasing order i.e. 1 then 2 2, 3 3 3 and so on.

  • So we will use 2 for loop for i and j.

  • Pattern is in increasing order so we use i++ and j++ i.e. i and j increment.

Program:

#include <stdio.h>

int main(){
    int i, j;
    for(i=1; i<=5; i++) {
        for(j=1; j<=i; j++) {
            printf("%d", i);
        }
        printf("\n");
    }
}

Output2:

enter image description here

Please log in to add an answer.