0% found this document useful (0 votes)
3 views20 pages

Python Programs for Basic Algorithms

The document contains ten Python programs that demonstrate various programming tasks, including calculating the area and perimeter of a circle, generating Fibonacci series, computing the GCD of two numbers, finding prime numbers, summing squares of natural numbers, and manipulating arrays and strings. Each program includes user input, logic for the specific task, and output statements. The programs cover fundamental programming concepts and provide practical examples for beginners.

Uploaded by

sakthi priya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views20 pages

Python Programs for Basic Algorithms

The document contains ten Python programs that demonstrate various programming tasks, including calculating the area and perimeter of a circle, generating Fibonacci series, computing the GCD of two numbers, finding prime numbers, summing squares of natural numbers, and manipulating arrays and strings. Each program includes user input, logic for the specific task, and output statements. The programs cover fundamental programming concepts and provide practical examples for beginners.

Uploaded by

sakthi priya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Program 1

Develop a Python program to find the area and


perimeter of a circle.
# Python program to find the
# area and perimeter of circle in python
# Initialising the value of PI
PI = 3.14
# Getting input from user
R = float(input("Enter radius of the circle: "))
# Finding the area and perimeter of the circle
area = (PI*R*R)
perimeter = (2*PI*R)
# Printing the area and perimeter of the circle
print("The area of circle is", area)
print("The perimeter of circle is", perimeter)
Output:
Program 2
Develop a Python program to generate Fibonacci series.

# Program to display the Fibonacci sequence up to n-th term


nterms = int(input("How many terms? "))
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if nterms<= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elifnterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generatefibonacci sequence
else:
print("Fibonacci sequence:")
while count <nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
output:
Program 3
Develop a Python program to compute the GCD of two numbers.

num1 = 36
num2 = 60
gcd = 1

for i in range(1, min(num1, num2)):


if num1 % i == 0 and num2 % i == 0:
gcd = i
print("GCD of", num1, "and", num2, "is", gcd)
Output:
Program 4
Develop a Python program to generate first n prime numbers

numr=int(input("Enter range:"))

print("Prime numbers:",end=' ')

for n in range(1,numr):

for i in range(2,n):

if(n%i==0):

break

else:

print(n,end=' ')
Output:
program5
Develop a Python program to find the sum of squares of n
natural numbers
defsumOfSquares(n):
if n <0:
return
sum=0
foriinrange(n+1):
sum+=i*i
returnsum

n =int(input('Enter n : '))
sum=sumOfSquares(n)
print(f'Sum : {sum}')
Output:
Program 6:
Develop a Python program of find the Sum of the elements
in an array
1. #Initialize array
2. arr = [1, 2, 3, 4, 5];
3. sum = 0;
4.
5. #Loop through the array to calculate sum of elements
6. for i in range(0, len(arr)):
7. sum = sum + arr[i];
8.
9. print("Sum of all the elements of an array: " + str(sum));
Output:
Program7:
Develop a Python program to find the largest element in
the array
# largest function
def largest(arr,n):
#maximum element
max = arr[0]
# traverse the whole loop
for i in range(1, n):
if arr[i] > max:
max = arr[i]
return max
# Driver Code
arr = [23,1,32,67,2,34,12]
n = len(arr)
Ans = largest(arr,n)
print ("Largest element given in array is",Ans)
Output:
Program 8:
Develop a Python program to check if the given string is a
palindrome or not
# function which return reverse of a string

defisPalindrome(s):
returns ==s[::-1]

# Driver code
s ="malayalam"
ans =isPalindrome(s)

ifans:
print("Yes")
else:
print("No")
Output:
Program9:

Develop a Python program to store strings in a list and


print them

# Python code to convert string to list

defConvert(string):
li =list([Link](" "))
returnli
# Driver code
str1 ="Geeks for Geeks"
print(Convert(str1))
Output:
Program10:

Develop a Python program to find the length of a list,


[Link] and then clearit.

#Python program to reverse an array


deflist_reverse(arr,size):

#if only one element present, then return the


array
if(size==1):
returnarr

#if only two elements present, then swap both the


numbers.
elif(size==2):
arr[0],arr[1],=arr[1],arr[0]
returnarr

#if more than two elements presents, then swap


first and last numbers.
else:
i=0
while(i<size//2):

#swap present and preceding numbers at time and


jump to second element after swap
arr[i],arr[size-i-1]=arr[size-i-1],arr[i]

#skip if present and preceding numbers indexes


are same
if((i!=i+1andsize-i-1!=size-i-2) and(i!
=size-i-2andsize-i-1!=i+1)):
arr[i+1],arr[size-i-2]=arr[size-i-
2],arr[i+1]
i+=2
returnarr

arr=[1,2,3,4,5]
size=5
print('Original list: ',arr)
print("Reversed list: ",list_reverse(arr,size))

output:

You might also like