Python Assign
Python Assign
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
n=sum
sum=0
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.
output:
7.WAP to check Tech number or not
output:
8. WAP to chech number is 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.
Output:
output:
14 Count occurence of all characters within a string.
output: