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

Python Assign

The document contains 15 programming problems with sample code and output for each problem. The problems include writing programs to: 1) Calculate the sum of a number series 2) Find the sum of digits of a number 3) Generate a list and tuple from comma separated user input 4) Subtract the sum of digits from a number repeatedly 5) Count prime numbers below a given range 6) Check if a number is a disarium number 7) Check if a number is a tech number 8) Check if a number is a spy number 9) Print numbers with all even digits between 100-400 10) Check if a key exists in a dictionary 11) Count vowels, conson

Uploaded by

PRATIKSHA BHOYAR
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Python Assign

The document contains 15 programming problems with sample code and output for each problem. The problems include writing programs to: 1) Calculate the sum of a number series 2) Find the sum of digits of a number 3) Generate a list and tuple from comma separated user input 4) Subtract the sum of digits from a number repeatedly 5) Count prime numbers below a given range 6) Check if a number is a disarium number 7) Check if a number is a tech number 8) Check if a number is a spy number 9) Print numbers with all even digits between 100-400 10) Check if a key exists in a dictionary 11) Count vowels, conson

Uploaded by

PRATIKSHA BHOYAR
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Name : Pratiksha Bhoyar Roll No[MC22F14F010]

1.Write a program that accepts an integer(n) and computes the value


of n+nn+nnn.....

num=int(input("Enter the number for series: "))


t=int(input("Enter number of terms : "))
sum=0
term=0
while t:
term=term*10+num
print(term,end=",")
sum=sum+term
t=t-1
print("\n Sum of series is : ",sum)

Output:

2. write a program to find sum of digits of number in single digit using nested
while loop
n=int(input("Enter a number: "))

sum=0

while(n>0):

rem=n%10

sum=sum+rem

n=n//10

while sum>9 and n==0:

n=sum

sum=0

print("The Addition of digits of number is: ",sum)


Output:

3.WAP which accepts sequence of comma separated number from


user and generate a list and tuple with those numbers.
num=input("Enter numbers separated by comma: ")
lst=num.split()
tuple=tuple(lst)
print("list of the given numbers: ",lst)
print("Tuple of these numbers is : ",tuple)

output:
4.WAP which accepts positive number and subtract from this number the sum of
digits and so on.Continues this operation until number is positive
num=int(input("Enter a positive number : "))

Cnum=num

sum=0

while(Cnum!=0):

n=Cnum%10

sum=sum+n

Cnum=Cnum//10

print(sum)

print(num-sum)

Output:
5. WAP to print numbers of prime number less than or equal to given
range.

def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
def count_primes(limit):
count = 0
for num in range(2, limit + 1):
if is_prime(num):
count += 1
return count
number = int(input("Enter an integer: "))
prime_count = count_primes(number)
print("The number of prime numbers less than or equal to", number, "is:", prime_count)
Output:
6. WAP to chech for disarium number.

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


length=len(str(n))
temp=n
sum=0
rem=0
while temp>0:
rem=temp%10
sum+=int(rem**length)
temp//=10
length-=1
print("Sum of digits is : ",sum)
if sum==n:
print(n,"is a Disarium number")
else:
print(n,"is not a Disarium number")

output:
7.WAP to check Tech number or not

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


st=str(n)
a=st[:len(st)//2]
b=st[len(st)//2:]
sum=int(a)+int(b)
sqr=sum**2
if(sqr==n):
print(n,"is Tech number")
else:
print(n,"is not Tech number")

output:
8. WAP to chech number is Spy number.

num=int(input("enter a number "))


s=0
prod=1
while(num>0):
b=num%10
s=s+b
prod=prod*b
num=num//10
if(s==prod):
print('It is a Spy number')
else:
print('It is not a Spy number')

output:
9. WAP to print number between 100 to 400 where each digit of number is even
number.

a,b=100,400
while a!=b:
d=a
while d!=0:
d,k=divmod(d,10)
if k%2!=0:
break
else:
print(a,end=",")
a+=2

output:
10 WAP to check the key is already exist in dictionary.

d={'A':10,'B':20,'C':30,'D':40,'E':'pratiksha'}
key=input("Enter key to chcek : ")
if key in d.keys():
print("Key is present and value of key is : ")
print(d[key])
else:
print("Key is not present")

Output:
11.Given an input string, write a program to count number of vowels, consonant and
symbols available in it.

str=input("Enter a string:")
def countCharacterType(str):
vowels = 0
consonant = 0
specialChar = 0
digit = 0
for i in range(0, len(str)):
ch = str[i]
if ( (ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z') ):
ch = ch.lower()
if (ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u'):
vowels += 1
else:
consonant += 1
elif (ch >= '0' and ch <= '9'):
digit += 1
else:
specialChar += 1
print("Vowels:", vowels)
print("Consonant:", consonant)
print("Digit:", digit)
print("Special Character:", specialChar)
countCharacterType(str)

Output:
12 WAP to print the string eliminating vowels.

st=input("Enter a string: ")


vowel='aeiouAEIOU'
newst=' '
for ch in st:
if ch not in vowel:
print(ch,end="" )

Output:

13. wap to return sum and average of digits present in string.

str=input("Enter a string: ")


lst=[]
for i in str:
if i >'0' and i<='9' :
lst.append(int(i))
print(sum(lst))
print(sum(lst)/len(lst))

output:
14 Count occurence of all characters within a string.

str1=input("Enter a string : ")


count={}
for i in str1:
if i in count:
count[i]+=1
else:
count[i]=1
print("count of all characters in string : ",str(count))

output:

15.Write a Python program to find common divisors between two numbers in


a given pair.

num1 = int(input("ENTER FIRST NUMBER : "))


num2 = int(input("ENTER SECOND NUMBER : "))
divisor = 0
print("THE COMMON DIVISORS OF NUMBER ",num1," AND ",num2," ARE -")
for i in range(1,min(num1,num2)+1):
if num1%i == num2%i == 0:
divisor = i
print(divisor)
output:

You might also like