w3resource

Print p lines of q numbers in sequence starting from 1


Print p lines of q numbers in sequence starting from 1

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.

Go to:


PREV : Print numbers with their squares and cubes for n lines.
NEXT : Calculate the average of student math marks until termination.

C programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

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.