MB

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

1.

Write a program to demonstrate the use of ‘int’


type and operators ‘+’,’-’,’*’.

# This program adds two numbers

num1 = int(input("Enter the first no. :"))


num2 = int(input("Enter the second no. :"))

# Add two numbers


sum = num1 + num2
#Subtracting two numbers
sub = num1-num2
#Multiplying two numbers
multi = num1*num2

# Display the sum


print('The sum of {} and {} is {}'.format(num1, num2, sum))
print('The substraction of {} and {} is {}'.format(num1, num2, sub))
print('The product of {} and {} is {}'.format(num1, num2, multi))

OUTPUT:
Enter the first no. 12
Enter the second no. 8
The sum of 12 and 8 is 20
The substraction of 12 and 8 is 4
The product of 12 and 8 is 96
5. Write a program that swaps the value of two
variables a,b without using the third variable .

# This program swaps value of two variable

num1 = int(input("Enter the first no. :"))


num2 = int(input("Enter the second no. :"))

# Display before swap


print('The values of variable ‘a’ before swapping are: {}'.format(num1))
print('The values of variable ‘b’ before swapping are: {}'.format(num2))

#For swapping the values


num1=num1+num2
num2=num1-num2
num1=num1-num2

# Display after swap


print('The values of variable ‘a’ after swapping are: {}'.format(num1))
print('The values of variable ‘b’ after swapping are: {}'.format(num2))

OUTPUT:
Enter the first no. 12
Enter the second no. 15
'The values of variable ‘a’ before swapping are:
12
The values of variable ‘b’ before swapping are:
15
'The values of variable ‘a’ after swapping are:
15
'The values of variable ‘b’ after swapping are:
12
6. Write a program to check whether the triangle
is possible or not when three angle of triangle
are entered by the user .

# This program to check the possibility of triangle by angles

a = int(input("Enter the first angle :"))


b = int(input("Enter the second angle :"))
c = int(input("Enter the third angle :"))

print(“The angle of triangle given by user are \n{},{},


{}:” .format(a,b,c))
sum=a+b+c

# To check validityof triangle


if sum==180 and a!=0 and b!=0 and c!=0:
print("Triangle is Valid.\nValue of angles is 180")
else:
print("Triangle is Invalid.\nValue of angles is not 180")

OUTPUT:

Enter the first angle: 30


Enter the second angle : 50
Enter the third angle : 100
Triangle is Valid.
Value of angles is 180
7. Write a program to check whether the points
entered by user are in straight line or not.

#Program to check colinearity of three points


x1, y1 = map(int, input('Enter first point values separated by
spaces = ').split())
x2, y2 = map(int, input('Enter second point values separated by
spaces = ').split())
x3, y3 = map(int, input(' Enter third point values separated by
spaces= ').split())

# Check if the points are collinear


if((y3 - y2)*(x2 - x1) == (y2 - y1)*(x3 - x2)):
print('The points (', x1, ',', y1, ') (', x2, ',', y2, ') (', x3, ',', y3, ')', 'are
collinear and lies on the straight line ')
else:
print('The points (', x1, ',', y1, ') (', x2, ',', y2, ') (', x3, ',', y3, ')', 'are not
collinear ')

OUTPUT:
Enter first point values separated by spaces =
00
Enter second point values separated by spaces =
11
Enter third point values separated by spaces=
22
The points ( 0 , 0 ) ( 1 , 1 ) ( 2 , 2 ) are collinear and lies on the straight
line
8. Write a program to find factorial of number .

#Program to find the factorial of a number


num = int(input("Enter a number for which u find the factorial: "))
factorial = 1

# Check if the number is negative, positive or zero


if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

OUTPUT:

Enter a number for which u want to find the factorial:


5
The factorial of 5 is 120
9. Write a program to print fibonacci series .

# Program to display the Fibonacci sequence up to n term


nterms = int(input("Enter the number of terms you want print : "))
# 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")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1 , end=' ')
nth = n1 + n2
n1 = n2
n2 = nth
count =count+1

OUTPUT :
Enter the number of terms you want print :
9
Fibonacci sequence:
0 1 1 2 3 5 8 13 21

10. Write a program to find wheather the


entered number is Armstrong number or not.

#Program to find the factorial of a number


num = int(input("Enter number for angstrom : "))
sum = 0
temp = num
#Find the order of the number
order = len(str(num))
s = order
#Define a function
while temp > 0:
digit = temp % 10
sum += digit ** s
temp = temp//10

if sum==num:
print('It is an Armstrong number')
else:
print('It is not an Armstrong number')

OUTPUT:

Enter number for angstrom :


1634
It is an Armstrong number
11. Write a program to find wheather the
entered number is Palindrome or not.

#Program for Palindrome number


num = int(input("Enter the number : "))
sum = 0
temp = num

#Define a function
while temp > 0:
digit = temp % 10
sum=sum*10+digit
temp = temp//10

if sum==num:
print('It is an palindrome number')
else:
print('It is not an plindrome number')

OUTPUT:

Enter the number :


1661
It is an palindrome number
12. Write a program to find the first 25 odd
numbers using range function.

# Program to find the first 25 odd numbers using range function

# Initialize an empty list to store the odd numbers


odd_numbers = []

# Use range function to iterate through the first 50 numbers


for num in range(1, 50, 2):
odd_numbers.append(num)

# Print the first 25 odd numbers


print(odd_numbers, end=” ”)

OUTPUT:

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
14.(b) Write a program to print the patterns .

# Program to print the patterns


rows = 5
for i in range(0, rows):
# nested loop for each column
for j in range(0, i + 1):
print("*", end=' ')
print("\r")

OUTPUT

*
**
***
****
*****
14.(a) Write a program to print the pattern .

# Program to print the patterns


rows = 5
for i in range(rows + 1, 0, -1):
for j in range(0, i - 1):
print("*", end=' ')
print(" ")

OUTPUT

*****
****
***
**
*
14.(c) Write a program to print the pattern .

# Program to print the patterns


rows = 4
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=' ')
print('')

OUTPUT

1
12
123
1234
14.(d) Write a program to print the pattern .

# Program to print the patterns


rows = 4
t=0
for i in range(1, rows + 1):
for j in range(1, i + 1):
t=t+1
print(t, end=' ')
print('')

OUTPUT

1
23
456
7 8 9 10
14.(e) Write a program to print the pattern .

# Program to print the patterns


n=4
for i in range(1, n + 1):
# Print leading spaces
for j in range(n - i):
print(" ", end="")

# Print asterisks for the current row


for k in range(1, 2*i):
print("*", end="")
print()

OUTPUT

*
***
*****
*******
13. Write a program to compute the total
marks of subject and after finding the
percentage grade them accodringly .

# Program to print the patterns


num_subjects = int(input("Enter the number of subjects: "))
total_marks = 0

for i in range(num_subjects):
marks = float(input(f"Enter marks for subject {i+1}: "))
total_marks += marks
percentage = (total_marks / (num_subjects * 100)) * 100

if percentage >= 90:


grade = "A+"
elif percentage >= 80:
grade = "A"
elif percentage >= 70:
grade = "B
elif percentage >= 60:
grade = "C"
elif percentage >= 50:
grade = "D"
else:
grade = "F"

print(f"Total Percentage: {percentage:.2f}%")


print(f"Grade: {grade}")
OUTPUT:

Enter the number of subjects: 5


Enter marks for subject 1 : 90
Enter marks for subject 2 : 80
Enter marks for subject 3 : 70
Enter marks for subject 4 : 89
Enter marks for subject 5 : 95
Total Percentage: 84.5%
Grade: A
3. Write a program to convert one number type to
another type.

# Program to convert one number type to another

#Implicit Type Conversion in Python


integer_num = int(input("Enter an integer number: "))
float_num = float(input("Enter a float number: "))

new_num = integer_num + float_num

print("Value:",new_num)
print("Data Type:",type(new_num))

# Explicit Type Conversion in Python

integer_num = int(input("Enter an integer number: "))


float_num = float(input("Enter a float number: "))

int_num = int(float_num)
s = integer_num + int_num

print("Value:",s)
print("Data Type:",type(s))
OUTPUT:

Enter an integer number: 15


Enter a float number: 14.25
Value: 29.25
Data Type: <class 'float'>

Enter an integer number: 18


Enter a float number: 28.78
Value: 46
Data Type: <class 'int'>
4. Write a program to perform mathematical
functions using math module.

# Program to perform mathematical functions using Math module

import math
# Calculate the square root of a number
x = 25
sqrt_x = math.sqrt(x)
print("Square root of", x, "is:", sqrt_x)

# Calculate the factorial of a number


n=5
fact_n = math.factorial(n)
print("Factorial of", n, "is:", fact_n)

# Calculate the sine of an angle (in radians)


angle = math.pi / 6
sin_angle = math.sin(angle)
print("Sine of", angle, "radians is:", sin_angle)

# Round a number up to the nearest integer


y = 3.7
ceil_y = math.ceil(y)
print("Ceiling of", y, "is:", ceil_y)
print("Floor of", y, "is:", floor_y)
# Calculate the greatest common divisor (GCD) of two numbers
a = 15
b=5
gcd_ab = math.gcd(a, b)
print("GCD of", a, "and", b, "is:", gcd_ab)

OUTPUT:

Square root of 25 is: 5


Factorial of 5 is: 120
Sine of pi/6 radians is: 0.49999999999999994
Ceiling of 3.35 is: 4
Floor of 3.35 is: 3
GCD of 5 and 15 is: 5
2. Write a program for
Floating ,Complex ,Boolean .

# Program understand boolean, complex ,floating


function

# Various examples and working of float()


# for integers
print(float(21.89))
# for floating point numbers
print(float(8))

# for integer type strings


print(float("23"))
# for floating type strings
print(float("-16.54"))

# for string floats with whitespaces


print(float(" -24.45 \n"))

# for inf/infinity
print(float("InF"))
print(float("InFiNiTy"))
# for NaN(Not a number)
print(float("nan"))
print(float("NaN"))
# Various examples and working of complex()
# nothing is passed
z = complex()
print("complex() with no parameters:", z)

# integer type
# passing first parameter only
complex_num1 = complex(5)
print("Int: first parameter only", complex_num1)
# passing both parameters
complex_num2 = complex(7, 2)
print("Int: both parameters", complex_num2)

# float type
# passing first parameter only
complex_num3 = complex(3.6)
print("Float: first parameter only", complex_num3)
# passing both parameters
complex_num4 = complex(3.6, 8.1)
print("Float: both parameters", complex_num4)
print()

print(type(complex_num1))

# Various examples and working of bool()


# Returns False as x is False
x = False
print(“Result : ”,bool(x))
# Returns True as x is True
x = True
print(“Result : ”,bool(x))
# Returns False as x is not equal to y
x=5
y = 10
print(“Result : ”,bool(x == y))
# Returns False as x is None
x = None
print(“Result : ”,bool(x))

# Returns False as x is an empty sequence


x = ()
print(“Result : ”,bool(x))
# Returns False as x is an empty mapping
x = {}
print(“Result : ”,bool(x))
# Returns False as x is 0
x = 0.0
print(“Result : ”,bool(x))
# Returns True as x is a non empty string
x = 'GeeksforGeeks'
print(“Result : ”,bool(x))
OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy