0% found this document useful (0 votes)
18 views

C Exercises

Uploaded by

Noshin Un Noor
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

C Exercises

Uploaded by

Noshin Un Noor
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

C Exercises: Display the

pattern like right angle using


an asterisk
• This C program generates a right-angle triangle pattern using asterisks
(*). The user specifies the number of rows for the triangle, and the
program uses nested loops to print the pattern. Each row contains an
increasing number of asterisks, starting with one asterisk in the first
row and adding one more in each subsequent row, creating a right-
angle triangle shape.
• #include <stdio.h> // Include the standard input/output header file.

• void main() {
• int i, j, rows; // Declare variables 'i' and 'j' for loop counters, 'rows' for user input.

• printf("Input number of rows : "); // Print a message to prompt user input.


• scanf("%d", &rows); // Read the value of 'rows' from the user.

• for (i = 1; i <= rows; i++) { // Start a loop to generate rows of asterisks.


• for (j = 1; j <= i; j++) // Nested loop to print asterisks based on the current row.
• printf("*"); // Print an asterisk.

• printf("\n"); // Move to the next line for the next row.


• }
• In the above for loop, the variable i is initialized to 1, and the loop will
continue as long as i is less than or equal to the value of variable 'rows'. In
each iteration of the outer loop, another loop is started with variable j,
initialized to 1, and it will continue as long as j is less than or equal to the value
of i.
• In each iteration of the inner loop, the printf() function will print an asterisk
symbol (*) to the console. This symbol will be printed i times, as the value of i
is increasing by 1 in each iteration of the outer loop.
• After the inner loop completes, the outer loop will print a newline character (\
n) to create a new line.
• Finally, the outer loop will increment the value of i by 1, and the process will
repeat until the condition i<=rows is no longer true.
C Exercises: Display the
pattern like right angle
triangle using a number
• This C program generates a right-angle triangle pattern where each
row displays numbers incrementally from 1 to the row number. It
utilizes nested “for” loops to control the number of rows and the
numbers displayed in each row, creating the desired pattern.
• #include <stdio.h> // Include the standard input/output header file.

• void main() {
• int i, j, rows; // Declare variables 'i' and 'j' for loop counters, 'rows' for user input.

• printf("Input number of rows : "); // Print a message to prompt user input.


• scanf("%d", &rows); // Read the value of 'rows' from the user.

• for (i = 1; i <= rows; i++) { // Start a loop to generate rows.


• for (j = 1; j <= i; j++) // Nested loop to print numbers based on the current row.
• printf("%d", j); // Print the value of 'j'.

• printf("\n"); // Move to the next line for the next row.


• }
• }
• In the above loop, the variable i is initialized to 1, and the loop will continue
as long as i is less than or equal to the value of variable 'rows'. In each
iteration of the outer loop, another loop is started with variable j, initialized
to 1, and it will continue as long as j is less than or equal to the value of i.
• In each iteration of the inner loop, the printf() function will print the value
of j to the console. This value will be printed j times, as the value of j is
increasing by 1 in each iteration of the inner loop.
• After the inner loop completes, the outer loop will print a newline
character (\n) to create a new line.
• Finally, the outer loop will increment the value of i by 1, and the process
will repeat until the condition i<=rows is no longer true.
C Exercises: Display the pattern like right angle
triangle which repeat a number in a row

• This C program creates a right-angle triangle pattern where each row


displays a repetition of the row number. The pattern starts with a
single digit in the first row and increases with each subsequent row.
The program utilizes nested "for" loops to control the number of rows
and repetitions of each digit, producing the desired pattern.
C Exercises: Display the pattern like right angle
triangle which repeat a number in a row

• #include <stdio.h> // Include the standard input/output header file.

• void main() {
• int i, j, rows; // Declare variables 'i' and 'j' for loop counters, 'rows' for user input.

• printf("Input number of rows : "); // Print a message to prompt user input.


• scanf("%d", &rows); // Read the value of 'rows' from the user.

• for (i = 1; i <= rows; i++) { // Start a loop to generate rows.


• for (j = 1; j <= i; j++) // Nested loop to print numbers based on the current row.
• printf("%d", i); // Print the value of 'i'.

• printf("\n"); // Move to the next line for the next row.


• }
• }
C Exercises: Display the pattern like right angle
triangle which repeat a number in a row

• In the said for loop, the variable i is initialized to 1, and the loop will continue
as long as i is less than or equal to the value of variable 'rows'. In each
iteration of the outer loop, another loop is started with variable j, initialized
to 1, and it will continue as long as j is less than or equal to the value of i.
• In each iteration of the inner loop, the printf() function will print the value of
j to the console. This value will be printed j times, as the value of j is
increasing by 1 in each iteration of the inner loop.
• After the inner loop completes, the outer loop will print a newline character
(\n) to create a new line.
• The outer loop will increment the value of i by 1, and the process will repeat
until the condition i<=rows is no longer true.
C Exercises: Display the pattern like right angle
triangle which repeat a number in a row
C Exercises: Display the pattern like right
angle triangle with number increased by 1
• This C program generates a right-angle triangle pattern where each
row displays numbers incrementally starting from 1 and increasing by
1 in each subsequent row. It employs nested loops to control the
number of rows and the numbers displayed in each row, creating the
desired pattern. This problem highlights the use of nested loops and
pattern formation techniques in C programming.
C Exercises: Display the pattern like right
angle triangle with number increased by 1
• #include <stdio.h> // Include the standard input/output header file.

• void main() {
• int i, j, rows, k = 1; // Declare variables 'i' and 'j' for loop counters, 'rows' for user input, 'k' for incrementing numbers.

• printf("Input number of rows : "); // Print a message to prompt user input.


• scanf("%d", &rows); // Read the value of 'rows' from the user.

• for (i = 1; i <= rows; i++) { // Start a loop to generate rows.


• for (j = 1; j <= i; j++) // Nested loop to print numbers based on the current row.
• printf("%d ", k++); // Print the value of 'k' and increment it.

• printf("\n"); // Move to the next line for the next row.


• }
• }
C Exercises: Display the pattern like right
angle triangle with number increased by 1
• In the above loop, the variable i is initialized to 1, and the loop will
continue as long as i is less than or equal to the value of variable 'rows'.
In each iteration of the outer loop, another loop is started with variable j,
initialized to 1, and it will continue as long as j is less than or equal to the
value of i.
• In each iteration of the inner loop, the printf function will print the value
of k to the console, followed by a space character. The value of k will be
incremented by 1 in each iteration of the inner loop using the postfix
increment operator (k++), so that each value printed is unique.
• After the inner loop completes, the outer loop will print a newline
character (\n) to create a new line.
C Exercises: Display the pattern like right
angle triangle with number increased by 1

You might also like