You are on page 1of 8

Department of Mechatronics and Control Engineering

University of Engineering and Technology Lahore

LAB 10: ITERATIONS IN C LANGAUGE


MCT-242L: Computer Programming-I Lab (Fall-2022)
Registration No. 2022-MC-51

OBJECTIVE:
This lab will introduce advanced concepts in loops available in C Language Program. At the end of this lab,
you should be able to:

 Understand and implement nested loops in C language program


 Learn about different loop control statements with examples

APPARATUS:
 Laptop\PC with following tools installed
o Visual Studio Code with C/C++ and Code Runner Extensions
o C/C++ mingw-w64 tools for Windows 10

LAB SUBMISSION:
Create a Visual Studio Code Workspace, Lab_10 and c files (Task_10_1.c to Task_10_10.c) for individual
tasks and add them to Lab_10 workspace.

ITRATION STATEMENTS
Nested Loops

C programming allows to use one loop inside another loop. The following section shows a few examples to
illustrate the concept.

for ( init; condition; increment ) while(condition) do


{ { {
for ( init; condition; increment while(condition) statement(s);
) { do
{ statement(s); {
statement(s); } statement(s);
} }while( condition );
statement(s);
statement(s); } }while( condition );
}
Nested for Loop Nested while Loop Nested do…while Loop

A final note on loop nesting is that you can put any type of loop inside any other type of loop. For example,
a ‘for’ loop can be inside a ‘while’ loop or vice versa.

Example 10.1: Nested loops in C

1|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

/* Example_10_1.c: Nested loops in C


---------------------------------------------------------------------------
This program demonstrates the use of Nested loops in C. It takes a number
as input form user and displays a right triangle of stars (*) on screen
with number of rows and columns (i.e. stars in base) equal to given number.
---------------------------------------------------------------------------
Written by Shujat Ali (engrshujatali@gmail.com) on 6-Oct-2021.
IDE: Visual Studio Code 1.60.0
C Compiler: GCC (Rev. 5, Built by MSYS2 Project) 10.3.0 */

#include <stdio.h>
void print_triangle(int num) {
// Prints a triangle of asterisks to the console.
for (int i = 0; i < num; i++) {
for (int j = 0; j <= i; j++) {
printf("*");
}
printf("\n");
}
}
int main() {
int num;
printf("Enter a number >> ");
scanf("%d", &num);
// Call the print_triangle() function.
print_triangle(num);
return 0;
}

// End of program
Enter a number >> 5
*
**
Program Output 1
***
****
*****
Enter a number >> 3
*
Program Output 2
**
***

TASK 10.1: Print Star Triangle [1 point]


Write a program that asks user to enter a number and prints a triangle
of * with number of base stars equal to the number. Use nested for
loops and make a function Stars_Format_1().
Sample Output 1 Enter the number >> 5

2|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

*****
****
***
**
*
Enter the number >> 3
***
Sample Output 2
**
*

Answer:
#include<stdio.h> void print_triangles(int num) { for (int i = 0; i < num; i++) { for (int j = num; j > i; j--) { prin

TASK 10.2: Print More Star Triangle [1 point]


Write a program that asks user to enter a number and prints a triangle
of * with number of base stars equal to the number. Use nested for
loops and make a function Stars_Format_2().
Enter the number >> 5
*
**
Sample Output 1
***
****
*****
Enter the number >> 3
*
Sample Output 2
**
***

Answer:

#include <stdio.h> void Stars_format_2(int num) { for (int i = 0; i < num; i++) { for (int j = 0; j <=num-i; j++) {

TASK 10.3: Print Star Triangle Again [1 point]


Write a program that asks user to enter a number and prints a triangle
of * with number of base stars equal to the number. Use nested for
loops and make a function Stars_Format_3().
Sample Output 1 Enter the number >> 5
*

3|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

**
***
****
*****
****
***
**
*
Enter the number >> 3
*
**
Sample Output 2
***
**
*

Answer:

#include <stdio.h> void Stars_format_3(int num) { for (int i = 0; i < num; i++) { for (int j = 0; j <= i; j++) { pri

TASK 10.4: Print Star Diamond [1 point]


Write a program that asks user to enter a number and prints a diamond
of * with number of base stars equal to the number. Use nested for
loops and make a function Diamond_Stars().
Enter an odd number >> 5
*
***
*****
*******
Sample Output 1
*********
*******
*****
***
*
Enter the number >> 3
*
***
Sample Output 2
*****
***
*

4|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

Answer:

#include <stdio.h> void Print_stars4(int num) { for (int i = 0; i < num; i++) { for (int k = 0; k <= num - i; k++) {

TASK 10.5: Print Number Triangle [1 point]


Write a program that asks user to enter a number and prints a triangle
of number as given in sample output. Use nested for loops and make a
function Number_triangle_1().
Enter the number >> 5
1
22
Sample Output 1
333
4444
55555
Enter the number >> 3
1
Sample Output 2
22
333

Answer:

#include <stdio.h> void Number_triangle_1(int num) { for (int i = 1; i <= num; i++) { for (int j = 1; j <= i; j++) {

TASK 10.6: Print Number Triangle (Again) [1 point]


Write a program that asks user to enter a number and prints a triangle
of number as given in sample output. Use nested for loops and make a
function Number_triangle_2().
Enter the number >> 5
1
12
Sample Output 1
123
1234
12345
Sample Output 2 Enter the number >> 3
1
12

5|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

123

Answer:

#include<stdio.h> void Number_triangle_2(int num) { for (int i = 1; i <= num; i++) { for (int j = 1; j <= i; j++) {

TASK 10.7: Make a Matrix [1 point]


Write a program that asks the user to input number of rows and column
and returns a matrix of natural numbers and make a function
Number_Matrix().
Enter the number of rows >> 3
Enter the number of columns >> 4
Sample Output 1 1 2 3 4
5 6 7 8
9 10 11 12
Enter the number of rows >> 2
Enter the number of columns >> 5
Sample Output 2
1 2 3 4 5
6 7 8 9 10

Answer:

#include <stdio.h> void Number_Matrix(int a, int b) { int i; for (int j = 1; j <= a; j++) { for (i = 1; i <= b; i++) {

TASK 10.8: Average Test Scores [1 point]


Write a program that asks the user to enter number of students and
subjects, and then asks to enter test score of each subject for each
student and returns average marks of each student and make a function
Test_Scores().
Sample Output 1 Enter the number of students >> 2
Enter the number of test scores >> 3

Enter score 1 of student 1 >> 50


Enter score 2 of student 1 >> 75
Enter score 3 of student 1 >> 81
The average for student 1 is 68.7

Enter score 1 of student 2 >> 65


Enter score 2 of student 2 >> 58

6|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

Enter score 3 of student 2 >> 83


The average for student 2 is 68.7

Answer:

#include <stdio.h> void testscores(int student, int score) { float a; for (int i = 1; i <= student; i++) { float b = 0; fo

TASK 10.9: Sum Even or Odd [1 point]


Write a program that that will take a number from user; if the number
is even, it will print sum of all even numbers from 0 to that number
and if the entered number is odd, it will print sum of all odd numbers
from 1 to that number. Furthermore, if user enters a negative number,
it should display some message e.g., negative numbers are not allowed
and make a function Even_Odd_numbers().
Enter a number >> 9
Sample Output 1
9 + 7 + 5 + 3 + 1 = 25
Enter the number >> -2
Sample Output 2
-ve numbers are not allowed.
Enter a number >> 6
Sample Output 3
6 + 4 + 2 = 12

Answer:

#include <stdio.h> void Even_Odd_Numbers(int num) { int a; if (num < 0) { printf("Negative numbers are not allow

TASK 10.10: Sum the Even/Odd [1 point]


Write a program that will ask user for 7 inputs; one by one using for
loop and will show sum of even numbers entered and sum of odd numbers
entered. Make a function Sum_Even_Odd_numbers().
Sample Output 1 Enter the 1 number >> 12
Enter the 2 number >> 4
Enter the 3 number >> 8
Enter the 4 number >> 20
Enter the 5 number >> 7
Enter the 6 number >> 6

7|Page Computer Programming-I Lab


Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore

Enter the 7 number >> 15


Sum of even numbers entered is 50
Sum of odd numbers entered is 22

Answer:

#include <stdio.h> void Sum_Even_Odd_Numbers(int num) { int a, sum1 = 0, sum2 = 0; for (int i = 0; i < num; i++) {

Students are advised to fill the manual and submit it before the upcoming lab. Kindly rename the file as
‘MCT-242L_CP1_2022_LM10_XX’, where XX is your roll number. After completing the manual, turn it in Google Classroom.

8|Page Computer Programming-I Lab

You might also like