0% found this document useful (0 votes)
18 views4 pages

1

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)
18 views4 pages

1

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/ 4

1.

Python program to print a multiplication table of a given number


Ans..
given_number = 5
for i in range(11):
print (given_number," x",i," =",5*i)

2.WAP to print the pattern:


0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
Ans..
rows = 5
for i in range(rows, 0, -1):
for j in range(0, i + 1):
print(j, end=' ')
print("\r")

3.WAP to print the pattern:


1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
Ans..
rows = 6
for i in range(1, rows):
for j in range(i, 0, -1):
print(j, end=' ')
print("")

4.WAP to print the pattern:


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Ans..
rows = 6
for i in range(1, rows):
num = 1
for j in range(rows, 0, -1):
if j > i:
print(" ", end=' ')
else:
print(num, end=' ')
num += 1
print("")
5.Python program to get the Fibonacci series between 0 to 50
Ans..
num = 50
first_value,second_value = 0, 1
for n in range(0, num):
if(n <= 1):
next = n
if nextnum:
break
print(next)

6.Python program to find the factorial of a given number.


Ans..
given_number= 5
factorial = 1
for i in range(1, given_number + 1):
factorial = factorial * i
print("The factorial of ", given_number, " is ", factorial)

7.Write a Python program to find the difference between two lists of the
same type.
Ans..
list1 = [10, 20, 30, 40, 50]
list2 = [10, 20, 30, 60, 70]
print("list1:", list1)
print("list2:", list2)
print("Difference elements:")
print(list(set(list1) - set(list2)))

8.Write a Python program to construct the following pattern, using a


nested for loop.

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

Ans..
n = 5
for i in range(n):
for j in range(i):
print('* ', end="")
print('')
for i in range(n, 0, -1):
for j in range(i):
print('* ', end="")
print('')

9. Write a Python program to convert temperatures to and from Celsius and


Fahrenheit.
[ Formula : c/5 = f-32/9 [ where c = temperature in celsius and f =
temperature in fahrenheit ]
Expected Output :
60°C is 140 in Fahrenheit
45°F is 7 in Celsius

Ans..
temp = input("Input the temperature you like to convert? (e.g., 45F, 102C
etc.) :
degree = int(temp[:-1])
i_convention = temp[-1]
if i_convention.upper() == "C":
result = int(round((9 * degree) / 5 + 32))
o_convention = "Fahrenheit" # Set the output convention as
Fahrenheit
elif i_convention.upper() == "F":
result = int(round((degree - 32) * 5 / 9))
o_convention = "Celsius" # Set the output convention as Celsius
else:
print("Input proper convention.")
quit()
print("The temperature in", o_convention, "is", result, "degrees.")

10.Check Armstrong number (for 3 digits)


Ans..
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

11.WAP to find GCD:


Ans.
def compute_hcf(x, y):
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
num1 = 54
num2 = 24
print("The H.C.F. is", compute_hcf(num1, num2))

12.WAP to print the pattern:


1 *
2 * 2 *
3 * 3 * 3 *
4 * 4 * 4 * 4 *

Ans..
for i in range(4):
for j in range(0,1+i):
print(i+1,"*",end=" ")
print()

13.WAP to print the pattern:


*
* *
* * *
* * * *
Ans..
for i in range(4):
for j in range(i+1):
print("*",end=" ")
print()

14.WAP to print the pattern:


1 2 3 4
1 2 3
1 2
1
Ans..
for i in range(4):
for j in range(1,5-i):
print(j,end=" ")
print()

15.WAP to find the sum of n numbers and count the numbers in a list:
Ans..
a=[]
n=int(input("How many numbers?: "))
for i in range(1,n+1):
k=int(input("Enter Number: "))
a.append(k)
c=int(input("Enter the number to be counted: "))
z=int(input("Enter the number to find: "))
print("The Sum is: %d"%sum(a))
print("The No of times is: %d"%a.count(c))

You might also like