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

Lab_4_Use_of_Different_Loops

This document outlines the objective of understanding and implementing different loop structures in programming, specifically `for`, `while`, and `do-while` loops. It provides examples of each loop type, including a multiplication table, sum of digits, and user input validation. Additionally, it includes tasks for practicing these concepts, such as calculating factorials, reversing numbers, and password validation.

Uploaded by

mufassir abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab_4_Use_of_Different_Loops

This document outlines the objective of understanding and implementing different loop structures in programming, specifically `for`, `while`, and `do-while` loops. It provides examples of each loop type, including a multiplication table, sum of digits, and user input validation. Additionally, it includes tasks for practicing these concepts, such as calculating factorials, reversing numbers, and password validation.

Uploaded by

mufassir abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 4

Use of Different Loops


Objective:
- Understand and implement different loop structures (`for`, `while`, and `do-while`).

- Learn how to use loops for iteration, calculations, and user inputs.

- Solve practical problems using loops.

Introduction:
Loops are essential programming constructs that allow the execution of a block of code
multiple times. The three primary types of loops are:

1. `for` Loop - Used when the number of iterations is known.

2. `while` Loop - Used when the number of iterations is unknown but a condition must be
met.

3. `do-while` Loop - Similar to the `while` loop but executes at least once before
condition checking.

1. for Loop Example: Printing Multiplication Table

#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

printf("Multiplication Table of %d:\n", num);


for(int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}
2. while Loop Example: Finding Sum of Digits

#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);

while (num != 0) {
sum += num % 10;
num /= 10;
}
printf("Sum of digits: %d\n", sum);
return 0;
}

3. do-while Loop Example: Validating User Input

#include <stdio.h>
int main() {
int num;
do {
printf("Enter a positive number: ");
scanf("%d", &num);
} while (num <= 0);

printf("You entered: %d\n", num);


return 0;
}

Tasks:
1. **Task 1:** Write a program using a `for` loop to calculate and print the factorial of a
given number.

2. **Task 2:** Use a `while` loop to reverse a given number (e.g., input `123` should
output `321`).

3. **Task 3:** Implement a `do-while` loop to repeatedly ask the user for a password
until they enter "password123".
Conclusion:

You might also like