0% found this document useful (0 votes)
12 views7 pages

Csc201 Test Question Solution

The document contains a series of Python programs that demonstrate various programming concepts, including calculating roots of quadratic equations, checking for prime numbers, finding the product of real numbers, and performing basic arithmetic operations. Each program is designed to solve a specific problem, such as calculating averages, finding maximum values, and handling user input. The document serves as a comprehensive guide for beginners to learn Python through practical examples.

Uploaded by

miraspence5
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)
12 views7 pages

Csc201 Test Question Solution

The document contains a series of Python programs that demonstrate various programming concepts, including calculating roots of quadratic equations, checking for prime numbers, finding the product of real numbers, and performing basic arithmetic operations. Each program is designed to solve a specific problem, such as calculating averages, finding maximum values, and handling user input. The document serves as a comprehensive guide for beginners to learn Python through practical examples.

Uploaded by

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

#1.

Write a python program to calculate the roots of a quadratic equation

#the formula to find the root of a quadratic equation is: ax**2 + bx + c = 0

import cmath

#cmath: this module provides access to mathematical functions for complex


numbers.

#defining the coefficient of the quadratic equation


a = int(input("Enter number of a:"))
b = int(input("Enter number of b:"))
c = int(input("Enter number of c:"))

#calculate the discriminant


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

#key points about Discriminant


if D > 0:
print("Two distinct real roots")
elif D == 0:
print("One repeated real roots")
elif D < 0:
print("Two complex or non-real root")

#calculate the roots


solve_for_positive = (-b+cmath.sqrt(D))/(2*a)
solve_for_negative = (-b-cmath.sqrt(D))/(2*a)

#print the roots


print(f"The roots of the equation are: {solve_for_positive} and
{solve_for_negative}")

#2. Write a program to find if the given number is prime or not

#prime numbers are numbers that are only divisible by 1 and itself
Number = int(input("Enter a number:"))
for i in range(2,Number):
if Number % i == 0 :
print(f"{Number} is not a prime number")
break
else:
print(f"{Number} is a prime number")

#3. Write a python program to find the product of set of real numbers

#Real number is any number that can be written as a decimal number,


#with any number of decimal places including an infinite number of decimal
places.

count = int(input("Enter Number Of Count:"))


product_of_real_number = 1
for i in range(count):
Value = float(input("Enter a real numbers:"))
product_of_real_number = product_of_real_number * Value
print(f"The product of the real number is:", product_of_real_number)

#4. Write a Python program to find the average of 10 numbers using while
loop.

#initiate the sum of the numbers


Sum = 0
#inititate the counter of the numbers
count = 0
#using while loop to get the average:
while count < 10:
Number_Count = float(input("Enter a number:"))
Sum = Sum + Number_Count
count = count + 1
average = Sum/10
print(f"The average of these number is: {average}")

#5. Write a python code to find the maximum of three numbers

#let A,B and C be the variable of the numbers.


A = int(input("Enter NUmber for A:"))
B = int(input("Enter NUmber for B:"))
C = int(input("Enter NUmber for C:"))
if A > B and A > C:
print(f"The maximum number of three is: {A}")
elif B > A and B > C:
print(f"The maximum number of three is: {B}")
else:
print(f"The maximum number of three is: {C}")

#6. Write the python program to find the sum, difference and product of two
numbers

#let x and y be the variables of the two numbers.


x = int(input("Enter first number:"))
y = int(input("Enter second number:"))

sum_of_the_two_numbers = x + y

difference_of_the_two_numbers = x - y

product_of_the_two_numbers = x * y

print(f"The sum of the two numbers is: {sum_of_the_two_numbers}, \n"


f"The difference of the two numbers is:
{difference_of_the_two_numbers}, \n"
f"The product of the two numbers is: {product_of_the_two_numbers}")

#7. Write a python program to find factorial of a number

#let the number of factorial be 5

# compute the factorial of 5

from math import factorial

Number_Of_Factorial =5
if Number_Of_Factorial ==0:
print(Number_Of_Factorial,"is not a factorial of 5")
else:
print(Number_Of_Factorial * factorial(Number_Of_Factorial-1))

#8. write a phyton program to check whether the given integer is a multiple
of 5

#let N represent the variable of the numbers.

N = int(input("Enter a Number:"))

if N % 5 == 0:
print(N, "is a multiple of 5")
else:
print(N, "is not a multiple of 5")

#9. write a phyton program to check whether the given integer is a multiple
of both 5 and 7

#let M represent the variable of the numbers.

M = int(input("Enter a Number:"))
if M % 5 == 0 and M % 7 ==0:
print(M, "is a multiple of both 5 and 7")
elif M % 5 == 0 and M % 7 != 0:
print(M, "is a multiple of 5 but not a multiple of 7")
elif M % 5 != 0 and M % 7 == 0:
print(M, "is not a multiple of 5 but a multiple of 7")
else:
print(M, "is not a multiple of both 5 and 7")

#10. Write a python program to find the biggest of five numbers.

number1 = int(input("Enter NUmber1 :"))


number2 = int(input("Enter NUmber2:"))
number3 = int(input("Enter NUmber3:"))
number4 = int(input("Enter NUmber4:"))
number5 = int(input("Enter NUmber5:"))

print(max(number1, number2, number3, number4, number5))

#11. Write a python program to find the circumference and area of a circle
with a given radius.

#let pi = 3.142
#let r = 9.5
#then compute the expression since we have all parameters
pi = 3.142
r = 9.5
Area_Of_Circle = pi*r**2
print(f"The Area of a Circle with radius {r} is:", Area_Of_Circle)

#12. Write a python program to convert temperature in degree centigrade to


Fahrenheit.
#0 degree celsius made 32 fahrenheit
#computing the expression.

temperature_in_degree = int(input("Enter temperature in Degree:"))

temperature_in_fahrenheit = (temperature_in_degree*9/5)+32

print("Temperature in fahrenheit is:", temperature_in_fahrenheit )

#13. Write a phyton program that finds the sum of three number. If the
numbers are equal ,return three times their sum.
#let I,K and J be the variable of the three numbers
I=int(input("first number"))
J=int(input("second number"))
K=int(input("third number"))
if I == J == K:
print(3*(I+J+K))
else:
print(I+J+K)

#14. Write a python program to calculate the sum of all even numbers to n.
#even numbers are numbers that are divisible by 2 without reminder.

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

even = 0

for i in range(1,n+1):
if i % 2 ==0:
even = even + i

print("The sum of even number till",n,"is", even )

#daviin solution ->09051217349


#incase u don't understand the below code message the number above.

#15. write a python program that stores the biodata of a student


name = input("name: ")
while True:
try:
level = int(input("level (e.g 100): "))
break
except:
print("enter nuber value ")
sex = input('sex: ')
department = input("Department: ")
address = input("Home address (e.g room 4, height hostel, Ogbomosho): ")
storage = {
"name" : name.strip(),
"level" : str(level) + "L",
"sex" : sex.strip(),
"department": department,
"address" : address.strip()
}
print(storage)

#16. write a python program that sums the digits of a given integers
n = input("Enter the number ")
x = 0
h = 0
while x < len(n):
h += int(n[x])
x+=1
print(h)

#17. write a python program that removes a duplicate from the list
N = ["apple", 8, "oranges", "knife", 11,8,"knife","apple"]
x = 0
while x < len(N):
if N.count(N[x])>1:
h = N.index(N[x])
N.pop(h)
x = 0
x+=1
print(N)

#18. write a python program that displays the numbers between 1 and
50(inclusive)
#replacing multiples of 3 with fizz and 5 with buzz
#and of both with FizzBuzz

out = []
h = ""
for x in range(1,50):
if x % 3 == 0 and x%5 != 0:
h = "Fizz"
elif x % 3 != 0 and x%5 == 0:
h = "Buzz"
elif x % 3 == 0 and x%5 == 0:
h = "FizzBuzz"
else:
h = x
print(h)
out.append(h)
#Using array printing
print(out)

#19. write a python program that displays the mean median and mode of 100
real numbers
a = int(input("Enter the start number:"))
def mean(a):
sum = 0
for x in range(a,a + 100):
sum += x
return sum/x
def median (a):
mid = a+99
return mid/2
def mode(a):
return a+99
print("The mean, median, and mode of",a,"to",(a+99),"are
respectively:",mean(a),",",median(a),",",mode(a))

#20. write a python program that displays the multiples of 3 between 10 and
500
multiples = []
for x in range(10,501):
if x % 3 == 0:
multiples.append(x)
print("The multiples of 3 between 10 and 500 are", multiples)

#21. Write a python program that displays the prime numbers between 1 and
given number N
N = int(input("End number: "))
primes = []
def prime_number(n):
for x in range(2, round(n**0.5)+1):
if n%x == 0:
return False
else:
continue
return True
for x in range(1, N + 1):
if(prime_number(x)):
primes.append(x)
print("The prime numbers between 1 and",N,"are\n", primes)

#22. Write a python program that checks if a number is a prime number


N = int(input("Enter number to check: "))
def prime_number(n):
for x in range(2, round(n**0.5)+1):
if n%x == 0:
return False
else:
continue
return True
if prime_number(N):
print(N,"is a prime number")
else:
print(N,"is not a prime number")

#23. Write a python program to calculate the sum of N numbers using a list
N = int(input("How many numbers are you adding: "))
nums = []
for x in range(1,N+1):
h = int(input(f"Number {x}: "))
nums.append(h)
results = 0
for x in range(len(nums)):
results += nums[x]
print(results)

#24. write a python program that swaps the first and last elements of a list
a = [1,4,5,3,77,3]
def swap(a):
x = a[0]
y = a[-1]
a[0] = y
a[-1] = x
return a
print(swap(a))

You might also like