CS 4 Ans - Introduction To Python
CS 4 Ans - Introduction To Python
1. Write a program which will find factors of given number and find whether
the factor is even or odd.
Input:
Number: 50
Output:
factor is odd: 1
Factor is even: 2
factor is odd: 5
Factor is even: 10
factor is odd: 25
Factor is even: 50
Ans:
num=50
for i in range(1,num+1):
rem=num%i
if(rem==0):
if(i%2==0):
print("Factor is even:",i)
else:
print("factor is odd: ",i)
else:
pass
2. Write a code which accepts a sequence of words as input and prints the words
in a sequence after sorting them alphabetically.
Hint: In case of input data being supplied to the question, it should be
assumed to be a console input.
Input:
"Welcome to Python"
Output:
The sorted words are:
Python
Welcome
to
3. Write a program, which will find all the numbers between 1000 and 3000
(both included) such that each digit of a number is an even number. The
numbers obtained should be printed in a comma separated sequence on a
single line.
Output:
2000,2002,2004,2006,2008,2020,2022,2024,2026,2028,2040,2042,2044,2046,20
48,2060,2062,2064,2066,2068,2080,2082,2084,2086,2088,2200,2202,2204,2206,
2208,2220,2222,2224,2226,2228,2240,2242,2244,2246,2248,2260,2262,2264,22
66,2268,2280,2282,2284,2286,2288,2400,2402,2404,2406,2408,2420,2422,2424,
2426,2428,2440,2442,2444,2446,2448,2460,2462,2464,2466,2468,2480,2482,24
84,2486,2488,2600,2602,2604,2606,2608,2620,2622,2624,2626,2628,2640,2642,
2644,2646,2648,2660,2662,2664,2666,2668,2680,2682,2684,2686,2688,2800,28
02,2804,2806,2808,2820,2822,2824,2826,2828,2840,2842,2844,2846,2848,2860,
2862,2864,2866,2868,2880,2882,2884,2886,2888
Ans:
values = []
for i in range(1000,3001):
s = str(i)
if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0) and
(int(s[3])%2==0):
values.append(s)
print (",".join(values))
4. Write a program that accepts a sentence and calculate the number of letters
and digits.
Input:
Suppose if the entered string is: Python0325
Output:
Then the output will be:
LETTERS: 6
DIGITS:4
Hint: Use built-in functions of string.- isdigit() and isalpha()
Ans:
s = "aac34520"
d={"DIGITS":0, "LETTERS":0}
for c in s:
if c.isdigit():
d["DIGITS"]+=1
elif c.isalpha():
d["LETTERS"]+=1
else:
pass
print("LETTERS", d["LETTERS"])
print("DIGITS", d["DIGITS"])
5. Design a code which will find the given number is Palindrome number or not.
Example
alph = ["a", "b", "c", "d"]
ralph = reversed(alph)
for x in ralph:
print(x)
Input:
my_str = 'aIbohPhoBiA'
Output:
Ans:
It is palindrome
Option 2
my_str = 'aIbohPhoBiA'
my_str = my_str.casefold() # make it suitable for caseless comparison
if my_str == my_str[::-1]:
print("It is palindrome")
else:
print("It is not palindrome")