0% found this document useful (0 votes)
17 views

Write a program to reverse an integer in Python

Uploaded by

anilperfect
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Write a program to reverse an integer in Python

Uploaded by

anilperfect
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1. Write a program to reverse an integer in Python.

n = int(input("Please enter a number: "))


print("Before reverse:", n)
reverse = 0
while n != 0:
reverse = reverse * 10 + n % 10
n = n // 10
print("After reverse:", reverse)

number = 7536
print("Given number", number)
while number > 0:
# get the last digit
digit = number % 10
# remove the last digit and repeat the loop
number = number // 10
print(digit, end=" ")
2. Write a program in Python to check given number is prime
or not.
temp = 0
n = int(input("Please enter a number: "))
for i in range(2, n//2):
if n % i == 0:
temp = 1
break
if temp == 1:
print("The given number is not a prime number")
else:
print("The given number is a prime number"

3. Write a program in Python to find greatest among three


integers.
n1 = int(input("Please enter the first number (n1): "))
n2 = int(input("Please enter the second number (n2): "))
n3 = int(input("Please enter the third number (n3): "))
if n1 >= n2 and n1 >= n3:
print("n1 is the greatest")
elif n2 >= n1 and n2 >= n3:
print("n2 is the greatest")
else:
print("n3 is the greatest")

4. Write a program in Python to check if a number is binary?


def is_binary(num):
while num > 0:
digit = num % 10
if digit not in [0, 1]:
return False
num = num // 10
return True
num = int(input("Please enter a number: "))
if is_binary(num):
print("The number is binary.")
else:
print("The number is not binary.")

5. Python program to concatenate two strings without using


join() method.
#taking input from the user
str1 = input('Enter first string: ')
str2 = input('Enter second string: ')
#using string concatenation method ‘+’
str3 = str1 + str2
print("String after concatenation :",str3)

6. Python program to remove blank space from string.


#taking input from the user
string = input("Enter a String : ")
result=''
#iterating the string
for i in string:
#if the character is not a space
if i!=' ':
result += i
print("String after removing the spaces :",result)

7. Python program to check given character is vowel or


consonant.
ch=input(“Enter a character to check vowel or consonant :”)
if(ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u'
or ch == 'A' or ch == 'E' or ch == 'I' or ch == 'O' or ch ==
'U'):
#if ‘if’ condition satisfy
print("Given character", ch ,"is vowel")
else:
#if ‘if’ condition does not satisfy the condition
print("Given character",ch, "is consonant")

8. Turn every item of a list into its square


numbers = [1, 2, 3, 4, 5, 6, 7]
# result list
res = []
for i in numbers:
# calculate square and add to the result list
res.append(i * i)
print(res)

9. Concatenate two lists in the following order


list1 = ["Hello ", "take "]
list2 = ["Dear", "Sir"]
Expected output:
['Hello Dear', 'Hello Sir', 'take Dear', 'take Sir']
list1 = ["Hello ", "take "]
list2 = ["Dear", "Sir"]

res = [x + y for x in list1 for y in list2]


print(res)
10. Calculate sum of all numbers from 1 to a given number
# s: store sum of all numbers
s = 0
n = int(input("Enter number "))
# run loop n times
# stop: n+1 (because range never include stop number in result)
for i in range(1, n + 1, 1):
# add current number to sum variable
s += i
print("\n")
print("Sum is: ", s)

You might also like