Csc201 Test Question Solution
Csc201 Test Question Solution
import cmath
#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
#4. Write a Python program to find the average of 10 numbers using while
loop.
#6. Write the python program to find the sum, difference and product of two
numbers
sum_of_the_two_numbers = x + y
difference_of_the_two_numbers = x - y
product_of_the_two_numbers = x * y
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
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
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")
#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)
temperature_in_fahrenheit = (temperature_in_degree*9/5)+32
#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.
even = 0
for i in range(1,n+1):
if i % 2 ==0:
even = even + i
#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)
#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))