Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
42 views

Python Loops Programs

The document contains 20 code snippets showing examples of various python programming concepts like finding sum of digits, reversing numbers, checking palindromes, finding factorials, checking prime numbers etc. Each code snippet is followed by sample input/output to demonstrate the program.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Python Loops Programs

The document contains 20 code snippets showing examples of various python programming concepts like finding sum of digits, reversing numbers, checking palindromes, finding factorials, checking prime numbers etc. Each code snippet is followed by sample input/output to demonstrate the program.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1. Write a python program to find out sum of digit of given number.

n=int(input("Enter a Number : "))


sum=0
r=0
while(n):
r=n%10
n//=10
sum+=r
print(sum)

output

Enter a Number : 12345


15

2. Write a python program to reverse any number.


n=int(input("Enter a Number : "))
rev=0
while(n):
r=n%10
rev=rev*10+r
n=n//10
print(rev)

output

Enter file name : 756


657

3. Write a python program to check given number is palindrome or not.

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!

4. Write a python program to find out power of number.

n1=int(input("Enter number: "))


n1=int(input("Enter number: ")) n2=int(input("Enter number: "))
n2=int(input("Enter number: ")) print(n1," power of ",n2," is ",n1**n2)
print(n1," power of ",n2," is ",pow(n1,n2))

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

5. Write a python program to check given string is palindrome or not.

my_str =input('Enter a String : ')


my_str = my_str.casefold()

rev_str = reversed(my_str)
if list(my_str) == list(rev_str):
print("It is palindrome")
else:
print("It is not palindrome")

Output

Enter a String : malayalam


It is palindrome

Enter a String : vignannirula


It is not palindrome
6. Write a python program to check given number is Armstrong or not.

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

Enter any Number : 407


407 is an Armstrong number

Enter any Number : 107


107 is not an Armstrong number

7. Write a python program to check given number is prime number or not.

num=int(input("Enter any Number : "))


count=0
for i in range(1,num+1):
if (num%i == 0):
count+=1
if count == 2:
print("The number is Prime number!")
else:
print("The number is not a Prime number!")

output

Enter any Number : 13


The number is Prime number!

Enter any Number : 20


The number is not a Prime number!
8. Write a python program to check given number is perfect number or not.
n = int(input("Enter any number: "))
sum1 = 0
for i in range(1, n):
if(n % i == 0):
sum1 = sum1 + i
if (sum1 == n):
print("The number is a Perfect number!")
else:
print("The number is not a Perfect number!")

output

Enter any number: 6


The number is a Perfect number!

Enter any number: 20


The number is not a Perfect number!

9. Write python program to check a number is odd or even.


num=int(input("Enter a number:"))
if(num%2==0):
print("The number is a Even number")
else:
print("The number is Odd number")

Output

Enter a number:100
The number is a Even number

Enter a number:201
The number is Odd number

10. Write a python program to solve quadratic equation.


import cmath
a = int(input('Enter a Value for a : '))
b = int(input('Enter a Value for b : '))
c = int(input('Enter a Value for c : '))
d = (b**2) - (4*a*c)
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))
output

Enter a Value for a : 1


Enter a Value for b : 5
Enter a Value for c : 6
The solution are (-3+0j) and (-2+0j)

11. Write a python program to get factorial of given number.

n=int(input('Enter a value : '))


fact=1
if n==0:
print(1)
else:
for i in range(1,n+1):
fact*=i
print(fact)

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

13. Write a python program to print Fibonacci series of given range.

n=int(input('Enter END/Range value : '))


n1=int(input('Enter Feb[1] value : '))
n2=int(input('Enter Feb[2] value : '))
print(n1)
print(n2)
while((n1+n2)<n):
i=(n1+n2)
n1=n2
n2=i
print(n2)

output

Enter END/Range value : 100


Enter Feb[1] value : 1
Enter Feb[2] value : 1
1
1
2
3
5
8
13
21
34
55
89

14. Write a python program for Floyd’s triangle.


n=int(input('Enter a Vaule : '))
count = 1
string = ""
for i in range(1,n+2):
for j in range(1,i):
string = string + " " + str(count)
count = count + 1
print(string)
string = ""

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

15. Write a python program to print ASCII value of all characters.

ch=input('Enter a string : ')


for i in ch:
print('ASCII value of ',i,' is ',ord(i))

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

17. Print 1 to n prime numbers

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

18. Print the following format

12

123

n=int(input("enter number of rows"))


i=1
while(i<=n):
j=1
while(j<=i):
print(j,end=" ")
j=j+1
print("\n")
i=i+1
output
enter number of rows
3

12

123

19. Multiplication table

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)

n1=int(input("Enter number: "))


n2=int(input("Enter number: "))
sub=n1 + (~n2) + 1
print(sub)

output Enter number: 20 Enter number: 30 -10

You might also like