Monday 19 September 2022

Example programs in Python: Fibonacci Series

 Writing Fibonacci Series in Python using if else statement and for loop

Example:1

x=int(input("enter the number of terms "))
y=[]

if x==1:
    z=0
    y.append(z)
    print(y)
elif x==2:
    z=0
    q=1
    y.append(z)
    y.append(q)
    print(y)
else:
    z=0
    q=1
    y.append(z)
    y.append(q)
    for i in range(3,x+1):        
        l=y[i-3]
        m=y[i-2]
        k = l + m
        #k=y[i-1]
        y.append(k)
print(y)

Output

enter the number of terms 10
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

----------------------------------------------------------------------------------


Example 2: Fibonacci Series

x=int(input("Enter the number of terms: "))
a=0
b=1
print(a,b,end=" ")
for i in range(2,x):
    c = a+b
    print(c,end=" ")
    a=b
    b=c
   
Output

0 1 1 2 3 5 8 13 21 34

Sunday 18 September 2022

Python Example Program: Star Pyramid

 Writing Star Pyramid Program using Function in Python

Example:

x="*"
def function(y):
    i=1
    for i in range(y+1):
        z=i*x
        print(f"{z} ")
function(5)  

Output

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

Python Example Program : Palindrome

 Checking whether given word or sequence of number is palindrome

Example 1

word = input("Enter a word to check its palindrome: ")
word=word.lower()
y=len(word)
z=y-1
i=0
while (i<y):
   
    if(word[i]!=word[z]):
        print(f"{word} is not a palindrome")
        break
    i=i+1
    z=z-1    
else:
   
    print(f"{word} is a palindrome")

Output

Enter a word to check its palindrome: Madam
madam is a palindrome

----------------------------------------------------------------------------------

Example 2 : Finding Palindrome in reversing the string

a="mom"
print(f"a= {a}")
b=a[::-1] #it will reverse
print(f"After reversing, b= {b}")
if a==b:
    print(f"{a} is a palindrome")
else:
    print(f"{a} is not a palindrome")

Output

a= mom After reversing, b= mom mom is a palindrome

Python Example Program : Finding Factors

Finding all the possible factors of an integer in Python

Example

x=int(input("Enter a number to find all the possible factors: "))
y=[]
for i in range(1,x+1):
    if x%i == 0:
        y.append(i)
print(y)

Output

Enter a number to find all the possible factors: 400
[1, 2, 4, 5, 8, 10, 16, 20, 25, 40, 50, 80, 100, 200, 400]

Saturday 17 September 2022

Python Example programs : Prime Number

 Finding a number is prime or not in python

Example1

x = int(input("enter a number: "))
i=2
while (i<x):
    if (x%i == 0):
        print(f"{x} is not a prime number")
        break
     
    i=i+1
else:
        print(f"{x} is  a prime number")

Output

enter a number: 7727
7727 is a prime number

-------------------------------------------------------------------------------------


Example 2

x = int(input("enter a  number: "))
i=2
if x <= 0:
       print(f"{x} is not a prime number")
else:
    while (i<x):
        if (x%i == 0):
          print(f"{x} is not a prime number")
          break
     
        i=i+1

    else:
        print(f"{x} is  a prime number")

Output
enter a positive integer number: 0
0 is not a prime number

Python Example Problems : Odd and Even Numbers

 Finding even and odd numbers using while loop in Python

Example 1

i=1
x=[]
y=[]
while (i <= 30):
    if i%2 == 0:
        x.append(i)
       
    else :
        y.append(i)
       
    i = i + 1  
print(f"even numbers are {x} ")    
print(f" odd number are {y} ")

Output

even numbers are [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30] odd number are [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]

Example 2

Finding odd and even numbers in a specified list

n = int(input("Enter a number till which we need to list out odd and even numbers: "))
i=1
x=[]
y=[]

while (i <= n):
    if i%2 == 0:
        x.append(i)
       
    else :
        y.append(i)
       
    i = i + 1  
print(f"even numbers are {x} ")    
print(f" odd number are {y} ")

output Enter a number till which we need to list out odd and even numbers: 10
even numbers are [2, 4, 6, 8, 10] odd number are [1, 3, 5, 7, 9]