Lab_4_Use_of_Different_Loops
Lab_4_Use_of_Different_Loops
- Learn how to use loops for iteration, calculations, and user inputs.
Introduction:
Loops are essential programming constructs that allow the execution of a block of code
multiple times. The three primary types of loops are:
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.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
#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;
}
#include <stdio.h>
int main() {
int num;
do {
printf("Enter a positive number: ");
scanf("%d", &num);
} while (num <= 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: