Python Loops Programs
Python Loops Programs
output
output
n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
output
Enter number:161
The number is a palindrome!
Enter number:162
The number isn't a palindrome!
output
Enter number: 2 Enter number: 2
Enter number: 10 Enter number: 10
2 power of 10 is 1024 2 power of 10 is 1024
rev_str = reversed(my_str)
if list(my_str) == list(rev_str):
print("It is palindrome")
else:
print("It is not palindrome")
Output
sum = 0
num=int(input("Enter any Number : "))
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")
output
output
output
Output
Enter a number:100
The number is a Even number
Enter a number:201
The number is Odd number
output
Enter a value : 5
120
12. Write a python program to check given number is strong number or not.
num=int(input("Enter a number:"))
temp=num
sum1=0
while(num):
i,f=1,1
r=num%10
while(i<=r):
f=f*i
i+=1
sum1+=f
num//=10
if(sum1==temp):
print("The number is a strong number")
else:
print("The number is not a strong number")
output
Enter a number:145
The number is a strong number
Enter a number:200
The number is not a strong number
output
output
Enter a Vaule : 10
1
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
output
Enter a string : vignan
ASCII value of v is 118
ASCII value of i is 105
ASCII value of g is 103
ASCII value of n is 110
ASCII value of a is 97
ASCII value of n is 110
ASCII value of is 32
16. Multiplication of odd numbers and sum of even numbers within a range
n=int(input("enter n value"))
even=0
odd=1
for i in range (1,n+1):
if(i%2==0):
even=even+i
else:
odd=odd*i
print("sum of even numbers={0}\nMultiplication of odd numbers={1}".format(even,odd))
output
enter n value10
sum of even
numbers=30
Multiplication of odd
numbers=945
n=int(input("enter n value"))
for i in range (1,n+1):
c=0
for j in range(1,i+1):
if(i%j==0):
c=c+1
if(c==2):
print(i)
output
enter n value50
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
12
123
12
123
n=int(input("enter number"))
for i in range(1,11):
print("{0} * {1} = {2}".format(n,i,n*i))
output
enter number 10
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100
20. Write a python program to subtract two numbers without using subtraction operator.(
sum = a + ~b + 1)