Cognizant Coding Questions
Cognizant Coding Questions
Convert the same result to the U.S. style of miles per gallon and display the
result. If the quantity or distance is zero or negative display ” is an Invalid
Input”.
The result should be with two decimal place.To get two decimal place refer
the below-mentioned print statement :
float cost=670.23;
Sample Input 1:
Enter the no of liters to fill the tank
20
Enter the distance covered
150
Sample Output 1:
Liters/100KM
13.33
Miles/gallons
17.64
Explanation:
For 150 KM fuel consumption is 20 liters,
Then for 100 KM fuel consumption would be (20/150)*100=13.33,
Distance is given in KM, we have to convert it to miles
(150*0.6214)=93.21,
Fuel consumption is given in liters, we have to convert it to gallons
(20*0.2642)=5.284,
Then find (miles/gallons)=(93.21/5.284)=17.64
Sample Input 2:
Enter the no of liters to fill the tank
-5
Sample Output 2:
-5 is an Invalid Input
Sample Input 3:
Enter the no of liters to fill the tank
25
Enter the distance covered
-21
Sample Output 3:
-21 is an Invalid Input
import sys
ltt =int(input())
lt= (ltt*1.00)
if(ltt<1):
sys.exit()
diss =int(input())
dis= (diss*1.00)
if(diss<1):
sys.exit()
hundred = ((lt/dis)*100)
print("Liters/100KM")
print(round(hundred,2))
miles = (dis*0.6214);
gallons =(lt*0.2642);
mg = miles/gallons;
print("Miles/gallons")
print(round(mg,2))
Question-2
Sample Input 1:
Enter the no of pizzas bought:10
Enter the no of puffs bought:12
Enter the no of cool drinks bought:5
Sample Output 1:
Bill Details
No of pizzas:10
No of puffs:12
No of cooldrinks:5
Total price=1290
print("Bill details")
print("No of pizzas:{}".format(pizza_count))
print("No of puffs:{}".format(puffs_count))
print("No of cooldrinks:{}".format(drinks_count))
print("Total price={}".format(bill))
Question-3
Sample Input 1:
Enter the digits:
65
66
67
68
Sample Output 1:
65-A
66-B
67-C
68-D
Sample Input 2:
Enter the digits:
115
116
101
112
Sample Output 2:
115-s
116-t
101-e
112-p
a=int(input())
b=int(input())
c=int(input())
d=int(input())
print(str(a)+"-"+chr(a))
print(str(b)+"-"+chr(b))
print(str(c)+"-"+chr(c))
print(str(d)+"-"+chr(d))
Question-4
Note : If any input is negative, the output should be “Input is Invalid”. If all
department has equal number of placements, the output should be “None of
the department has got the highest placement”.
Sample Input 1:
Enter the no of students placed in CSE:90
Enter the no of students placed in ECE:45
Enter the no of students placed in MECH:70
Sample Output 1:
Highest placement
CSE
Sample Input 2:
Enter the no of students placed in CSE:55
Enter the no of students placed in ECE:85
Enter the no of students placed in MECH:85
Sample Output 2:
Highest placement
ECE
MECH
Sample Input 3:
Enter the no of students placed in CSE:0
Enter the no of students placed in ECE:0
Enter the no of students placed in MECH:0
Sample Output 3:
None of the department has got the highest placement
Sample Input 4:
Enter the no of students placed in CSE:10
Enter the no of students placed in ECE:-50
Enter the no of students placed in MECH:40
Sample Output 4:
Input is Invalid
import sys
print("Input is Invalid")
sys.exit()
sys.exit()
print("Highest placement")
m=max(cse,ece,mech)
if(cse==m):
print("CSE")
if(ece==m):
print("ECE")
if(mech==m):
print("MECH")
Question-5
Hint: k and q and You have to book minimum of 5 tickets and maximum
of 40 at a time. If fails display “Minimum of 5 and Maximum of 40
Tickets”. If circle is given a value other than ‘k’ or ‘q’ the output should
be “Invalid Input”.
Sample Input 1:
Enter the no of ticket:35
Do you want refreshment:y
Do you have coupon code:y
Enter the circle:k
Sample Output 1:
Ticket cost:4065.25
Sample Input 2:
Enter the no of ticket:1
Sample Output 2:
Minimum of 5 and Maximum of 40 Tickets
import sys
sys.exit(0)
if(circle== 'k'):
cost=75*noTicket
elif(circle== 'q'):
cost=150*noTicket
else:
print("Invalid Input")
sys.exit(0)
total=cost
if(noTicket>20):
total=cost
if(co== 'y'):
if(ref== 'y'):
total += (noTicket*50);
print("Ticket cost:{}".format(round(total,2)))
Question-6
Problem Statement – Rhea Pandey’s teacher has asked her to prepare well
for the lesson on seasons. When her teacher tells a month, she needs to say
the season corresponding to that month. Write a program to solve the above
task.
Spring – March to May,
Summer – June to August,
Autumn – September to November and,
Winter – December to February.
Month should be in the range 1 to 12. If not the output should be “Invalid
month”.
Sample Input 1:
Enter the month:11
Sample Output 1:
Season:Autumn
Sample Input 2:
Enter the month:13
Sample Output 2:
Invalid month
mon=int(input("Enter month"))
print("Season:Winter")
print("Season:Spring")
print("Season:Summer")
print("Season:Winter")
else:
print("Invalid month")
Question-7
Problem Statement – To speed up his composition of generating
unpredictable rhythms, Blue Bandit wants the list of prime numbers available
in a range of numbers.Can you help him out?
Write a java program to print all prime numbers in the interval [a,b] (a and b,
both inclusive).
Note
Input 1 should be lesser than Input 2. Both the inputs should be
positive.
Range must always be greater than zero.
If any of the condition mentioned above fails, then display “Provide valid
input”
Use a minimum of one for loop and one while loop
Sample Input 1:
15
Sample Output 1:
2 3 5 7 11 13
Sample Input 2:
Sample Output 2:
Provide valid input
a=int(input())
b=int(input())
else:
while(a<=b):
if(a==2):
print(a,end=" ");
elif(a==1):
a+=1
continue
else:
flag=0
for i in range(2,a//2+1):
if(a%i==0):
flag=1
break
if flag==0:
print(a,end=" ")
a+=1
Question-8
Sample Input 1 :
21212
Sample Output 1 :
Palindrome
Sample Input 2 :
6186
Sample Output 2 :
Not a Palindrome
n=input()
if(int(n)<0):
print("Invalid Input")
elif(n==n[::-1]):
print("Palindrome")
else:
print("Not a Palindrome")
Question-9
Note : If either the salary is 0 or negative (or) if the appraisal rating is not in
the range 1 to 5 (inclusive), then the output should be “Invalid Input”.
Sample Input 1 :
Enter the salary
8000
Enter the Performance appraisal rating
Sample Output 1 :
8800
Sample Input 2 :
Enter the salary
7500
Enter the Performance appraisal rating
4.3
Sample Output 2 :
9750
Sample Input 3 :
Enter the salary
-5000
Enter the Performance appraisal rating
Sample Output 3 :
Invalid Input
Question-10
Note : The input other than 4 digit positive number[includes negative and 0] is
considered as invalid.
Refer the samples, to read and display the data.
Sample Input 1:
Enter the car no:1234
Sample Output 1:
Lucky Number
Sample Input 2:
Enter the car no:1214
Sample Output 2:
Sorry its not my lucky number
Sample Input 3:
Enter the car no:14
Sample Output 3:
14 is not a valid car number
Question-11
Problem Statement –
Sample Input 1:
Enter no of course:
5
Enter course names:
Java
Oracle
C++
Mysql
Dotnet
Enter the course to be searched:
C++
Sample Output 1:
Sample Input 2:
Enter no of course:
3
Enter course names:
Java
Oracle
Dotnet
Enter the course to be searched:
C++
Sample Output 2:
Sample Input 3:
Enter no of course:
Sample Output 3:
Invalid Range
import sys
n = int(input("Enter no of course"))
if not (n >0 or n <20) :
print("Invalid Range")
exit()
course=[]
print("Enter the course names")
for i in range(n):
course.append(input())
search = input("Enter the course to be searched")
if search in course:
print(search ,"course is available ")
else:
print(search,"course is not available")
Question-12
Problem Statement – Mayuri buys “N” no of products from a shop. The shop
offers a different percentage of discount on each item. She wants to know the
item that has the minimum discount offer, so that she can avoid buying that
and save money.
[Input Format: The first input refers to the no of items; the second input is the
item name, price and discount percentage separated by comma(,)]
Assume the minimum discount offer is in the form of Integer.
Note: There can be more than one product with a minimum discount.
Sample Input 1:
mobile,10000,20
shoe,5000,10
watch,6000,15
laptop,35000,5
Sample Output 1:
shoe
Explanation: The discount on the mobile is 2000, the discount on the shoe is
500, the discount on the watch is 900 and the discount on the laptop is 1750.
So the discount on the shoe is the minimum.
Sample Input 2:
Mobile,5000,10
shoe,5000,10
WATCH,5000,10
Laptop,5000,10
Sample Output 2:
Mobile
shoe
WATCH
Laptop
Question-13
Problem Statement – Raj wants to know the maximum marks scored by him
in each semester. The mark should be between 0 to 100 ,if goes beyond the
range display “You have entered invalid mark.”
Sample Input 1:
Enter no of semester:
3
Enter no of subjects in 1 semester:
3
Enter no of subjects in 2 semester:
4
Enter no of subjects in 3 semester:
2
Marks obtained in semester 1:
50
60
70
Marks obtained in semester 2:
90
98
76
67
Marks obtained in semester 3:
89
76
Sample Output 1:
Maximum mark in 1 semester:70
Maximum mark in 2 semester:98
Maximum mark in 3 semester:89
Sample Input 2:
Enter no of semester:
3
Enter no of subjects in 1 semester:
3
Enter no of subjects in 2 semester:
4
Enter no of subjects in 3 semester:
2
Marks obtained in semester 1:
55
67
98
Marks obtained in semester 2:
67
-98
Sample Output 2:
Question-14
Sample Input 1 :
54
Sample Output 1 :
1, 2, 3, 6, 9, 18, 27, 54
Sample Input 2 :
-1869
Sample Output 2 :
x = abs(int(input()))
if x == 0:
print("No Factors")
exit()
for i in range(1, x + 1):
if x % i == 0:
if i != x:
print(i,end=", ")
else:
print(i)