0% found this document useful (0 votes)
3 views9 pages

Name: Diksha Upadhyay Class: Xii Section: E Roll Number: 13 Subject: Computer Science Subject Code: 083

The document contains Python programming exercises for a computer science class, including functions to check for twin primes, palindromes, Armstrong numbers, and the frequency of vowels in a string. Each exercise includes a problem statement, a sample input/output, and the corresponding Python code solution. The solutions demonstrate the use of functions and basic control structures in Python.

Uploaded by

ragemanop
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)
3 views9 pages

Name: Diksha Upadhyay Class: Xii Section: E Roll Number: 13 Subject: Computer Science Subject Code: 083

The document contains Python programming exercises for a computer science class, including functions to check for twin primes, palindromes, Armstrong numbers, and the frequency of vowels in a string. Each exercise includes a problem statement, a sample input/output, and the corresponding Python code solution. The solutions demonstrate the use of functions and basic control structures in Python.

Uploaded by

ragemanop
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/ 9

NAME : DIKSHA UPADHYAY

CLASS : XII
SECTION :E
ROLL NUMBER : 13
SUBJECT : COMPUTER SCIENCE
SUBJECT CODE : 083
Q1) Solve using Python code to accept two numbers. Check and
display whether they are twin prime or not by using function. The
function returns 1 if a number is prime, otherwise returns 0. Twin
prime numbers are a pair of prime numbers whose difference is 2.
[Hint: (5,7),(11,13),(17,19) are twin primes.]

Solution :
def prime(n):
f=1
if n<=1:
return 0
for i in range(2,n//2 + 1):
if n%i==0:
f=0
return f
return f
def check_twin_prime(a, b):
if prime(a) and prime(b) and abs(a - b) == 2:
print(f"({a}, {b}) are twin primes.")
else:
print(f"({a}, {b}) are not twin primes.")

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


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

check_twin_prime(num1, num2)
OUTPUT:
Q2) Write a Python code to accept a number and check whether
the number is a palindrome or not. Use a function def Palin(n)
which returns a number after reversing its digits.
Sample Input: 343
Sample Output: It is a Palindrome Number

Solution :
def Palin(n):
a=n
c=0
while a!=0:
r=a%10
a=a//10
c=c*10 + r
if n==c:
print("palindrome")
else:
print("not palindrome")
num1=int(input("Enter a number: "))
Palin(num1)
OUTPUT:
Q3) Write a Python code to input a number and use a function def
Armstrong() to receive the number. The method will return 1 if the
number is Armstrong, otherwise it will return 0.
For example, 153 is an Armstrong number because 13+53+33= 153

Solution :
def Armstrong(n):
b=n
len1 = len(str(n))
s=0
while b!=0:
r = b%10
s += r**len1
b = b//10
if s == n:
return 1
else:
return 0
num = int(input("Enter a number: "))
if Armstrong(num) == 1:
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
OUTPUT:
Q4) Write a Python code to accept a string in a mixed case. Pass the
string to a function def Freq (String). The function should find and
display the frequency of each vowel.
Sample Input: Understanding Computer Science
Sample Output: Frequency of 'a' or 'A' = 1
Frequency of 'e' or 'E' = 4
Frequency of 'i' or 'T' = 2
Frequency of 'o' or 'O' = 1
Frequency of 'u' or 'U' = 2

Solution :
def Freq(String):
String = String.lower()
vowels = {'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}
for char in String:
if char in vowels:
vowels[char] += 1
for v in vowels:
print(f"Frequency of '{v}' or '{v.upper()}' = {vowels[v]}")
text = input("Enter a string: ")
Freq(text)
OUTPUT :

You might also like