0% found this document useful (0 votes)
53 views3 pages

Python Control Statements Guide

The document contains Python code snippets and explanations for using control statements in Python. It includes programs to find the factorial of a number using a while loop, print the alphabet pattern 'E', and check if a number is a palindrome. For each question, it provides the aim, algorithm, code, and output.

Uploaded by

jeffychrista13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views3 pages

Python Control Statements Guide

The document contains Python code snippets and explanations for using control statements in Python. It includes programs to find the factorial of a number using a while loop, print the alphabet pattern 'E', and check if a number is a palindrome. For each question, it provides the aim, algorithm, code, and output.

Uploaded by

jeffychrista13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Ex.No.

1
USES OF CONTROL STATEMENTS IN PYTHON Reg.No: URK23CS5063
13.12.23
1a.Question
Write a Python program to find the factorial of a number using while loop

Aim:
The aim of this program is to write a python program to find the factorial of a number using while loop.

Algorithm:
Step1: Start the program
Step2: Initialize a variable n and prompt the user to input the number to find factorial
Step3: Initialize another variable result with an initial value of 0 to store the result
Step4: Initialize a while loop with the condition that the input number should be greater than 1
Step5: Multiply the result by I and assign the result to the variable result
Step6: Increment i by 1 and print the value the factorial
Step7: Stop the program

Program:
n = int(input("enter a number to find factorial:"))
result = 1
i=1
while n >= i:
result *= i
i += 1
print(f"The factorial of {n} is {result}")

Output: (Screenshots)

Result:
Python program to find the factorial of a number using while loop is written and executed successfully.

1
1b.Question: Write a Python program to print alphabet pattern 'E'
*****
*
*
****
*
*
*****

Aim:
The aim of this program is to write and execute a python code to is to print the alphabet pattern ‘E’.

Algorithm:
Step1: Start the program.
Step2: Print 5 ‘*’ in the first and last line
Step3: Initialize 2 for loops to print the vertical “*”
Step4: End the program.

Program:
print("*"*5)

for i in range (2):


print('*')

print("*"*4)

for i in range (2):


print('*')

print('*'*5)

Output(Screenshots):

Result:
Python program to print the alphabet ‘E’ is written and executed successfully.

2
1c.Question: Write a python program to check whether the number is palindrome or not.

Aim:
The aim of this program to write and execute a python code to check whether the number is palindrome or
not.

Algorithm:
Step1: Initialize a variable a and prompt the user to input the number to check it is palindrome
Step2: Convert the number to a string and store it in the variable ‘string_number’
Step3: Reverse the number and strore it in the variable ‘reversed_number’
Step4: Check whether the ‘string_number’ is equal to the ‘reversed_number’ using the if-else conditional
Step5: Print the result
Step6: Stop the program.

Program:
a=int(input("enter number:"))
string_number=str(a)
reversed_string=string_number[::-1]
if string_number == reversed_string:
print(f'The number {string_number} is a palindrome')

else:
print('It is not a palindrome')

Output: (Screenshots):

Result:
Python program to check whether the number is palindrome or not is written and executed successfully.

You might also like