Ticker

6/recent/ticker-posts

Python subjective question for competitive exam (22)

Q1.Program to find number of words in string.

def wordinstring(s): wo=(len(s.split())) return(wo) s=input("enter string to find number of word in string") print("number of words in string is",wordinstring(s)) Output >>> ==================== RESTART: C:/Users/Dell/Desktop/g.py ==================== enter string to find number of word in string my name is xyz number of words in string is 4



Q2.program to count the frequency of each word in string.

def wordinstring(s): counts={} words=s.split() for word in words: if word in counts: counts[word]+=1 else: counts[word]=1 return counts s=input("enter string to find number of word in string") print("number of words in string is",wordinstring(s)) Output >>> ==================== RESTART: C:/Users/Dell/Desktop/g.py ==================== enter string to find number of word in string ram drink coffee also ram drink tea number of words in string is {'ram': 2, 'drink': 2, 'coffee': 1, 'also': 1, 'tea': 1}



Q3.Program to find entered year is leap year or not.

def leapyear(year): if((year%4==0)and (year%100!=0)): print("given year is leap year") else: print("given year is not a leap year") year=int(input("enter year")) leapyear(year) Output >>> ==================== RESTART: C:/Users/Dell/Desktop/g.py ==================== enter year2020 given year is leap year


Q4.Program to find number is divisible by 5 and 7 or not.

number=int(input("enter number")) if((number%5==0)and(number%7==0)): print("number is divisble by 5 and 7 both") else: print("number is not divisible by 5 and 7 both") Output >>> ==================== RESTART: C:/Users/Dell/Desktop/g.py ==================== enter number12 number is not divisible by 5 and 7 both >>> ==================== RESTART: C:/Users/Dell/Desktop/g.py ==================== enter number35 number is divisble by 5 and 7 both


Q5.Program to display all integers within the range 10-50 whose sum of digits is an even number.

for i in range(10,50): num=i sum=0 while(num!=0): digit=num%10 sum=sum+digit num=num//10 if(sum%2==0): print(i) Output ==================== RESTART: C:/Users/Dell/Desktop/g.py ==================== 11 13 15 17 19 20 22 24 26 28 31 33 35 37 39 40 42 44 46 48 >>>



Post a Comment

0 Comments