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

Class-11- C Lab Report 123

The lab report focuses on nested loops and jumping statements in C programming, explaining their importance in controlling the flow of execution. It outlines objectives for students to understand and implement these concepts, along with practical programming examples. The report includes various programs demonstrating nested loops and jumping statements, enhancing problem-solving skills in multi-dimensional data structures.

Uploaded by

shivam9746583742
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Class-11- C Lab Report 123

The lab report focuses on nested loops and jumping statements in C programming, explaining their importance in controlling the flow of execution. It outlines objectives for students to understand and implement these concepts, along with practical programming examples. The report includes various programs demonstrating nested loops and jumping statements, enhancing problem-solving skills in multi-dimensional data structures.

Uploaded by

shivam9746583742
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Triton SS & College

Seti Opi Marga Koteshwor-32, Kathmandu

Lab Report Of Computer Science on C Programming


Lab Report No: 01
Submitted By: Submitted to:

Name: Rama Shankar Rai Computer Science Department

Roll: 31 Triton SS and College

Class : 11

Sec: 503-PHY
Lab-04: Nested Loops and Jumping Statements in C
Introduction:

The lab session focuses on understanding the concepts of nested loops and jumping statements in
the C programming language. In programming, loops and jumping statements are essential tools
for controlling the flow of execution. Loops help repeat a block of code a certain number of
times, while jumping statements provide ways to alter the normal flow of control, such as
skipping iterations or breaking out of loops entirely.

A nested loop refers to a loop inside another loop, where the inner loop runs completely for each
iteration of the outer loop. This concept is particularly useful for problems that require working
with multi-dimensional data structures like matrices or grids.

Jumping statements in C, such as break, continue, and goto, allow the programmer to
transfer control to different parts of the program. These statements play an important role in
optimizing loops or controlling flow within nested structures.

Objectives:

By the end of this lab, the student should be able to:

1. Understand and implement nested loops in C for multi-level iteration.


2. Learn the purpose and use cases for different types of jumping statements in C.
3. Practice writing programs that involve nested loops and demonstrate how jumping
statements can be used effectively.
4. Solve problems involving two-dimensional arrays or other structures requiring nested
iteration.
5. Enhance problem-solving skills by experimenting with flow control and loop
optimizations.

Theory:
1. Nested Loops:

A nested loop refers to the use of one loop inside another loop. It allows you to perform
repetitive tasks within a series of repeated tasks. For example, in matrix multiplication or
searching in multi-dimensional arrays, nested loops are commonly used.

In C, a loop can be nested inside another loop. The outer loop controls the number of times the
inner loop is executed. The basic syntax for nested loops is:
NESTED FOR LOOPS
for (initialization; condition; increment) {
// Outer loop body
for (initialization; condition; increment) {
// Inner loop body
}
}

NESTED WHILE LOOPS


while (outer_condition) {
// Outer loop body

while (inner_condition) {
// Inner loop body
}

// More outer loop body


}

NESTED DO-WHILE LOOPS


do {
// Outer loop body

do {
// Inner loop body
} while (condition2);

} while (condition1);

2. Jumping Statements:

Jumping statements are control flow statements that alter the execution path of a program. There
are three main types of jumping statements in C:

• break: The break statement is used to exit a loop or switch statement prematurely. When
break is encountered, control is transferred to the statement immediately following the
loop or switch.

• continue: The continue statement is used to skip the current iteration of a loop and jump
to the next iteration. It is often used when a condition is met, but you still want the loop
to continue with the next iteration.
• goto: The goto statement allows transferring control to another part of the program,
which can be useful in certain situations, though it is generally discouraged because it
makes code harder to follow. The goto statement can jump to a labeled statement.
Programs:

1) WAP to print the given pattern.

#include <stdio.h>
int main() {
int i, j;
// Outer loop for the number of rows
for (i = 1; i <= 5; i++) {
// Inner loop for printing stars
for (j = 1; j <= i; j++) {
printf("*");
}
// Moving to the next line after printing each row
printf("\n");
}
return 0;
}
2) WAP to print the given pattern.

#include <stdio.h>
int main() {
int i, j, spaces;
// Outer loop for the number of rows
for (i = 1; i <= 5; i++) {
// Inner loop for printing spaces
for (spaces = 1; spaces <= 5 - i; spaces++) {
printf(" ");
}
// Inner loop for printing stars
for (j = 1; j <= i; j++) {
printf("*");
}
// Move to the next line after printing each row
printf("\n");
}
return 0;
}
3) WAP to print the given pattern.

#include <stdio.h>
int main() {
int i, j;
// Outer loop for the number of rows
for (i = 5; i >= 1; i--) {
// Inner loop for printing stars
for (j = 1; j <= i; j++) {
printf("*");
}
// Move to the next line after printing each row
printf("\n");
}
return 0;
}
4) WAP to print the given pattern.

#include <stdio.h>
int main() {
int i, j, spaces;
// Outer loop for the number of rows
for (i = 1; i <= 5; i++) {
// Inner loop for printing spaces
for (spaces = 1; spaces <= 5 - i; spaces++) {
printf(" ");
}
// Inner loop for printing stars with space in between
for (j = 1; j <= i; j++) {
printf("*");
// Print a space after each star except the last one in the row
if (j < i) {
printf(" ");
}
}
// Move to the next line after printing each row
printf("\n");
}
return 0;
}
5) WAP to print the given pattern.

#include <stdio.h>
int main() {
int i, j, spaces;
// Total number of rows for the pattern
int rows = 6;
// Loop to print the pattern
for (i = rows; i >= 1; i--) {
// Loop to print spaces before stars
for (spaces = 1; spaces <= rows - i; spaces++) {
printf(" ");
}
// Loop to print stars
for (j = 1; j <= i; j++) {
printf("*");
}
// Move to the next line after each row
printf("\n");
}
return 0;
}
6) WAP to print the given pattern.

#include <stdio.h>
int main() {
int i, j;
// Outer loop to handle the number of rows
for (i = 0; i < 5; i++) {
// Inner loop for leading spaces
for (j = 0; j < i; j++) {
printf(" ");
}
// Inner loop to print stars
for (j = 5 - i; j > 0; j--) {
printf("* ");
}
// Move to the next line after each row
printf("\n");
}

return 0;
}
7) WAP to print the given pattern.

#include <stdio.h>
int main()
{
int n = 5;
// First we print upper n rows out of the total 2*n-1 rows
for (int i = 1; i <= n; i++) {
// j iterates till i to print i number of stars
// (*) in the (i)th row separated with one space
for (int j = 1; j <= i; j++) {
// Printing stars separated by one space
printf("* ");
}
// Line ends after printing i stars in (i)th row
printf("\n");
}
// Now, we print other (n-1) rows of the triangle
for (int i = 1; i <= n - 1; i++) {
// j iterates till n-i to print n-i number of stars
// (*) in the (i)th row separated with one space
for (int j = 1; j <= n - i; j++) {
// Printing stars separated by one space
printf("* ");
}
// Line ends after printing n-i stars in (i)th row
printf("\n");
}
return 0;
}
8) WAP to print the given pattern.

#include <stdio.h>
int main()
{
int n = 5;
// First, we print upper n rows out of the total 2*n-1 rows
for (int i = 1; i <= n; i++) {
// j iterates till n-i to print n-i number of spaces in the (i)th row
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
// k iterates till i to print i number of stars (*) in the (i)th row
for (int k = 1; k <= i; k++) {
printf("*");
}
// Line ends after printing i stars in (i)th row
printf("\n");
}
// Now, we print the other (n-1) rows of the triangle
for (int i = 1; i <= n - 1; i++) {
// j iterates till i to print i number of spaces in the (i)th row
for (int j = 1; j <= i; j++) {
printf(" ");
}
// k iterates till n-i to print n-i number of stars in the (i)th row
for (int k = 1; k <= n - i; k++) {
printf("*");
}
// Line ends after printing n-i stars in (i)th row
printf("\n");
}
return 0;
}
9) WAP to print the given pattern.

#include <stdio.h>
int main()
{
int n = 5;
// First, we print the upper part of the diamond (n rows)
for (int i = 1; i <= n; i++) {
// j iterates till n-i to print n-i number of spaces in the (i)th row
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
// k iterates till 2*i-1 to print 2*i-1 stars (*) in the (i)th row
for (int k = 1; k <= 2 * i - 1; k++) {
printf("*");
}
// Line ends after printing 2*i-1 stars in (i)th row
printf("\n");
}
// Now, we print the lower part of the diamond (n-1 rows)
for (int i = 1; i <= n - 1; i++) {
// j iterates till i to print i number of spaces in the (i)th row
for (int j = 1; j <= i; j++) {
printf(" ");
}
// k iterates till 2*(n-i)-1 to print 2*(n-i)-1 stars (*) in the (i)th row
for (int k = 1; k <= 2 * (n - i) - 1; k++) {
printf("*");
}
// Line ends after printing 2*(n-i)-1 stars in (i)th row
printf("\n");
}
return 0;}
10)WAP to print the given pattern.

#include <stdio.h>
int main() {
int rows = 5;
// Print the hourglass pattern
for (int i = 0; i < 2 * rows - 1; i++) {
if (i < rows) {
for (int j = 0; j < 2 * rows - i - 1; j++) {
if (j < i) {
printf(" ");
} else {
printf("* ");
}
}
} else {
for (int j = 0; j < i + 1; j++) {
if (j < 2 * rows - i - 2) {
printf(" ");
} else {
printf("* ");
}
}
}
printf("\n");
}
return 0;
}
11)WAP to print the given pattern.

#include <stdio.h>
int main() {
int rows = 4; // Number of rows in the pattern
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%d", i); // Print the current row number
}
printf("\n"); // Move to the next line after each row
}

return 0;
}
12)WAP to print the given pattern.

#include <stdio.h>
int main() {
int rows = 4; // Number of rows in the pattern
for (int i = 1; i <= rows; i++) {
// Print spaces for alignment
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
// Print numbers
int num = i; // Starting number for the current row
for (int j = 1; j <= i; j++) {
printf("%d ", num);
num++; // Increment the number
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
13)WAP to print the given pattern.

#include <stdio.h>
int main() {
int rows = 4; // Number of rows in the pattern
for (int i = 1; i <= rows; i++) {
// Print spaces for alignment
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
// Print increasing numbers
int num = i; // Starting number for the current row
for (int j = 1; j <= i; j++) {
printf("%d ", num);
num++; // Increment the number
}
// Print decreasing numbers
num -= 2; // Adjust the number to start decreasing
for (int j = 1; j < i; j++) {
printf("%d ", num);
num--; // Decrement the number
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
14)WAP to print the given pattern.

#include <stdio.h>

int main()

int rows = 4; // Define the number of rows (you can change this value as needed)

// Loop to handle the number of rows

for (int i = 0; i < rows; i++) {

// Loop to print the characters in each row

for (int j = 0; j <= i; j++) {

// Print the character corresponding to the row (starting from 'A')

printf("%c", 'A' + i);

// Move to the next line after each row

printf("\n");

return 0;

}
15)WAP to print the given pattern.

#include <stdio.h>
int main() {
int rows = 4; // Number of rows in the pattern
char currentChar = 'A'; // Starting character
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%c", currentChar); // Print the current character
currentChar++; // Move to the next character
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
16)WAP illustrating the concept of Jumping statement in C.

#include <stdio.h>
int main() {
int i;
// Example of BREAK statement
printf("Example of BREAK statement:\n");
for (i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit the loop when i equals 5
}
printf("%d ", i);
}
printf("\n\n");
// Example of CONTINUE statement
printf("Example of CONTINUE statement:\n");
for (i = 1; i <= 10; i++) {
if (i == 5) {
continue; // Skip the rest of the loop when i equals 5
}
printf("%d ", i);
}
printf("\n\n");
// Example of GOTO statement
printf("Example of GOTO statement:\n");
i = 1;
start: // Label for GOTO
if (i <= 10) {
printf("%d ", i);
i++;
goto start; // Jump back to the label 'start'
}
printf("\n");
return 0;
}
Conclusion:

Understanding nested loops and jumping statements is essential in developing efficient


algorithms and solving complex problems in programming. By mastering these concepts, you
can gain better control over the flow of your programs, making them more effective and easier to
manage.

You might also like