Python Code For Programming Questions
Python Code For Programming Questions
>> print(“Hello\nWorld”)
>> print(“Hello\tWorld”)
6. print a character
i/p: Enter a character: a
o/p: a
OPERATORS:
1. Write a python program to get 2 numbers from the user and calculate their sum and
difference using '+' and '-' operators respectively. Print the corresponding sum and difference
of the numbers as output in the console.
Input format:
Output format:
Sample Input:
55
34
Sample Output:
89
21
>> n1 = int(input())
>> n2 = int(input())
>> print(n1+n2)
>> print(n1-n2)
2. Write a python program to get 2 numbers from the user and calculate their product,
quotient and remainder using '*', '/' and '%' operators respectively. Print the corresponding
product, quotient and remainder of the numbers as output in the console.
Input format:
Output format:
Sample Input:
50
10
Sample Output:
500
>> n1 = int(input())
>> n2 = int(input())
>> print(n1*n2)
>> print(n1//n2)
>> print(n1%n2)
3. Write a python program to get 2 numbers from the user and swap their values without
any loss of data. You can make use of an additional 3rd variable for swapping. Print the
corresponding swapped values of the two numbers as output in the console.
Input format:
Output format:
Sample Input:
20
10
Sample Output:
10
20
>> n1 = int(input())
>> n2 = int(input())
>> t = n1
>> n1 = n2
>> n2 = t
>> print(n1)
>> print(n2)
4. Write a python program to get 2 numbers from the user and swap their values without
any use of 3rd variable. Print the corresponding swapped values of the two numbers as output
in the console.
Input format:
Output format:
Sample Input:
20
10
Sample Output:
10
20
>> n1 = int(input())
>> n2 = int(input())
>> print(n1)
>> print(n2)
5. A teacher wants to compute the average of 5 students in her class. Write a program to
help her to find the average. The average is the sum of all the numbers, then divided by the
total numbers.
Input format:
Sample Input:
10
20
30
40
50
Sample Output:
30.00
>> n1 = int(input())
>> n2 = int(input())
>> n3 = int(input())
>> n4 = int(input())
>> n5 = int(input())
>> print(“%.2f”%(total/5))
6. Alice was bored that day,so she was sitting on the riverbank .Suddenly she notices a
talking, White Rabbit with a pocket watch .It ran fast,and she followed it, down a rabbit hole
.She fell into the hole and found a magical wonderland with dark trees, beautiful flowers.She
found many ways numbered from 1,2,3,........18.she was confused which is the right way that
will lead her to her home. She found a cute bird, standing in one of the trees. Alice asked the
bird the way to go back to her home.The bird said a two digit number( say 23 ) and asked her
to find the sum of its digits (2+3=5) and that numbered way will lead her to her home.Alice
was already confused, so pls help Alice in finding the route to her home....
Input Format:
Refer sample input and output for formatting specifications. [All text in bold corresponds to
input and the rest corresponds to output]
SAMPLE INPUT :
23
SAMPLE OUTPUT:
>> n = int(input())
>> r = n%10
>> q = n // 10
>> print(r + q)
7. Sheela has three things in her bag. She wants to compute the area of 3 things but 3
things are in different shapes. The three things are in square shape, rectangular shape, and
circular shape respectively. Write a program to help Sheela to calculate the area of different
shapes.
Input format:
Output format:
The third output should be the area of a circle in float with 2 decimal places
Sample Input:
5
2.0
Sample Output:
25
20
12.56
>> s = int(input())
>> l = int(input())
>> b = int(input())
>> r = int(input())
>> print(s*s)
>> print(l*b)
>> print(r*r*3.14)
8. The college ground is rectangular in shape. The Management decides to build a fence
around the ground. In order to help the construction workers to build a straight fence, they
planned to place a thick rope around the ground. They wanted to buy only the exact length of
the rope that is needed. They also wanted to cover the entire ground with a thick carpet during
the rainy season. They wanted to buy only the exact quantity of carpet that is needed. They
requested your help. Can you please help them by writing a program to find the exact length of
the rope and the exact quantity of carpet that is required?
Input Format:
SAMPLE INPUT:
50
20
SAMPLE OUTPUT:
>> l = int(input())
>> b = int(input())
9. pow() function is used to calculate the power of any base and it is defined in the math
header file. Write a program to read X as the base and N as the power and calculate the result
(X^N - X to the power of N).
Input format:
Output format:
Sample Input:
3
Sample Output:
>> b = int(input())
>> p = int(input())
>> print(math.pow(b,p))
(OR)
>> b = int(input())
>> p = int(input())
>> print(b**p)
10. Sara wished to build a new house but she didn't have a sufficient amount. So, she is
planning to borrow some money from the bank on simple interest. When she is borrowing
some money from the bank, she has to pay back the original amount accompanied by a
certain amount of interest on that amount. She wants to find the interest for borrowed money
within a certain period. Help her to find a simple interest.
Input format:
The first line containing integer value denoting the borrowed amount(principal amount)
The second line containing integer value denoting the period in years
The third line containing float value denoting the rate of interest
Output format:
Sample Input:
15000
2.8
>> n = int(input())
>> r = float(input())
>> si = p*n*r/100
DECISION-MAKING STATEMENTS:
1. Get two integers a and b from the user and write a python program to relate 2 integers
as equal to, less than or greater than.
Input format:
Output format:
If the first number is equal to the second number, print "a is equal to b".
Otherwise, print "a greater than b" or "a less than b"
Sample Input:
17
12
Sample Output:
17 greater than 12
2. Write a python program to check whether the given character is a vowel or consonant
or Not an alphabet.
Input format:
Output format:
Sample Output:
Vowel
100 S
(90,100) A
(80,90) B
(70,80) C
(60,70) D
(50,60) E
<50 F
The interval [a,b) includes all numbers greater than or equal to a and less than b.
Input consists of a single integer which corresponds to the marks scored by the student. Print
Invalid Input if it is not in the range 0 to 100.
Sample Input 1:
85
Sample Output 1:
Sample Input 2:
850
Sample Output 2
Invalid Input
4. A fruit seller buys a dozen mangoes at Rs.X. He sells 1 mango at Rs.Y. Write a program
to determine the profit or loss in Rs. for the fruit seller.
Sample Input1:
60.0
Sample Output1:
Loss : Rs.12.00
Sample Input 2:
60.0
Sample Output 2:
Profit : Rs.12.00
Sample Input 3:
60.0
Sample Output 3:
5. Write a program to determine the fee amount to be collected from a student. The input
to the program are the type of the student, tuition fee, bus fee, hostel fee.
Input consists of a string (student type), tuition fee (float), bus fee (float) and hostel fee (float).
All floating point numbers are displayed correctly to 2 decimal places.
All text in bold corresponds to input and the rest corresponds to output.
MSH
40000
50000
Input Format:
The first line of the input is a string. The input may be “front” or “rear”.
Output Format:
Sample Input:
front
Sample Output:
Left Handed
7. A microwave oven manufacturer recommends that when heating two items, add 50%
to the heating time, and when heating three items double the heating time. Heating more than
three items at once is not recommended. Write a program that asks the user for the number of
items and the single-item heating time. The program then writes out the recommended
heating time.
Input Format:
The first input is an integer which corresponds to the number of items. The second integer is a
float which corresponds to the single item heating time.
Output Format:
Note: All text in bold corresponds to input and the rest corresponds to output
5.0
5.0
CONTROL STATEMENTS:
Input Format:
Output Format:
Sample Input 1
Sample Output 1
yes
Sample Input 2
12
Sample Output 2
no
2. Write a program to find the sum of digits in a given number. Program to find the sum of
digits of the given number is discussed here.
For example, let the input number be 719. The sum of digits of 719 = 7 + 1 + 9 = 17
Sample Input:
719
Sample Output:
17
3. Program to find if the given numbers are Friendly pair or not (Amicable or not) is discussed
here. Friendly Pair are two or more numbers with a common abundance.
Sample Input:
28
Sample Output:
Friendly Pair
9 -> 1,3,9
8 -> 1 , 8 / 2 , 4 (1,2,4)
4. Writing a program to find LCM of two numbers is discussed here. Two numbers are
obtained as input and the prime factors of both the numbers are found. The product of the
union of prime factors of both the numbers gives the LCM of the two numbers.
Sample Input:
30
Sample Output:
LCM of 5 and 30 is 30
Sample Input:
454
Sample Output:
Palindrome
Input format:
Output format:
Print the series and refer the sample output for formatting
Sample Input :
Sample Output:
1 4 9 16 25 36 49
Input format:
Output format:
Print the series and refer the sample output for formatting.
Sample Input:
Sample Output:
6 11 21 36 56
Sample Input 1:
5
Sample Output 1:
*****
*****
*****
*****
*****
Sample Input 2:
7
Sample Output 2:
*******
*******
*******
*******
*******
*******
*******
1. In this problem you must write a program that determines if two circles intersect each
other. The input to your program will be the <x, y> coordinates for the center of each
circle, followed by the radius(r) of each circle. The output will state whether the circles
overlap, do not overlap, or are tangential (i.e., tangential circles touch each other at just
one common point).
Input Format:
Input consists of 6 integers. The first integer corresponds to the x-coordinate of the
centre of the first circle. The second integer ccorresponds to the y-coordinate of the
centre of the first circle. The third integer corresponds to the radius of the first circle.
The next 3 integers correspond to the x,y and radius of the second circle.
Output Format:
The output consists of a single line which contains any of these 3 strings --- “The circles
are tangential”, “The circles overlap”, “The circles do not overlap”
Sample Input 1:
10
10
10
Sample Output 1:
Sample Output 2:
2. We are driving down the street and see a green traffic light ahead. Because we know
precisely the pattern of this traffic light, we know exactly how long we have before it will
turn red. We wish to compute whether we will pass the traffic light before it turns red at
our current speed.
Write a program that takes the following floating point numbers as input:
and displays a message indicating whether we will beat the light. You may assume that
the input won't be such that we reach the light at exactly the same time it turns red.
Input Format:
● The first line of the input consists of a floating point number which corresponds to
the speed,s.
● The second line of the input consists of a floating point number which
corresponds to the distance,d.
● The third line of the input consists of a floating point number which corresponds
to the time, t.
Output Format:
Sample Input 1:
59.99
1.0
60.0
Sample Output 1:
no
Sample Input 2:
60.01
60
Sample Output 2:
Yes
15 (1+2+3+4+5)
3. (b) Write program to reverse a number is discussed here. We can reverse a number
using loop and arithmetic operators in both iterative and recursive approaches.
Sample Input:
13579
Sample Output:
97531