0% found this document useful (0 votes)
34 views

Arvin Python Project

The document contains programs written in Python to solve various problems. In Program 1, programs are presented to calculate the area of a triangle, swap two variables, and convert Celsius to Fahrenheit. Program 2 contains programs to check if a number is even or odd, positive, negative or zero, and to check if a number is an Armstrong number. Program 3 contains programs to check if a number is a Fibonacci number, calculate the cube sum of first n natural numbers, and print all odd numbers in a range. Program 4 presents a program to print Pascal's triangle and a pattern of numbers. Program 5 contains a function to capitalize the first letter of each word in a string. Program 6 contains programs to reverse a list

Uploaded by

arvinjulaha17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Arvin Python Project

The document contains programs written in Python to solve various problems. In Program 1, programs are presented to calculate the area of a triangle, swap two variables, and convert Celsius to Fahrenheit. Program 2 contains programs to check if a number is even or odd, positive, negative or zero, and to check if a number is an Armstrong number. Program 3 contains programs to check if a number is a Fibonacci number, calculate the cube sum of first n natural numbers, and print all odd numbers in a range. Program 4 presents a program to print Pascal's triangle and a pattern of numbers. Program 5 contains a function to capitalize the first letter of each word in a string. Program 6 contains programs to reverse a list

Uploaded by

arvinjulaha17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Problem Solving using Python Programming (23CS001)

Program 1

A) Write a Python Program to Calculate the Area of a Triangle

h=float(input("Enter the Height Of The Triangle: "))


b=float(input("Enter the Base Of The Triangle: "))
area=(1/2)*b*h
print(“Area Of Triangle: ”,area)

Output:
Enter the Height Of The Triangle: 10
Enter the Base Of The Triangle: 5
Area Of Triangle: 25.0 cm sq

B) Write a Python Program to Swap Two Variables


a=float(input("Enter Value of a : "))
b=float(input("Enter Value of b : "))
c=a
a=b
b=c
print("a=",a ,"and b=",b)

Output:
Enter Value of a : 10.5
Enter Value of b : 9.5
a= 9.5 and b= 10.5

C) Write a Python Program to Convert Celsius to Fahrenheit


celsius=float(input("Enter The Celsius Temperature : "))
fahrenheit= (celsius*(9/5) +32)
print(f"{celsius} Degree Celsius Temperature in Fahreheit is","%.1f" %
fahrenheit , "Fahrenheit")

Output:
Enter The Celsius Temperature : 37
37.0 Degree Celsius Temperature in Fahreheit is 98.6 Fahrenheit

Arvin (2310990386) BE-CSE(G5-A) Page 1


Problem Solving using Python Programming (23CS001)

Program 2
A) Write a Python Program to Check if a Number is Odd or Even

n=int(input("Enter a Number You Want To Check If Its ODD OR EVEN : "))


if(n%2==0):
print("The Entered Number is EVEN")
else:
print("The Entered Number is ODD")

Output:

Enter a Number You Want To Check If Its ODD OR EVEN : 9104


The Entered Number is EVEN

B) Write a Python Program to Check if a Number is Positive, Negative or 0

n=float(input("Enter a Number You Want To Check If It is Positive,


Negative Or ZERO : "))
if(n>0):
print("The Entered Number is POSITIVE")
elif(n<0):
print("The Entered Number is NEGATIVE")
else:
print("The Entered Number is ZERO")

Output:

Enter a Number You Want To Check If It is Positive, Negative Or ZERO :


9
The Entered Number is POSITIVE

Arvin(2310990386) BE-CSE(G5-A) Page 2


Problem Solving using Python Programming (23CS001)

C) Write a Python Program to Check Armstrong Number

n=int(input("Enter a Number You Want To Check If It is Armstrong


Number OR NOT: "))

sum=0
x=len(str(n))
copy_n=n

while(n>0):
digit=n%10
sum=sum+(digit**x)
n=n//10

if(sum==copy_n):
print("The Entered Number is an Armstrong Number")

else:
print("The Entered Number is a NOT an Armstrong Number")

Output:

Enter a Number You Want To Check If It is Armstrong Number OR NOT: 153

The Entered Number is an Armstrong Number

Arvin(2310990386) BE-CSE(G5-A) Page 3


Problem Solving using Python Programming (23CS001)

Program 3

A) Write a Python program to check if a given number is Fibonacci number?

def is_fibonacci(n):
if n <= 0:
return False
else:
return is_perfect_square(5*n*n + 4)
or is_perfect_square(5*n*n - 4)

def is_perfect_square(x):
sqrt_x = int(x ** 0.5)
return sqrt_x * sqrt_x == x

n = int(input("Enter The Number You Want To Check If It is Fibonacci


Number OR Not: "))

if is_fibonacci(n):
print("The Entered Number is a Fibonacci Number")
else:
print("The Entered Number is NOT a Fibonacci Number")

Output:

Enter The Number You Want To Check If It is Fibonacci Number OR Not:


21
The Entered Number is a Fibonacci Number

B) Write a Python program to print cube sum of first n natural numbers

n=int(input("Enter the Value of n , For Which You Want the Cube of


First n Natural Numbers : "))

sum=0

if (n<=0):
print("Enter a Positive Natural Number Please !")

Arvin(2310990386) BE-CSE(G5-A) Page 4


Problem Solving using Python Programming (23CS001)

n=int(input("Enter the Value of n , For Which You Want the Cube of


First n Natural Numbers : "))

else:
for i in range (1,n+1):
sum=sum+(i*i*i)

print(“Sum = ”,sum)

Output:

Enter the Value of n , For Which You Want the Cube of First n Natural
Numbers : 9

Sum = 2025

C) Write a Python program to print all odd numbers in a range

n=int(input("Enter The Value of n For Which You Want To Print All The
Odd Numbers in That Range n : "))

Odd=0

for i in range(1,n+1):
if (i%2==0):
continue
else:
Odd=i
print(Odd," ",end="")

Output:

Enter The Value of n For Which You Want To Print All The Odd Numbers
in That Range n : 9

1 3 5 7 9

Arvin(2310990386) BE-CSE(G5-A) Page 5


Problem Solving using Python Programming (23CS001)

Program 4

A) Write a Python Program to Print Pascal Triangle

try:
n = int(input("Enter the Number Of Rows For Pascal's Triangle: "))
if n <= 0:
raise ValueError("Please enter a positive integer.")

triangle = [[1]]

for i in range(1, n):


prev_row = triangle[-1]
triangle.append([1] + [prev_row[j - 1] + prev_row[j] for j in
range(1, i)] + [1])

for row in triangle:


print(" " * (n - len(row)), *row)

except ValueError as e:
print(f"Error: {e}")

Output:

Enter the Number Of Rows For Pascal's Triangle: 5

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Arvin(2310990386) BE-CSE(G5-A) Page 6


Problem Solving using Python Programming (23CS001)

B) WAP to Draw the following Pattern for n number:


11111
2222
333
44
5

n=int(input("Enter the Value of n for the Pattern : "))

for i in range(1,n+1):
for j in range (n,0,-1):
print(i,end="")
print()
n=n-1

Output:

Enter the Value of n for the Pattern : 5

11111
2222
333
44
5

Arvin(2310990386) BE-CSE(G5-A) Page 7


Problem Solving using Python Programming (23CS001)

Program 5

Write a program with a function that accepts a string from keyboard and create a new
string after converting character of each word capitalized. For instance, if the sentence is
“stop and smell the roses” the output should be “Stop And Smell The Roses”

def capitalize_words(str):
words = str.split()
capitalized_words = [word.capitalize() for word in words]
return ' '.join(capitalized_words)

str = input("Enter a String: ")


capitalized_str = capitalize_words(str)
print("Capitalized String:", capitalized_str)

Output:
Enter a String: arvin
Capitalized String: Arvin

Arvin(2310990386) BE-CSE(G5-A) Page 8


Problem Solving using Python Programming (23CS001)

Program 6
A) Write a program that accepts a list from user. Your program should reverse the content
of list and display it. Do not use reverse () method

list=[]
n=int(input("No. Of Elements in List: "))
for i in range(0,n):
ele=int(input(f"Enter the Element {i+1}: "))
list.append(ele)
a=list[::-1]
print("Reversed List:",a)

Output:

No. Of Elements in List: 5


Enter the Element 1: 10
Enter the Element 2: 20
Enter the Element 3: 30
Enter the Element 4: 40
Enter the Element 5: 50
Reversed List: [50, 40, 30, 20, 10]

B) Find and display the largest number of a list without using built-in function max ().
Your program should ask the user to input values in list from keyboard

list=[]
n=int(input("No. Of Elements in List: "))
for i in range(0,n):
ele=int(input(f"Enter the Element {i+1}: "))
list.append(ele)
list.sort()
print("The Largest Number in the List is",list[-1])

Arvin(2310990386) BE-CSE(G5-A) Page 9


Problem Solving using Python Programming (23CS001)

Output:
No. Of Elements in List: 5
Enter the Element 1: 0
Enter the Element 2: 1000
Enter the Element 3: 1
Enter the Element 4: 2
Enter the Element 5: 9
The Largest Number in the List is 1000

Arvin(2310990386) BE-CSE(G5-A) Page 10


Problem Solving using Python Programming (23CS001)

Program 7
Find the sum of each row of matrix of size m*n

m=int(input("Enter The No. Of Rows: "))


n=int(input("Enter The No. Of Columns: "))
matrix=[]
for i in range(m):
row=[]
for j in range(n):
element=int(input(f"Enter the Element {j+1} Of Row {i+1}: "))
row.append(element)
matrix.append(row)
for i , row in enumerate(matrix):
row_sum=sum(row)
print(f"Sum Of Elements of Row {i+1} = {row_sum}")

Output:
Enter The No. Of Rows: 2
Enter The No. Of Columns: 3
Enter the Element 1 Of Row 1: 1
Enter the Element 2 Of Row 1: 2
Enter the Element 3 Of Row 1: 3
Enter the Element 1 Of Row 2: 4
Enter the Element 2 Of Row 2: 5
Enter the Element 3 Of Row 2: 6
Sum Of Elements of Row 1 = 6
Sum Of Elements of Row 2 = 15

Arvin(2310990386) BE-CSE(G5-A) Page 11


Problem Solving using Python Programming (23CS001)

Program 8
A) Write a program that reads a string from keyboard and display:
* The number of uppercase letters in the string.
* The number of lowercase letters in the string.
* The number of digits in the string.
* The number of whitespace characters in the string

string=str(input("Enter a String: "))


UC=0
LC=0
D=0
WS=0
Uppercase=0
for i in string:
if i.isupper():
UC=UC+1
elif i.islower():
LC=LC+1
elif i.isdigit():
D=D+1
elif i.isspace():
WS=WS+1

print("No. Of UpperCase Letters in String =",UC)


print("No. Of LowerCase Letters in String =",LC)
print("No. Of Digits in String =",D)
print("No. Of Whitespace Characters in String =",WS)

Output:

Enter a String: Arvin


No. Of UpperCase Letters in String = 2
No. Of LowerCase Letters in String = 9
No. Of Digits in String = 1

Arvin(2310990386) BE-CSE(G5-A) Page 12


Problem Solving using Python Programming (23CS001)

No. Of Whitespace Characters in String = 1

B) Python Program to Find Common Characters in Two Strings

s1 = input("Enter The First String: ")


s2 = input("Enter The Second String: ")
s1_lower=s1.lower()
s2_lower=s2.lower()
common_characters = set(s1_lower) & set(s2_lower)
if common_characters:
print("Common Characters are", ','.join(common_characters))
else:
print("There Are No Common Character Between Both The Strings")

Output:

Enter The First String: Arvin


Enter The Second String: arvinj
Common Characters are a,a

C) Python Program to Count the Number of Vowels in a String

string=str(input("Enter a String: "))


vowels=0
for i in string:
if(i=="a" or i=="e" or i=="i" or i=="o" or i=="u" or i=="A" or
i=="E" or i=="I" or i=="O" or i=="U"):
vowels=vowels+1
print("No Of Vowels in the Given String are ", vowels)

Output:

Enter a String: Arvin


No Of Vowels in the Given String are 2

Arvin(2310990386) BE-CSE(G5-A) Page 13


Problem Solving using Python Programming (23CS001)

Arvin(2310990386) BE-CSE(G5-A) Page 14

You might also like