w3resource

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

C Basic Declarations and Expressions: Exercise-43 with Solution

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

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.