0% found this document useful (0 votes)
70 views

CSC-practicals Answer Key

The document provides 20 Python code snippets with answers to common programming problems. Each snippet includes the question prompt, code to solve the problem, and a separator line. The problems cover topics like prime number checking, perfect number checking, LCM and GCD calculation, palindrome checking, number reversal, Armstrong number checking, digit sum calculation, maximum/minimum number identification, odd-even number sum calculation, Fibonacci series generation, and pattern printing.

Uploaded by

Lalithambiga J
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)
70 views

CSC-practicals Answer Key

The document provides 20 Python code snippets with answers to common programming problems. Each snippet includes the question prompt, code to solve the problem, and a separator line. The problems cover topics like prime number checking, perfect number checking, LCM and GCD calculation, palindrome checking, number reversal, Armstrong number checking, digit sum calculation, maximum/minimum number identification, odd-even number sum calculation, Fibonacci series generation, and pattern printing.

Uploaded by

Lalithambiga J
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/ 11

CSC-Practicals

-Answer key

1)WRITE A PYTHON PROGRAM TO FIND THE GIVEN NUMBER IS A PRIME NUMBER OR NOT.

Ans: n = int(input("Number: "))

if n > 1:

for i in range(2, (n//2) + 1):

if n % i== 0:

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

break

else:

print(n, "is a prime number")

break

else:

print(n, "is a prime number")

else:

print("Number should be greater than 1")

----------------------------------------------------------------------------------

2) WRITE A PYTHON PROGRAM TO FIND THE GIVEN NUMBER IS A PERFECT NUMBER OR NOT.

Ans: n = int(input("Number: "))

sum = 1

for i in range(2, n+1):

if sum >= n:

break

sum += i

if sum == n:

print("It's a perfect square")


else:

print("It is not a perfect square")

-----------------------------------------------------------------------------------------------

3) WRITE A PYTHON PROGRAM TO FIND LCM AND GCD OF THE GIVEN 2 NUMBERS.

Ans: x=int(input("X: "))

y=int(input("Y: "))

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

lcm = (x*y)//hcf

print("HCF: ", hcf)

print("LCM: ", lcm)

-------------------------------------------------------------------------------------------

4) WRITE A PYTHON PROGRAM TO FIND THE GIVEN N DIGIT NUMBER IS A DIGIT PALINDROME OR
NOT.

Ans: n = 121

a = n%10

b = n//100

if a == b:

print("It's a palindrome")

else:

print("It's not a palindrome")

-------------------------------------------------------------------------------------------
5) WRITE A PYTHON PROGRAM TO PRINT THE REVERSE OF THE GIVEN N DIGIT NUMBER.

Ans: n = int(input("Number: "))

reverse_number= 0

while n > 0:

remainder = n % 10

reverse_number= (reverse_number* 10) + remainder

n //= 10

print("The reverse number is ", reverse_number)

----------------------------------------------------------------------------------------

6) WRITE A PYTHON PROGRAM TO FIND THE GIVEN N DIGIT NUMBER IS AN ARMSTRONG


NUMBER OR NOT.

Ans: n = int(input("Value: "))

sum = 0

a=0

b=n

Check = n

remainder = 0

while b > 0:

a += 1

b //= 10

while n > 0:

remainder = n%10

sum += remainder ** 3

n //= 10

if sum == Check:

print("It's an Armstrong number")

else:

print("It's not an Armstrong number")


7) WRITE A PYTHON PROGRAM TO FIND THE SUM OF THE DIGITS OF THE GIVEN N DIGIT NUMBER.

Ans: n = int(input("Value: "))

sum = 0

while n > 0:

sum += n%10

n //= 10

print(sum)

---------------------------------------------------------------------------------

8) WRITE A PYTHON PROGRAM TO FIND THE SMALLEST AND SECOND SMALLEST NUMBER FROM
THE USER INPUT.

Ans: a = int(input("Value of A: "))

b = int(input("Value of B: "))

c = int(input("Value of C: "))

Smallest_NoA= 0

Smallest_NoB= 0

if a > b:

Smallest_NoA= b

Smallest_NoB= a

else:

Smallest_NoA= a

Smallest_NoB= b

if Smallest_NoA> c:

Smallest_NoB= Smallest_NoA

Smallest_NoA= c

elif Smallest_NoB> c:

Smallest_NoB= c

print(Smallest_NoA, Smallest_NoB)

--------------------------------------------------------------------------------------------
9) WRITE A PYTHON PROGRAM TO FIND THE SUM OF ODD NUMBERS AND EVEN NUMBERS GIVEN
AS INPUT.

Ans: n = int(input(""))

Oddsum= 0

EvenSum= 0

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

if i%2 == 0:

EvenSum+= i

else:

Oddsum+= i

print("OddSum:", Oddsum, "EvenSum:", EvenSum)

-------------------------------------------------------------------------------------------------

10) WRITE A PYTHON PROGRAM TO FIND THE LARGEST AND SECOND LARGEST NUMBER FROM
THE USER INPUT.

Ans: a = int(input("Value of A: "))

b = int(input("Value of B: "))

c = int(input("Value of C: "))

LargestNoA= 0

LargestNoB= 0

if a > b:

LargestNoA= a

LargestNoB= b

else:

LargestNoA= b

LargestNoB= a

if LargestNoA< c:

LargestNoB= LargestNoA

LargestNoA= c
elif LargestNoB< c:

LargestNoB= c

print(LargestNoA, LargestNoB)

---------------------------------------------------------------------------------------------------

11) Write a python program to find the prime factors of a given number.

Ans:

n = int(input("Enter no: "))

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

if n%i == 0:

count = 0

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

if i%j == 0:

count += 1

if count == 2:

print(i)

----------------------------------------------------------------------------------------------------------------------------- -----

12) Write a python program to generate the Fibonacci series of n


terms.

Ans:

n = int(input("Terms: "))

n1, n2 = 0, 1

nth = 0

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

print(n1)

nth = n1 + n2
n1 = n2

n2 = nth

-----------------------------------------------------------------------------

13) Write a python program to print the given pattern

1
12
123
1234
1 2345

Ans:
for i in range(1, 6):

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

print(j, end="")

print()

--------------------------------------------------------------------------------------------------------

14) Write a python program to print the given pattern

A
AB
ABC
ABCD
ABCDE

Ans:

for i in range(65, 70):

for j in range(65, i+1):

print(chr(j), end="")

print()
------------------------------------------------------------------------------------------------------------

15) Write a python program to print the given pattern

54321
5432
543
54
5

Ans:

for i in range(1, 6, 1):

for j in range(5, i-1, -1):

print(j, end="")

print()

---------------------------------------------------------------------------------------------------------

16) Write a python program to print the given pattern

1
22
333
4444
55555

Ans:

for i in range(1, 6):

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

print(i, end="")

print()

-------------------------------------------------------------------------------------------
17) Writea python program to find the sum of the series 1 +
(1+22)+(1+22+32)+… n terms.

Ans:

n = int(input("Terms: "))

sum = 0

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

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

sum += j ** 2

print(sum)

-------------------------------------------------------------------------------------------

18) Write a python program to check the given character is a digit,


uppercase alphabet, lowercase alphabet or special character.

Ans:

number = int(input("Number: "))

if number >= 65 and number <= 90:

print("Uppercase Alphabet:", chr(number))

elif number >= 97 and number <= 122:

print("Lowercase Alphabet:", chr(number))

elif number >= 48 and number <= 57:

print("Digit:", chr(number))

else:

print("SpecialCharacter:", chr(number))
19) Write a python program to find Factorial of given number.

Ans:

num = int(input("Num: "))

factorial = 1

if num < 0:

print("Negative!Invalid")

elif num == 0:

print("factorial of", num, "is", 1)

else:

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

factorial *= i

print("factorial of", num, "is", factorial)

-------------------------------------------------------------------------------------

20) Write a python program to find the given quadratic equation


roots are real, equal or imaginary.

Ans:

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

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

c = float(input("c: "))

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

if d > 0:
print("Real Roots", d)

elif d < 0:

print("Imaginary Roots", d)

else:

print("EqualRoots", d)

-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x- Enjoy
-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x

You might also like