w3resource

Print p lines of q numbers in sequence starting from 1


C Practice Exercise

Write a C program that reads two integers p and q, prints p number of lines in a sequence of 1 to b in a line.

Sample Solution:

C Code:

#include <stdio.h>
int main() {
    int x, y, i, j, l;

    // Prompt for user input
    printf("Input number of lines: ");
    scanf("%d", &x);
    printf("Number of numbers in a line: ");
    scanf("%d", &y);

    // Loop to generate the pattern
    for(i = 1, l=1; i <= x; i++) {
        for(j = 1; j <= y; j++) {
            printf("%d ", l);		
            l++; 
        }
        printf("\n");
    }

    return 0;
}

Sample Output:

Input number of lines: 5                                               
Number of numbers in a line: 6                                      
1 2 3 4 5 6                                                            
7 8 9 10 11 12                                                         
13 14 15 16 17 18                                                      
19 20 21 22 23 24                                                      
25 26 27 28 29 30

Flowchart:

C Programming Flowchart: Reads two integers p and q, print p number of lines in a sequence of 1 to q in a line

For more Practice: Solve these Related Problems:

  • Write a C program to print a p x q matrix of sequential numbers, where the numbers in each row are in reverse order.
  • Write a C program to display a p x q grid with alternating row orders (one row ascending, the next descending).
  • Write a C program to print a rectangular matrix where each row starts with the next consecutive number from the previous row.
  • Write a C program to generate a matrix starting from a user-defined number and fill it with sequential numbers with a fixed increment.

C programming Code Editor:



Previous: Write a C program to print a number, it’s square and cube in a line, starting from 1 and print n lines. Accept number of lines (n, integer) from the user.
Next: Write a C program to calculate the average marks of mathematics of some students. Input 0 (excluding to calculate the average) or negative value to terminate the input process.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.