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

Additional Questions

The document contains a series of programming tasks that include calculating areas of various shapes, performing arithmetic operations, finding the hypotenuse of a triangle, generating patterns, solving quadratic equations, and more. Each task is presented with code snippets in Python that demonstrate how to implement the required functionality. The tasks cover a wide range of concepts including loops, conditionals, functions, and mathematical calculations.

Uploaded by

annupoonia152005
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)
4 views10 pages

Additional Questions

The document contains a series of programming tasks that include calculating areas of various shapes, performing arithmetic operations, finding the hypotenuse of a triangle, generating patterns, solving quadratic equations, and more. Each task is presented with code snippets in Python that demonstrate how to implement the required functionality. The tasks cover a wide range of concepts including loops, conditionals, functions, and mathematical calculations.

Uploaded by

annupoonia152005
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/ 10

1.Write a menu driven program to print area of rectangle, circle, square and triangle.

# define a function for calculating


# the area of a shapes
def calculate_area(name):\

# converting all characters


# into lower cases
name = name.lower()

# check for the conditions


if name == "rectangle":
l = int(input("Enter rectangle's length: "))
b = int(input("Enter rectangle's breadth: "))

# calculate area of rectangle


rect_area = l * b
print(f"The area of rectangle is
{rect_area}.")

elif name == "square":


s = int(input("Enter square's side length: "))

# calculate area of square


sqt_area = s * s
print(f"The area of square is
{sqt_area}.")
elif name == "triangle":
h = int(input("Enter triangle's height length: "))
b = int(input("Enter triangle's breadth length: "))

# calculate area of triangle


tri_area = 0.5 * b * h
print(f"The area of triangle is
{tri_area}.")

elif name == "circle":


r = int(input("Enter circle's radius length: "))
pi = 3.14

# calculate area of circle


circ_area = pi * r * r
print(f"The area of circle is
{circ_area}.")

elif name == 'parallelogram':


b = int(input("Enter parallelogram's base length: "))
h = int(input("Enter parallelogram's height length: "))
# calculate area of parallelogram
para_area = b * h
print(f"The area of parallelogram is
{para_area}.")

else:
print("Sorry! This shape is not available")

# driver code
if __name__ == "__main__" :

print("Calculate Shape Area")


shape_name = input("Enter the name of shape whose area you
want to find: ")

# function calling
Calculate_area(shape_name)

2. WAP to print sum, difference, product, and division of two numbers entered by the user.

n1 = float(input("Enter First Number: "))

# taking input from user

n2 = float(input("Enter Second Number: "))

# taking input from user

print("Sum =", n1+n2)

# calculating and printing Sum

print("Difference =", n1-n2)

# calculating and printing Difference

print("Product =", n1*n2)

# calculating and printing Product

print("Quotient =", n1/n2)

# calculating and printing Quotient

3. Hypotenuse of a triangle.

from math import sqrt

print("Input lengths of shorter triangle sides:")

a = float(input("a: "))

b = float(input("b: "))

c = sqrt(a**2 + b**2)

print("The length of the hypotenuse is:", c)

4. Square pattern.
side = int(input("Please Enter any Side of a Square : "))

print("Square Star Pattern")

for i in range(side):

for i in range(side):

print('*', end = ' ')

print()

5. Find roots of a quadratic equation using math module.

import cmath

a = input(“enter value of a: ”)

b = input(“enter value of b: ”)

c = input(“enter value of c: ”)

d = (b**2) - (4*a*c)

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

sol2 = (-b+cmath.sqrt(d))/(2*a)

print('The solution are {0} and {1}'.format(sol1,sol2))

6. Area of a triangle.

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

b = float(input('Enter second side: '))

c = float(input('Enter third side: '))

s = (a + b + c) / 2

area = (s*(s-a)*(s-b)*(s-c)) ** 0.5

print('The area of the triangle is %0.2f' %area)

7. WAP to print the below pattern


8. Check a number if it is even or odd.

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

if (num % 2) == 0:

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

else:

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

9. WAP to print maximum no. among three numbers.

num1 = float(input("Enter first number: "))

num2 = float(input("Enter second number: "))

num3 = float(input("Enter third number: "))

if (num1 >= num2) and (num1 >= num3):

largest = num1

elif (num2 >= num1) and (num2 >= num3):

largest = num2

else:

largest = num3

print("The largest number is", largest)

10. WAP to print sum of n numbers.

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

if num < 0:

print("Enter a positive number")

else:

sum = 0
# use while loop to iterate un till zero

while(num > 0):

sum += num

num -= 1

print("The sum is",sum)

11. Factorial of a number.

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

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)

12. Fibonacci series

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

n1, n2 = 0, 1

count = 0

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)

nth = n1 + n2

n1 = n2

n2 = nth

count += 1

13. WAP that takes input from the user until he enters zero and then count even and odd
numbers.

num_str = input("Input an integer (0 terminates):")

num_int=int(num_str)

even_count=0

odd_count=0

even_sum=0

odd_sum=0

while num_int !=0:

num_str = input("Input an integer (0 terminates):")

num_int=int(num_str)

for num_int in num_str:

if num_int%2 == 0:

even_count += 1

else:

odd_count +=1

print("")

print("Sum of odds:", odd_sum)

print("Sum of evens:", even_sum)

print("Even count:", even_count)

print("Odd count:", odd_count)

print("Total positive int count:")

14. WAP that checks if a no. is prime or composite. num = int(input("Enter a number: "))
# define a flag variable

flag = False

if num == 1:

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

elif num > 1:

# check for factors

for i in range(2, num):

if (num % i) == 0:

# if factor is found, set flag to True

flag = True

# break out of loop

break

# check if flag is True

if flag:

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

else:

print(num, "is a prime number")

15. LCM

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

b=int(input("Enter the second number:"))

maximum = max(a, b)

while(1):

if(maximum %a==0 and maximum %b==0):

print("LCM is:",maximum)

break

maximum=maximum+1

16. HCF
def calculate_hcf(x, y):

# selecting the smaller number

if x > y:

smaller = y

else:

smaller = x

for i in range(1,smaller + 1):

if((x % i == 0) and (y % i == 0)):

hcf = i

return hcf

# taking input from users

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

# printing the result for the users

print("The H.C.F. of", num1,"and", num2,"is", calculate_hcf(num 1, num2))

17. Co prime

num1= int(input("Please enter the first number: "))

num2= int(input("Please enter the second number: "))

mn = min(num1, num2)

for i in range(1, mn+1):

if num1%i==0 and num2%i==0:

hcf = i

if hcf == 1:

print("Yes! They are Co-Prime.")

else:

print("Sorry! They are not Co-Prime.")

18. Print Armstrong numbers till a number entered by the user.


19. Check a number if it is a palindrome no. or not.

You might also like