You are on page 1of 6

EE-133L Programming Fundamentals

Spring 2024

Registration number 2023-EE-421


Name Musa Piracha

Looping Structure
Objective:
1. Understand and apply loop constructs (for, while, do-while) for iterative tasks.
2. Learn loop optimization techniques and enhance debugging skills for efficient and error-
free code.

Task 1:
Write a program to display the multiplication table of a given number up to 10 using a for loop.

Code:
1. # include <stdio.h>
2. int main (){
3. int table;
4. printf ("Entre the number whose table is required : ");
5. scanf (" %d", &table);
6. for (int multiple;multiple<= 10;multiple++){
7. printf(" %d x %d = %d \n",table,multiple,multiple*table);
8. }
9. return 0;
10. }
Output:

Instructor: Miss Rimsha Chauhdary


EE-133L Programming Fundamentals
Spring 2024

Explanation:
The code includes the standard input-output library “stdio.h” and defines the main
function as the program's entry point (line 1 & 2). It declares an integer variable “table” to hold
the input number for the multiplication table (line 3). A prompt asks the user to enter a number,
which is subsequently read and stored in “table” (line 4 and & 5). A for loop iterates over
multiples of the input number from 1 to 10. Inside the loop, “multiple” is initialized to 1 and
checked if it's less than or equal to 10. The multiplication table for the current “multiple” is
printed, and “multiple” is incremented by 1 in each iteration (line 6 & 7). The loop ends, and the
program returns 0 to signify successful execution (line 9).

Task 2:
Write a code to print the following pattern.
*
**
***
****

Code:
1. #include <stdio.h>
2. int main() {
3. for (int i = 1; i <= 4; i++) {
4. for (int j = 1; j <= i; j++) {
5. printf("* ");
6. }
7. printf("\n");
8. }
9. return 0 ;
10. }
Output:

Instructor: Miss Rimsha Chauhdary


EE-133L Programming Fundamentals
Spring 2024

Explanation:
This C program generates a triangular pattern of asterisks using two nested loops. The outer loop
controls the rows, iterating from 1 to 4. Within each iteration, the inner loop determines the
number of asterisks in the row, ranging from 1 to the current row number. Asterisks are printed
with a space using printf("* "). After each row is printed, a newline character is added. Finally,
the program returns 0 to indicate successful execution.

Task 3:
Create a simple number guessing game where the program generates a random number, and the
user must guess it.
 Provide feedback to the user using conditional statements based on whether their guess is
too high, too low, or correct.
 Give your user five attempts to guess right number using while loop.
 Compare this task to the last week’s task.
Hint: you can use these functions rand(), srand() and its library <stdlib.h>.
Code:
1. #include <stdio.h>
2. #include <stdlib.h>
3. int main() {
4. int random, input;
5. random = rand();
6. for (int i = 0; i < 5; i++) {
7. printf("Enter the number: ");
8. scanf("%d", & input);
9. if (input == random) {
10. printf("Your guess is correct!");
11. break;}
12. else if (input < random) {
13. printf("Entered number is smaller than the target number.\n\n");}
14. else if (input > random) {
15. printf("Entered number is larger than the target number.\n\n");}
16. if (i == 4) {
17. printf("You did not guess the number in five turns, the number was %d", random);}
18. }
19. return 0;
20. }

Instructor: Miss Rimsha Chauhdary


EE-133L Programming Fundamentals
Spring 2024

Output:

Explanation:

Task 4:
Write a program to calculate the factorial of a number given by user using a while loop.

Code:
1. #include <stdio.h>
2. int main() {
3. int number;
4. int factorial = 1;
5. printf("Enter a integer: ");
6. scanf("%d", &number);
7. int i = 1;
8. while (i <= number) {
9. factorial *= i;
10. ++i;
11. }
12. printf("Factorial of %d = %d", number, factorial);
13. return 0;
14. }

Instructor: Miss Rimsha Chauhdary


EE-133L Programming Fundamentals
Spring 2024

Output:

Explanation:
This C program calculates the factorial of a user-entered integer. It prompts the user for
input, reads the integer, and initializes a factorial variable to 1. Then, using a while loop, it
iterates from 1 to the input number, updating the factorial value by multiplying it with each
iteration. After the loop, it prints the calculated factorial. Finally, the program returns 0 to signify
successful execution.

Task 5:
Print your roll number in reverse order using do-while.

Code:
1. #include<stdio.h>
2. int main() {
3. int roll_number;
4. printf("Enter your roll number: ");
5. scanf("%d",&roll_number);
6. printf("The roll number ");
7. do {
8. printf("%d",roll_number % 10);
9. roll_number /= 10;
10. }while (roll_number != 0);
11. return 0;
12. }
Output:

Instructor: Miss Rimsha Chauhdary


EE-133L Programming Fundamentals
Spring 2024

Explanation:
This C program prompts the user to input their roll number and prints it in reverse order. It stores
the user input in an integer variable roll_number, then utilizes a do-while loop to reverse the
digits. Within the loop, it prints the last digit using printf("%d", roll_number % 10) and updates
roll_number by removing the last digit. This process repeats until all digits are printed in reverse
order. Finally, the program returns 0 to indicate successful execution.

Conclusion:
During our lab, we got into the world of C programming loops, checking out three
different types: for, while, and do-while. These loops are like tools that help us do things over
and over again in our programs. The for loop is like a straightforward friend, great for when we
know exactly how many times we want to repeat something. Then there's the while loop, which
keeps going until a certain condition is met, giving us more flexibility. And lastly, the do-while
loop makes sure something happens at least once before deciding whether to do it again. By
doing tasks like finding factorials and flipping numbers, we got better at using these loops and
understanding how they work in C programming.

Instructor: Miss Rimsha Chauhdary

You might also like