DEPARTMENT OF AVIONICS ENGINEERING
SUBJECT : Introduction to Information Technology Lab
SUBJECT CODE : 108444
LAB NO : 07
TITLE : While and For loops
SUBMITTED TO : Sir. Laal Said
SEMESTER : 1st
SECTION : B
MARKS OBTAINED
NAME Syed Fasih Abbas
REGISTERATION NUMBER 220701054
LAB REPORT
PERFORMATION
TOTAL MARKS
DEADLINE 29 November, 2022
DATE OF SUBMISSION
OBJECTIVE:
Understand the concept for the use of Loops
What is WHILE Loop and why this is used
What is FOR Loop and why this is used
To understand the circumstances under which these loops are used
Performing tasks to understand there proper technique of use
INTRODUCTION:
Loops:
Python for loops are used to loop through an iterable object (like a list, tuple, set, etc.) and perform the same action for
each entry. For example, for a loop would allow us to iterate through a list, performing the same action on each item in the
list.
TYPES OF LOOPS:
There are three types of loops in python programming language.
While loop
For loop
Nested loop
WHILE LOOP:
In python, a while loop is used to execute a block of statements repeatedly until a given condition is satisfied. And
when the condition becomes false, the line immediately after the loop in the program is executed.
Syntax :
Example :
Figure 1
Flowchart :
As shown in figure 1
FOR LOOP:
For loops are used for sequential traversal. For example: traversing a list or string or array etc. In Python, there is no C
style for loop, i.e., for (i=0; i<n; i++). There is “for in” loop which is similar to for each loop in other languages. Let us
learn how to use for in loop for sequential traversals.
Syntax :
Example:
Flow chart:
Figure 2
As shown in figure 2
References:
https://www.geeksforgeeks.org/loops-in-python/
https://www.datacamp.com/tutorial/loops-python-tutorial
LAB TASKS
WHILE LOOP
TASK 1
Write a PYTHON program to print the natural numbers up to n
Output:
Program gives the sum of numbers
TASK 2
Write a PYTHON program to print even numbers up to n
Output:
The loop gives the list of even numbers as output
TASK 3
Write a PYTHON program to print odd numbers up to n
Output:
A list of odd numbers is generated till n
TASK 4
Write a PYTHON program to print sum of natural numbers up to n
Output:
The program will provide the sum of given numbers as an output
TASK 5
Write a PYTHON program to print sum of odd numbers up to n
Output:
The program will provide sum of all odd numbers till
user input data
TASK 6
Write a PYTHON program to print sum of even numbers up to
n
Output:
The program will print the sum of even numbers till user
input data.
TASK 7
Write a PYTHON program to print natural numbers up to n in
reverse order.
Output:
A reverse order of natural numbers is generated as an output
TASK 8
Write a PYTHON program to print Fibonacci series up to n
Output:
The program generates the Fibonacci series.
Reference:
https://www.programiz.com/python-programming/examples/fibonacci-sequence
TASK 9
Write a PYTHON program find a factorial of given number
Output:
This program provides the factorial of a number.
TASK 10
Write a PYTHON program to check the entered number is prime number or not
Output:
The program shows sum of digits of provided number
TASK 11
Write a PYTHON program to find the sum of digits of given number
Output:
TASK 12
Write a PYTHON program to check if the entered number is
palindrome or not
Output:
The program shows whether given number is a palindrome
or not
TASK 13
Write a PYTHON program to reverse the given number.
Output:
The following program reverses a given number
TASK 14
Write a PYTHON program to print the multiplication table
Output:
The programs prints the ‘multiplication table’ of a given number e.g 3 as shown
TASK 15
Write a PYTHON program to print the largest of n numbers
Output:
The program prints largest of a number among the numbers from list
TASK 16
Write a PYTHON program to print smallest of n numbers
Output:
The program shows the smallest value of the numbers as output
FOR LOOPS
TASK 1
Write a PYTHON program to print the natural numbers up to n
Output:
The program generated natural numbers as output
TASK 2
Write a PYTHON program to print even numbers up to n
Output:
The programs generated even numbers as output
TASK 3
Write a PYTHON program to print odd numbers up to n
Output:
This program gives the odd numbers
TASK 4
Write a PYTHON program that prints 1 2 4 8 16 32 … n2
Output:
The list is given by the operating program
TASK 5
Write a PYTHON program to sum the given sequence 1 + 1/ 1! + 1/ 2! + 1/3! + …. + 1/n!
Output: (COMPLICATED)
TASK 6
Write a PYTHON program to compute the cosine series cos(x) = 1 – x2 / 2! + x4 / 4! – x6 / 6! + … xn / n!
Output: (COMPLICATED)
TASK 7
Write a short PYTHON program to check whether the square root of number is
prime or not.
Output:
The program check the given sq.root number as prime or not
TASK 8
Write a PYTHON program to produce following design
ABC
ABC
ABC
Output:
The required series is given by the following program
TASK 9
Write a PYTHON program to produce following design
A, A B, A B C,A B C D , A B C D E
Output:
The required series is given by the following program
TASK 10
Write a PYTHON program to produce following design
A B C D E, A B C D, A B C, A B, A
(If user enters n value as 5)
Output:
The following programs give this design
TASK 11
Write a PYTHON program to produce following design
12
123
1234
12345
(If user enters n value as 5)
Output:
The program gives this pattern as output
TASK 12
Write a PYTHON program to produce following design
22
333
4444
5 5 5 5 5 (If user enters n value as 5)
Output:
The program provides the appropriate pattern for a given number
Conclusion:
While and for loops are fruitful in programing language and can be helpful to run ,any programs in order to
achieve our demand
Reference:
https://www.geeksforgeeks.org/python-program-for-sum-the-digits-of-a-given-number/
https://www.programiz.com/python-programming/examples/fibonacci-sequence
https://code4coding.com/python-program-to-find-factorial-of-a-number-2/