KEMBAR78
Aman Python 2 | PDF | Computers
0% found this document useful (0 votes)
52 views7 pages

Aman Python 2

The document contains source code for various Python programs demonstrating the use of control flow statements like if-else, while, for loops. It includes programs to check palindrome, find greatest among 3 numbers, print patterns and find factorial of a number. It also contains solutions to Codechef problems.

Uploaded by

Aman Sharma
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)
52 views7 pages

Aman Python 2

The document contains source code for various Python programs demonstrating the use of control flow statements like if-else, while, for loops. It includes programs to check palindrome, find greatest among 3 numbers, print patterns and find factorial of a number. It also contains solutions to Codechef problems.

Uploaded by

Aman Sharma
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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET 1.1
Student Name: Aman Sharma UID:21BCS5592
Branch: CSE Section/Group: 810-B
Semester:4 Date of Performance:
Subject Name: Python Lab Subject Code: 21CSP-259

Aim: Program to demonstrate the use of if, if-else, while, for, break and
continue.

1. Write a program to check whether a given number is palindrome or not.

SOURCE CODE:
print("Aman Sharma\n21bcs5592")
number = int(input("Enter the number: "))
temp = number
reverse =0
while (number > 0):
dig = number % 10
reverse = reverse * 10 + dig
number = number // 10
print("The reverse number is: ", reverse)
if temp==reverse:
print("The number is a palindrome")
else:
print("The number is not a palindrome")

AMAN SHARMA 21BCS5592


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

3. Write a program to take input of three numbers and find greatest among them.

Source code:

print("Aman Sharma\n21bcs5592")
a=int(input('Enter first number: '))
b=int(input('Enter second number:'))
c=int(input('Enter third number:'))
if((a>=b) & (a>=c)):
print('Greatest Number is: ',a)
elif((b>=a) & (b>=c)):
print('Greatest Number is: ',b)
else:
print('Greatest Number is: ',c)

Output:

AMAN SHARMA 21BCS5592


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Write a program to print the pattern:

2 * * *****
24 ** ** ****
246 *** *** ***
2468 **** **** **
246810 ***** ***** *

Source code 1:

print("Aman Sharma\n21bcs5592")
n=int(input('Enter number of rows: '))
for i in range (1,n+1):
for j in range (1,i+1):
print(j*2,' ',end='')
print()

Output:

Source code 2:

print("Aman Sharma\n21bcs5592")

AMAN SHARMA 21BCS5592


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

n=int(input('Enter number of rows: '))


for i in range (0,n+1):
for j in range (1,i+1):
print('* ',end="")
print()

Output:

Source code 3:

print("Aman Sharma\n21bcs5592")
n = int(input("Enter the number of rows: "))
m = (2 * n) - 2
for i in range(0, n):
for j in range(0, m):
print(end=" ")
m=m-1
for j in range(0, i + 1):
print("*", end=' ')
print(" ")

Output:

AMAN SHARMA 21BCS5592


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Source code:

print("Aman Sharma\n21bcs5592")
rows = int(input("Enter the number of rows: "))
k = 2 * rows - 2
for i in range(rows, -1, -1):
for j in range(k, 0, -1):
print(end=" ")
k=k+1
for j in range(0, i + 1):
print("*", end=" ")
print("")

Output:

AMAN SHARMA 21BCS5592


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4.Write a program to find factorial of a given number.


Source code:
print("Aman Sharma\n21bcs5592")
a=int(input("Enter a number: "))
fact=1
for i in range (1,a+1):
fact=fact*i
print('Factorial of',a,'is',fact)

Output:

Codechef Problem:
i. Source code to write a program to obtain length (L) and breadth (B) of a rectangle and
check whether its area is greater or perimeter is greater or both are equal.

Source code:
l=int(input())
b=int(input())
area= l*b
perimeter= 2*(l+b)
if area>perimeter:
print("Area")
print(area)
elif area==perimeter:
print("Equal")
print(perimeter)
else:
print("Peri")
print(perimeter)

Output:

AMAN SHARMA 21BCS5592


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

ii. Pooja would like to withdraw X $US from an ATM. The cash machine will only accept the
transaction if X is a multiple of 5, and Pooja's account balance has enough cash to perform the
withdrawal transaction (including bank charges). For each successful withdrawal the bank
charges 0.50 $US.
Calculate Pooja's account balance after an attempted transaction.
Source code:
n,atm=map(float,input().split())
n=int(n)
if (n+0.5<=atm and n%5==0):
print(float(atm-n-0.5))
else:
print(float(atm)

Output:

AMAN SHARMA 21BCS5592

You might also like