You are on page 1of 9

 

                                 

WORKSHEET – 1.2 
Name: Abhishek
Section/Group: 719 /“A”
UID: 20BCS7643
Subject: Programming in Python Lab
Date of Submission:04.03.2022
Branch: BE CSE (4 Semester)
th

Aim:
Write a python code that will implement Conditional Statements and Loop
Control Structure.

Task to be done:
1. Write a program to check whether a given number is a palindrome.
2. Write a program to check whether entered number is Armstrong or not.
3. Write a program to take three numbers from the user and print the
greatest number.

Apparatus / Simulator Used:


Python IDE 

CHANDIGARH UNIVERSITY                                    PROGRAMMING IN PYTHON


LAB

                                

Algorithm / Flowchart:
1. To check that whether the given number is palindrome or not:
 Start the program.
 Enter the number from the user.
 Store the number in a temporary variable.
 With the help of while loop get each digit of the number and store
the reversed number in another variable.
 Check if the reversed number is equal to the temporary variable or
not.
 Print all the values with a suitable message.
 End the program.

2. To check that whether the given number is Armstrong or not:


 Start the program.
 Enter the number from the user.
 Convert each digit of the given number into string and calculate the
length of the number of digits.
 Calculate the sum of the cube of each digit.
 Check if the given number is equal to the sum or not.
 Print all the values with a suitable message.
 End the program. 

3. To enter the three numbers from the user and print the greatest
number:
 Start the program.
 Enter the three numbers from the user.
 Check the greatest number using conditional statements.
 Print all the values with a suitable message.
 End the program.

CHANDIGARH UNIVERSITY                                    PROGRAMMING IN PYTHON


LAB

                              

Code:
1. To check that whether the given number is palindrome or
not:
#Python program to check that whether the given number is palindrome
or not
#Take the number from the user 
num=int(input("Enter number:"))
#Store the number in temporary variable
temp=num
rev=0
#Extract the digit of the number and store the reverse of the number 
while(num>0):
    digit=num%10
    rev=rev*10+digit
    num=num//10
#Check whether the reverse of the number is equal or not
if(temp==rev):
#Print the message
    print(temp,"is a palindrome number")
else:
    print(temp,"is not a palindrome number")

CHANDIGARH UNIVERSITY                                    PROGRAMMING IN PYTHON


LAB

                              

2. To check that whether the given number is Armstrong or


not:
#Python program to check that whether the given number is armstrong or
not
#Take the input from the user
num = int(input("Enter a number:"))
#Convert the number into string and count the length of the digits
length = len(str(num))
#Calculate the sum of each digit
sum = 0
temp = num
while temp > 0:
    digit = temp % 10
    sum += digit ** length
    temp //= 10
#Check whether the given number is equal to its sum of the cube of the
digits or not
if(num == sum):
#Print the result 
    print(num,"is an Armstrong Number")
else:
    print(num,"is not an Armstrong Number")

CHANDIGARH UNIVERSITY                                    PROGRAMMING IN PYTHON


LAB

                              

3. To enter the three numbers from the user and print the
greatest number:
#Python program to enter the three numbers from the user and print the
greatest number:
#Take three numbers from the user
n1 = int(input("Enter the first number:"))
n2 = int(input("Enter the second number:"))
n3 = int(input("Enter the third number:"))
#Check the greatest number using conditional statements
if(n1 >= n2) and (n1 >= n3):
    greatest = n1
elif(n2 >= n1) and (n2 >= n3):
    greatest = n2
else:
    greatest = n3
#print the result
print("The greatest number among three is",greatest)

CHANDIGARH UNIVERSITY                                    PROGRAMMING IN PYTHON


LAB

                              

Screenshots:
1. To check that whether the given number is palindrome or
not:

#Python program to check that whether the given number is palindrome or not
#Take the number from the user 
num=int(input("Enter number:"))
#Store the number in temporary variable
temp=num
rev=0
#Extract the digit of the number and store the reverse of the number 
while(num>0):
    digit=num%10
    rev=rev*10+digit
    num=num//10
#Check whether the reverse of the number is equal or not
if(temp==rev):
#Print the message
    print(temp,"is a palindrome number")
else:
    print(temp,"is not a palindrome number")

CHANDIGARH UNIVERSITY                                    PROGRAMMING IN PYTHON


LAB

                              

2. To check that whether the given number is Armstrong or


not:
#Python program to check that whether the given number is armstrong or not
#Take the input from the user
num = int(input("Enter a number:"))
#Convert the number into string and count the length of the digits
length = len(str(num))
#Calculate the sum of each digit
sum = 0
temp = num
while temp > 0:
    digit = temp % 10
    sum += digit ** length
    temp //= 10
#Check whether the given number is equal to its sum of the cube of the digits or
not
if(num == sum):
#Print the result 
    print(num,"is an Armstrong Number")
else:
    print(num,"is not an Armstrong Number")
CHANDIGARH UNIVERSITY                                    PROGRAMMING IN PYTHON
LAB

                              

3. To enter the three numbers from the user and print the
greatest number:

#Python program to enter the three numbers from the user and print the greatest
number:
#Take three numbers from the user
n1 = int(input("Enter the first number:"))
n2 = int(input("Enter the second number:"))
n3 = int(input("Enter the third number:"))
#Check the greatest number using conditional statements
if(n1 >= n2) and (n1 >= n3):
    greatest = n1
elif(n2 >= n1) and (n2 >= n3):
    greatest = n2
else:
    greatest = n3
#print the result
print("The greatest number among three is",greatest)
 

CHANDIGARH UNIVERSITY                                    PROGRAMMING IN PYTHON


LAB
                              

Result / Output:
1. To check that whether the given number is palindrome or
not:

2. To check that whether the given number is Armstrong or


not:

3. To enter the three numbers from the user and print the
greatest number:

CHANDIGARH UNIVERSITY                                    PROGRAMMING IN PYTHON


LAB
                              

Learning Outcomes:
1. Learn how to implement the conditional statements and loop control
structures in python.
2. Learn about loop while loop.
3. Learn about conditional statements.
4. Learn about type conversion.
5. Learn how to write code in python , about indentation.

CHANDIGARH UNIVERSITY                                    PROGRAMMING IN PYTHON


LAB

You might also like