[Go to site: main page, start]

0% found this document useful (0 votes)
2 views16 pages

Python Additonal Program

Uploaded by

viswajit0412
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)
2 views16 pages

Python Additonal Program

Uploaded by

viswajit0412
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

EX NO.

A1 PROGRAM TO FIND THE BIGGEST OF THREE


NUMBERS

AIM
To write a program in python to find the biggest of three numbers.
ALGORITHM
Step 1: Enter the three numbers.
Step 2: use the following if condition to find the biggest number.
Step 3: if (num1 > num2) and (num1> num3): number 1 is the largest.
Step 4: elif (num2 > num1) and (num2 > num3): Number 2 is largest.
Step 5: else number 3 is largest.
Step 6: Print the output.

PROGRAM
# Step 1: Enter the three numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))

# Step 2–5: Use if conditions to find the biggest number


if (num1 > num2) and (num1 > num3):
largest = num1
elif (num2 > num1) and (num2 > num3):
largest = num2
else:
largest = num3
# Step 6: Print the output
print("The largest number is:", largest)

OUTPUT

RESULT
Thus, the program in python to find the biggest of three numbers was written
and executed successfully.
EX NO.A2 PROGRAM TO CHECK THE GIVEN NUMBER IS EVEN
OR ODD

AIM
To write a program in python to check the given number is even or odd.

ALGORITHM
Step 1: Read the number.
Step 2: Then divide the number by 2.
Step 3: if there is a remainder then the number is odd else the number is even.
Step 4: print the output.

PROGRAM
# Step 1: Read the number
num = int(input("Enter a number: "))

# Step 2 & 3: Divide by 2 and check remainder


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

OUTPUT
RESULT
Thus, the program in python to check the given number is odd or even was
written and executed successfully.

EX NO.A3 PROGRAM TO FIND FACTORIAL OF THE NUMBER


USING WHILE LOOP
AIM
To write a program in python to find the factorial of a number using while loop.
ALGORITHM
Step 1: Get the input from the user.
Step 2: Assume fac = 1.
Step 3: And i = 1.
Step 4: Initialize the while loop.
Step 5: print the output.

PROGRAM
# Step 1: Get the input from the user
num = int(input("Enter a number: "))
# Step 2: Assume fac = 1
fac = 1
# Step 3: Initialize i = 1
i=1
# Step 4: Initialize the while loop
while i <= num:
fac = fac * i
i=i+1
# Step 5: Print the output
print("The factorial of", num, "is:", fac)

OUTPUT

RESULT
Thus, the program in python to find the factorial of the number using while loop
was written and executed successfully.

EX NO.A4 PROGRAM TO FIND FACTORIAL OF A NUMBER USING


FOR LOOP

AIM
To write a program in python to print the reverse of a number.

ALGORITHM
Step 1: Get the input from the user.
Step 2: Assume reverse as 0.
Step 3: Then initialize the while condition
Step 4: Assume Reminder = num % 10
Reverse =(Reverse*10) + Reminder
Num = num // 10
Step 5: print the output
PROGRAM
# Step 1: Get the input from the user
num = int(input("Enter a number: "))

# Step 2: Assume reverse as 0


reverse = 0

# Step 3: Initialize the while condition


while num > 0:
# Step 4: Extract the last digit and build reverse
reminder = num % 10
reverse = (reverse * 10) + reminder
num = num // 10

# Step 5: Print the output


print("The reverse of the given number is:", reverse)

OUTPUT

RESULT
Thus, the program in python to find the reverse of the number was written and
executed successfully.
EX NO.A6 PROGRAM TO CHECK THE GIVEN STRINGS IS
PALINDROME OR NOT

AIM
To write a program in python to check given string is palindrome or not.

ALGORITHM
Step 1: Get the input from the user
Step 2: Assume
Index = 0 check = True
Step 3: Initialize the while condition
Step 4: Initialize the if statement
if n[ index ] == n[-1-index]:
index += 1
return True
return False
Step 5: print the output

PROGRAM
# Step 1: Get the input from the user
s = input("Enter a string: ")

# Step 2: Assume index = 0 and check = True


index = 0
check = True
# Step 3: Initialize the while condition
while index < len(s) // 2:
# Step 4: Compare characters from start and end
if s[index] != s[-1 - index]:
check = False
break
index += 1

# Step 5: Print the output


if check:
print("The given string is a palindrome")
else:
print("The given string is not a palindrome")

OUTPUT

RESULT
Thus, the program in python to check the palindrome condition was written and
executed successfully.
EX NO.A7 PROGRAM TO CHECK GIVEN NUMBER IS PRIME
OR NOT

AIM
To write a program in python to check the given number is prime or not.

ALGORITHM
Step 1: Get the number from the user and the number should be greater than 1.
Step 2: Then check for the factors using for loop
Step 3: if (num % i) == 0:
The number is not a prime number
Step 4: Else the number is a prime number
Step 5: print the output

PROGRAM
# Step 1: Get the number from the user (greater than 1)
num = int(input("Enter a number: "))

if num > 1:
# Step 2: Check for factors using for loop
for i in range(2, num):
# Step 3: If divisible, not prime
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
# Step 4: If no factors found, prime
print(num, "is a prime number")
else:
print(num, "is not a prime number")

OUTPUT

RESULT
Thus, the program in python to check the given number is prime or not was
written and executed successfully.
EX NO.A8 PROGRAM TO GENERATE FIBONACCI SERIES

AIM
To write a program in python to generate the Fibonacci series.

ALGORITHM
Step 1: Get the n as input from the user
Step 2: Then assume n1 = 0
n2 = 1
i=2
Step 3: Initialize the while loop as per the condition
Step 4: Then update the values n1=n2
n2=n3
i=i+1
Step 5: Finally print the output

PROGRAM
# Step 1: Get n as input from the user
n = int(input("Enter the number of terms: "))

# Step 2: Initialize first two terms


n1 = 0
n2 = 1
i = 2 # since first two terms are already defined

print("Fibonacci Series:")
print(n1, n2, end=" ")

# Step 3: Initialize the while loop


while i < n:
n3 = n1 + n2
print(n3, end=" ")

# Step 4: Update values


n1 = n2
n2 = n3
i=i+1

OUTPUT

RESULT
Thus, the program in python to generate Fibonacci series was written and
executed successfully.
EX NO.A9 PROGRAM FOR FLOYDS TRIANGLE

AIM
To write a program in python to the floyds triangle

ALGORITHM
Step 1: Initialize count as 0
Step 2: Assume the range as 1 to 6 using for loop
Step 3: Initialize the j loop
Step 4: Now assume count = count +1
Step 5: Finally print the count

PROGRAM
# Step 1: Initialize count as 0
count = 0

# Step 2: Assume the range as 1 to 6 using for loop


for i in range(1, 7): # 6 rows
# Step 3: Initialize the inner loop
for j in range(1, i + 1):
# Step 4: Update count
count = count + 1
print(count, end=" ")
print() # move to next line after each row
OUTPUT

RESULT
Thus, the program in python to find the Floyds triangle was written and
executed successfully.

EX NO.A10 PROGRAM FOR BUBBLW SORT

AIM
To write a program in python to implement the bubble sort.

ALGORITHM
Step 1: Define the function for bubble sort
Step 2: Use the for loop for pass num in range(len(nlist)-1,0,-1): to enter the
numbers
Step 3: Then within this use the if statement as if nlist =[i]>nlist[i+1]:
Step 4: Compute temp = nlist[i] nlist[i] = nlist[i+1] nlist[i+1]= temp
Step 5: print the output
PROGRAM
# Step 1: Define the function for bubble sort
def bubble_sort(nlist):
# Step 2: Use for loop for passes
for passnum in range(len(nlist)-1, 0, -1):
# Step 3: Inner loop to compare adjacent elements
for i in range(passnum):
if nlist[i] > nlist[i+1]:
# Step 4: Swap values
temp = nlist[i]
nlist[i] = nlist[i+1]
nlist[i+1] = temp

# Example usage
nlist = [64, 34, 25, 12, 22, 11, 90]
print("Original list:", nlist)

bubble_sort(nlist)

# Step 5: Print the output


print("Sorted list:", nlist)

OUTPUT
RESULT
Thus, the program in python to implement the bubble sort was written and
executed successfully.

You might also like