To write a Python program to convert the number 16 into its binary representation using built-in Python functions.
- Assign the value
16to a variablea. - Use the built-in
bin()function to convert the number to binary. - Print the result.
x=16
y=bin(x)
print(y)
Thus,the Python program to convert the number 16 into its binary representation using built-in Python functions is created successfully.
To write a Python program that defines a function which accepts two values and returns their modulo using the % operator.
- Define a function called
resultthat takes two argumentsaandb. - Inside the function, compute the modulo using
a % b. - Print the result of the modulo operation.
- Get two integer inputs from the user.
- Call the
resultfunction with the user-provided values.
def result(a, b):
modulo_value = a % b
return modulo_value
a=int(input())
b=int(input())
print("modulo is", result(a, b))
Thus,the Python program that defines a function which accepts two values and returns their modulo using the % operator is created successfully.
To write a Python program that defines a lambda function which takes two arguments a and b, and returns their sum.
- Get two integer inputs from the user.
- Use a lambda function to define a function
fthat returnsa + b. - Call the function with the user inputs and print the result.
i=int(input())
j=int(input())
z=int(input())
f = lambda a, b,c: a+b+c
print(f(i, j,z))
Thus,the Python program that defines a lambda function which takes two arguments a and b, and returns their sum is created successfully.
This project demonstrates a simple Python program to generate Pascal’s Triangle, where the number of rows is provided by the user.
To write a Python program that generates Pascal's Triangle using numbers. The number of rows is accepted from the user.
- Start the program.
- Input the number of rows from the user.
- Loop from 0 to the number of rows.
- For each row:
- Print appropriate spaces to shape the triangle.
- Compute values using the formula:
[ C(n, k) = \frac{n!}{k!(n-k)!} ]
- Print all rows of Pascal’s Triangle.
- End the program.
rows = int(input())
coef = 1
for i in range(1, rows+1):
for space in range(1, rows-i+1):
print(" ",end="")
for j in range(0, i):
if j==0 or i==0:
coef = 1
else:
coef = coef * (i - j)//j
print(coef, end = " ")
print()
Thus,the Python program that generates Pascal's Triangle using numbers. The number of rows is accepted from the user is created successfully.
To write a Python program that checks whether a given number is a palindrome using loops.
- Get input from the user and assign it to a variable
num. - Assign the value of
numto a temporary variabletemp. - Initialize a variable
revto 0 (used to store the reversed number). - Use a
whileloop to reverse the digits:- While
temp > 0:rev = (10 * rev) + temp % 10temp = temp // 10
- While
- After the loop, compare
revwithnum:- If equal, print that the number is a palindrome.
- Else, print that it is not a palindrome.
num=int(input())
rev=0
temp=num
while temp>0:
rev=(10*rev)+temp%10
temp//=10
if rev==num:
print("The given number {} is a Palindrome".format(num))
else:
print("The given number {} is not a palindrome".format(num))
Thus,the Python program that checks whether a given number is a palindrome using loops is created successfully.




