0% found this document useful (0 votes)
171 views10 pages

# Initialising The Value of PI: 1. Write A Python Program To Find The Area and Perimeter of A Circle

The document contains 10 code snippets that demonstrate various Python programming concepts and tasks. The snippets include programs to: 1. Calculate the area and perimeter of a circle given the radius. 2. Generate the Fibonacci series up to a given number of terms. 3. Find the greatest common divisor (GCD) of two numbers. 4. Generate the first n prime numbers. 5. Calculate the sum of squares of the first n natural numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
171 views10 pages

# Initialising The Value of PI: 1. Write A Python Program To Find The Area and Perimeter of A Circle

The document contains 10 code snippets that demonstrate various Python programming concepts and tasks. The snippets include programs to: 1. Calculate the area and perimeter of a circle given the radius. 2. Generate the Fibonacci series up to a given number of terms. 3. Find the greatest common divisor (GCD) of two numbers. 4. Generate the first n prime numbers. 5. Calculate the sum of squares of the first n natural numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Write a Python program to find the area and perimeter of a circle

# 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)

RESULT:

RUN 1:

Enter radius of the circle: 1.2


The area of circle is 4.521599999999999
The perimeter of circle is 7.536

RUN 2:
Enter radius of the circle: 35.63
The area of circle is 3986.2202660000007
The perimeter of circle is 223.7564
2. Write a Python program to generate Fibonacci series.

n = int(input("Enter the value of 'n': "))


a=0
b=1
sum = 0
count = 1
print("Fibonacci Series: ", end = " ")
while(count <= n):
print(sum, end = " ")
count += 1
a=b
b = sum
sum = a + b

RESULT :

Input:

Enter the value of 'n': 5

Output:

Fibonacci Series: 0 1 1 2 3
3. Write a Python program to compute the GCD of two numbers.

def gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b)
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
GCD=gcd(a,b)
print("GCD is: ")
print(GCD)

RESULT :

Enter the first number:5


Enter the second number:15
The GCD of the two numbers is 5
4. Write a Python program to generate first n prime numbers.

i=1
x = int(input("Enter the number:"))
for k in range(1, x+1):
c=0
for j in range(1, i+1):
a=i%j
if a == 0:
c=c+1

if c == 2:
print(i)
else:
k=k-1

i=i+1

RESULT :
5. Write a Python program to find the sum of squares of n natural numbers.

N = int(input("Enter value of N: "))

# calculating sum of square


sumVal = 0
for i in range(1, N+1):
sumVal += (i*i)

print("Sum of squares = ", sumVal)

RESULT :

RUN 1:
Enter value of N: 10
Sum of squares = 385

RUN 2:
Enter value of N: 12
Sum of squares = 650
6.Write a Python program to find the sum of the elements in an array.

import array as ar

def SumofArray(arr):
sum=0
n = len(arr)
for i in range(n):
sum = sum + arr[i]
return sum

#input values to list


a = ar.array('i',[10, 21, 12, 13])

# display sum
print ('Sum of the array is ', SumofArray(a) )

RESULT :

Sum of the array is 56


7. Write a Python program to find the largest element in the array.

import array as ar

def MaxofArray(arr):
max=a[0]
n = len(arr)
for i in range(n):
if(max<a[i]):
max=a[i]
return max

#input values to list


a = ar.array('i',[10, 21, 12, 13])

# display sum
print ('Max of the array is ', MaxofArray(a) )

RESULT :

Max of the array is 21


8.Write a Python program to check if the given string is a palindrome or not.

def is Palindrome(string):
if string==string[::-1]:
return "THE GIVEN STRING IS PARLINDROME"
else:
return " THE GIVEN STRING IS NOT PARLINDROME "

s1="AMMA"
s2="CAPTAIN"
print(s1,":",isPalindrome(s1))
print(s2,":",isPalindrome(s2))

OUTPUT :

AMMA : THE GIVEN STRING IS PARLINDROME


CAPTAIN : THE GIVEN STRING IS NOT PARLINDROME
9.Write a Python program to store strings in a list and print them.

li = [ 'have', 'a', 'nice', 'day']

# assign string
str = 'nice'

# check if string is present in the list


if str in li:
print(f'{string} is present in the list')
else:
print(f'{string} is not present in the list')

OUTPUT
nice is present in the list
10.Write a Python program to find the length of a list, reverse it, copy it

i = [10, 20, 30]


n = len(li)
print("The length of list is: ", n)

The length of list is: 3

You might also like