Programing Using While and For Loops
Programing Using While and For Loops
SEMESTER : 1st
SECTION : B
MARKS OBTAINED
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:
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:
TASK 2
Write a PYTHON program to print even numbers up to n
Output:
TASK 3
Write a PYTHON program to print odd numbers up to n
Output:
TASK 4
Write a PYTHON program to print sum of natural numbers up to n
Output:
TASK 5
Write a PYTHON program to print sum of odd numbers up to n
Output:
TASK 6
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:
Reference:
https://www.programiz.com/python-programming/examples/fibonacci-sequence
TASK 9
Write a PYTHON program find a factorial of given number
Output:
TASK 10
Write a PYTHON program to check the entered number is prime number or not
Output:
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:
TASK 13
Output:
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:
Output:
TASK 2
Write a PYTHON program to print even numbers up to n
Output:
TASK 3
Write a PYTHON program to print odd numbers up to n
Output:
TASK 4
Write a PYTHON program that prints 1 2 4 8 16 32 … n2
Output:
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:
TASK 8
ABC
ABC
Output:
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:
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
Output:
TASK 11
Write a PYTHON program to produce following design
12
123
1234
12345
Output:
TASK 12
22
333
4444
Output:
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/