0% found this document useful (0 votes)
19 views14 pages

Tialkoutput 1-15

The document contains 15 Python programs with sample code and output for each program. The programs cover basic concepts like arithmetic operations, area calculation and conditional statements. More advanced programs include checking palindrome, Armstrong, perfect and Fibonacci numbers.

Uploaded by

Shivam
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)
19 views14 pages

Tialkoutput 1-15

The document contains 15 Python programs with sample code and output for each program. The programs cover basic concepts like arithmetic operations, area calculation and conditional statements. More advanced programs include checking palindrome, Armstrong, perfect and Fibonacci numbers.

Uploaded by

Shivam
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
You are on page 1/ 14

NAME:TILAK RAWAT COURSE: BCA-G1 SEMESTER: 4th

ROLL-NO: 2222095(78) STUDENT ID: 22661014

1. Python program to print "Hello Python"


CODE:

print("hello world")

OUTPUT
NAME:ABHINAV PAL COURSE: BCA-G1 SEMESTER: 4th
ROLL-NO: 2221037(05) STUDENT ID: 220411449 2.

Python program to do arithmetical operations

CODE:

a=7
b=2
# addition print ('Sum: ', a
+ b) # subtraction print
('Subtraction: ', a - b)
# multiplication
print ('Multiplication: ', a * b)
# division
print ('Division: ', a / b) #
floor division
print ('Floor Division: ', a // b)
# modulo
print ('Modulo: ', a % b)
# a to the power b print
('Power: ', a ** b)

OUTPUT:

Python program to find the area of a triangle


NAME:ABHINAV PAL COURSE: BCA-G1 SEMESTER: 4th
ROLL-NO: 2221037(05) STUDENT ID: 220411449 3.

CODE:
# Python Program to find the area of
triangle a =
b=
c =# Uncomment below to take inputs from
the user

# a = float(input('Enter first side: '))


# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))
# calculate the semi-perimeter s =
(a + b + c) / 2 # calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5 print('The
area of the triangle is %0.2f' %area)

OUTPUT:

Python program to solve quadratic equation


NAME:ABHINAV PAL COURSE: BCA-G1 SEMESTER: 4th
ROLL-NO: 2221037(05) STUDENT ID: 220411449 4.

# Solve the quadratic equation ax**2 + bx + c = 0

# import complex math

module import cmath a = 1 b =

5c=6

# calculate the discriminant d = (b**2) - (4*a*c) #

find two solutions sol1 = (-b-cmath.sqrt(d))/(2*a)

sol2 = (-b+cmath.sqrt(d))/(2*a) print('The solution are

{0} and {1}'.format(sol1,sol2))

OUTPUT:

Python program to swap two variables

CODE:

# Python program to swap two variables

x=5y
= 10
NAME:ABHINAV PAL COURSE: BCA-G1 SEMESTER: 4th
ROLL-NO: 2221037(05) STUDENT ID: 220411449 5.

# To take inputs from the user


#x = input('Enter value of x: ') #y
= input('Enter value of y: ')

# create a temporary variable and swap the values


temp = x x = y y = temp

print('The value of x after swapping: {}'.format(x)) print('The


value of y after swapping: {}'.format(y))

OUTPUT:

Python Program to Check if a Number is Positive, Negative or Zero

CODE:

# Default function to run if else condition def


NumberCheck(a):
# Checking if the number is positive
if a > 0:
print("Number given by you is Positive")
# Checking if the number is negative elif
a < 0:
NAME:ABHINAV PAL COURSE: BCA-G1 SEMESTER: 4th
ROLL-NO: 2221037(05) STUDENT ID: 220411449 6.

print("Number given by you is Negative")


# Else the number is zero
else:
print("Number given by you is zero")
# Taking number from user a =
float(input("Enter a number as input value: "))
# Printing result
NumberCheck(a)

OUTPUT:

Python Program to Check if a Number is Odd or Even

CODE:

# Python program to check if the input number is odd or even.


# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.

num = int(input("Enter a number: ")) if


(num % 2) == 0:
print("{0} is Even".format(num)) else:
NAME:ABHINAV PAL COURSE: BCA-G1 SEMESTER: 4th
ROLL-NO: 2221037(05) STUDENT ID: 220411449 7.

print("{0} is Odd".format(num))

OUTPUT:

Python Program to Check Leap Year

CODE:

# Python program to check if year is a leap year or not

year = 2024

# To get year (integer input) from the user


# year = int(input("Enter a year: "))

# divided by 100 means century year (ending with 00)


NAME:ABHINAV PAL COURSE: BCA-G1 SEMESTER: 4th
ROLL-NO: 2221037(05) STUDENT ID: 220411449 8.

# century year divided by 400 is leap year if


(year % 400 == 0) and (year % 100 == 0):
print("{0} is a leap year".format(year))

# not divided by 100 means not a century year


# year divided by 4 is a leap year elif
(year % 4 ==0) and (year % 100 != 0):
print("{0} is a leap year".format(year))

# if not divided by both 400 (century year) and 4 (not century year)
# year is not leap year else:
print("{0} is not a leap year".format(year))

OUTPUT:

Python Program to Check Prime Number

CODE: num
= 11
# If given number is greater than 1 if
num > 1:
# Iterate from 2 to n / 2 for i
in range(2, int(num/2)+1):
# If num is divisible by any number between
# 2 and n / 2, it is not prime
if (num % i) == 0:
NAME:ABHINAV PAL COURSE: BCA-G1 SEMESTER: 4th
ROLL-NO: 2221037(05) STUDENT ID: 220411449 9.

print(num, "is not a prime number")


break else: print(num, "is a prime
number") else:
print(num, "is not a prime number")

OUTPUT:
NAME:ABHINAV PAL COURSE: BCA-G1 SEMESTER: 4th
ROLL-NO: 2221037(05) STUDENT ID: 220411449

10. Write a python program to check if a value entered by a user is Palindrome or not.

CODE:

# function which return reverse of a string

def isPalindrome(s):
return s == s[::-1]

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

if ans:
print("Yes") else:
print("No")

OUTPUT:

11. Write a python program to check if a value entered by a user is Armstrong or not.
CODE:
# Python program to check if the number is an Armstrong number or not
NAME:ABHINAV PAL COURSE: BCA-G1 SEMESTER: 4th
ROLL-NO: 2221037(05) STUDENT ID: 220411449
# take input from the user num =
int(input("Enter a number: "))

# initialize sum sum


=0

# find the sum of the cube of each digit


temp = num while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

# display the result if


num == sum:
print(num,"is an Armstrong number") else:
print(num,"is not an Armstrong number")

OUTPUT:

12.Consider a number entered by a user. Now calculate the Factorial of this number.

CODE:

# Define a function named 'factorial' that calculates the factorial of a number 'n' def
factorial(n):
# Check if the number 'n' is 0
if n == 0:
# If 'n' is 0, return 1 (factorial of 0 is 1)
return 1 else:
NAME:ABHINAV PAL COURSE: BCA-G1 SEMESTER: 4th
ROLL-NO: 2221037(05) STUDENT ID: 220411449

# If 'n' is not 0, recursively call the 'factorial' function with (n-1) and multiply it with 'n'
return n * factorial(n - 1)

# Ask the user to input a number to compute its factorial and store it in variable 'n' n
= int(input("Input a number to compute the factorial: "))

# Print the factorial of the number entered by the user by calling the 'factorial' function
print(factorial(n))

OUTPUT:

13. Write a python program to print the Fibonacci sequence upto the range entered by a user.

CODE:

nterms = int(input("How many terms? "))


n1, n2 = 0, 1 count = 0 if nterms <= 0:
print("Please enter a positive integer") elif
nterms == 1:
NAME:ABHINAV PAL COURSE: BCA-G1 SEMESTER: 4th
ROLL-NO: 2221037(05) STUDENT ID: 220411449

print("Fibonacci sequence upto",nterms,":")


print(n1) else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2 #
update values
n1 = n2 n2 =
nth count += 1

OUTPUT:

14. Write a python program to check whether the number entered by the user is a Perfect number or
not.

CODE:

n = int(input("Enter any number: "))


sum1 = 0 for i in range(1, n): if(n
% i == 0):
sum1 = sum1 + i if
(sum1 == n):
print("The number is a Perfect number!") else:
print("The number is not a Perfect number!")

OUTPUT:
NAME:ABHINAV PAL COURSE: BCA-G1 SEMESTER: 4th
ROLL-NO: 2221037(05) STUDENT ID: 220411449

15. Consider any long string. Now replace each space between two words with the tab (i.e. the space
created when you press the key 'Tab' from your keyboard).

CODE:

# Define your long string


long_string = "This is a long string with spaces between words."

# Replace spaces with tabs tab_replaced_string


= long_string.replace(' ', '\t')

# Print the result print(tab_replaced_string)

OUTPUT:

You might also like