[go: up one dir, main page]

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

Ex 2401

The document contains a series of Python programming exercises, including converting decimal to binary, binary to decimal, printing patterns, and identifying odd numbers. It also includes finding the largest of three integers and determining if a number is even or odd. Each exercise is accompanied by sample code implementations.

Uploaded by

yrabiu275
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)
9 views3 pages

Ex 2401

The document contains a series of Python programming exercises, including converting decimal to binary, binary to decimal, printing patterns, and identifying odd numbers. It also includes finding the largest of three integers and determining if a number is even or odd. Each exercise is accompanied by sample code implementations.

Uploaded by

yrabiu275
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

PRACTICE

1. Write a program that converts decimal to binary.


NEW_BASE = 2
num = int(input("Enter a non-negative integer: "))
result = ""
q = num
r = q % NEW_BASE
result = str(r) + result
q = q // NEW_BASE
while q > 0:
r = q % NEW_BASE
result = str(r) + result
q = q // NEW_BASE
print(num, "in decimal is", result, "in binary.")

2. Write a program that converts binary to decimal.


b = 2
n = int(input("Enter a binary number: "))
q = str(n)
rs = 0
l= len(q)-1
for x in q:
r = int(x)*b**l
l = l - 1
rs = rs + r
print(rs)

3. Write a program to implement the following

(i) (ii)
# 1
## 23
### 456
#### 7 8 9 10
##### 11 12 13 14 15
Solution
(i) (ii)

for i in range(5): number = 1


for j in range(i+1): for i in range(5):
print("# ", end="") for j in range(i+1):
print("\r") print(number, end=" ")
number += 1
print()

Note: Including end="" as the final parameter prevents it from moving down to the next line after
displaying the value.

4. Write two programs (one using for loop and the other using while loop)
that prints odd numbers between 1 - 10.
The output for both the programs should look like this 1 3 5 7 9 finished

for x in range(1, 11, 2):


print(x, end=' ')
print()
print('finished')

i = 0
while i < 11:
if i % 2 != 0:
print(i, end=' ')
i = i + 1
print()
print('finished')

5. Write Python program that accepts three (3) integers, and then find the
largest number among the three input numbers

num1 = int(input("Enter first number: "))


num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)
6. Write Python program that prints the multiplication table below

7. Write a program that reads an integer from the user. Then your
program should display a message indicating whether the integer is
even or odd.

num = int(input("Enter an integer: "))


if num % 2 == 1:
print(num, "is odd.")
else:
print(num, "is even.")

You might also like