0% found this document useful (0 votes)
17 views7 pages

Abdurahman Lab 1

Uploaded by

abdulwhy99
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)
17 views7 pages

Abdurahman Lab 1

Uploaded by

abdulwhy99
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/ 7

Data Structures and Algorithms

CS-201L
Data Structures and
Algorithms
Lab 01

Implementation of Conditional Statements, Loops


and Functions
Name: abdurahman
Roll no 21
Semester: 3rd

Presentation/Procedure Code Comments Response Total


& conclusion

Department of Electrical Engineering, WEC


Data Structures and Algorithms

Lab Tasks:

1. Display odd numbers from 0 to 100 using loop.

Algorithm:

1. Initialize a loop variable i to 1.


2. While i is less than or equal to 100:
o Print i.
o Increment i by 2.

2. Display even numbers from 0 to 100 using loop.

Algorithm:
1. Initialize a loop variable i to 2.
2. While i is less than or equal to 100:
o Print i.
o Increment i by 2.

Department of Electrical Engineering, WEC


Data Structures and Algorithms

3. Display triangle of integers using loops.

1
1 2
1 23
1 234
1 2345

Algorithm:
1. For each row i from 1 to 5:
o For each column j from 1 to i:
 Print i followed by a space.
o Print a newline after each row.

4. Add two numbers using function.

Department of Electrical Engineering, WEC


Data Structures and Algorithms

Algorithm:
1. Define a function add(a, b) that takes two integers.
2. Inside the function, return the sum of a and b.
3. In the main program, call the function with two
user-provided numbers and print the result.

5. Print Square of two numbers using function.

Algorithm:
1. Define a function square(a,b) that takes two integer.
2. Inside the function, return a * a & b*b .
3. In the main program, call the function with two
user-provided numbers and print the results.

Department of Electrical Engineering, WEC


Data Structures and Algorithms

6. Make calculator which uses 4 operators (+, -, *, /) and print results


using function. For this output should be like:

Enter any 2 numbers:


3
2
Enter any operator (+, -, *, /)
-

Subtraction of two numbers are = 1

Algorithm:
1. Define a function calc(a, b, c) that takes two integers and an
operator.
2. Inside the function, use a switch statement to perform the
operation based on the operator:
o If the operator is '+', return the sum.

o If '-', return the difference.

o If '*', return the product.

o If '/', return the quotient

3. In the main program, prompt the user for two numbers and an
operator, then call the function and print the result.

Department of Electrical Engineering, WEC


Data Structures and Algorithms

Department of Electrical Engineering, WEC


Data Structures and Algorithms

Conclusion
The tasks above outline how to perform various operations using loops and functions in
programming. They demonstrate fundamental programming concepts such as iteration, function
definition, and user input handling. By implementing these algorithms, one can create simple but
effective programs that illustrate core programming principles.

Department of Electrical Engineering, WEC

You might also like