0% found this document useful (0 votes)
13 views

Python Code For Programming Questions

Programing PDFs

Uploaded by

aicreative628
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Python Code For Programming Questions

Programing PDFs

Uploaded by

aicreative628
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

I/O STATEMENTS:

Write a program to print,


1. Hello World
o/p: Hello World

>> print(“Hello World”)

2. Hello World in two lines


o/p: Hello
World

>> print(“Hello\nWorld”)

3. Hello World with tab space


o/p:Hello World

>> print(“Hello\tWorld”)

Write a program to get input from the user and


4. print an integer
i/p: Enter a number: 5
o/p: 5

>>n = int(input(“Enter a number:”))


>> print(n)

5. print a floating value


i/p: Enter a number: 5.25
o/p: 5.25

>>n = float(input(“Enter a number:”))


>> print(n)

6. print a character
i/p: Enter a character: a
o/p: a

>>ch = input(“Enter a character:”)


>> print(ch)

7. print the following


i/p:Enter the details:
Name: Priyanka
Department: Data Science
Cgpa: 9.0
o/p:Name: Priyanka
Department: Data Science
Cgpa: 9.0
>> print(“Enter the details:”)
>> name = input(“Name:”)
>> dept = input(“Department:”)
>> cgpa = float(input(“Cgpa:”)
>> print(“Name:”,name)
>> print(“Department:”,dept)
>> print(“Cgpa:”,cgpa)

8. Write a program to print the following output


i/p: Enter a number: 12.345
o/p: 13
12

>> import math


>> n = float(input(“Enter a number:”))
>> print(math.ceil(n))
>> print(math.floor(n))

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:

First input: an integer

Second input: an integer

Output format:

First output will be the sum of two integers

Second output will be the difference of two integers

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:

First input: an integer

Second input: an integer

Output format:

The first output will be the product of two integers

The second output will be the quotient

The third output will be the remainder

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:

First input: an integer

Second input: an integer

Output format:

The output will be integers(swapped values)

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:

First input: an integer

Second input: an integer

Output format:

The output will be integers(swapped values)

Sample Input:

20

10

Sample Output:

10

20

>> n1 = int(input())

>> n2 = int(input())

>> n1,n2 = n2,n1

>> 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:

First input: 1st student mark in float

Second input: 2nd student mark in float

Third input: 3rd student mark in float

Fourth input:4th student mark in float

Fifth input: 5th student mark in float


Output format:

The output value should be in a float with 2 decimal places.

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())

>> total = n1+n2+n3+n4+n5

>> 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:

Input consists of an integer corresponding to the 2-digit number.


Output Format:

Output consists of an integer corresponding to the sum of its digits.

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:

First input: a side of a square in integer

Second input: length of a rectangle in integer

Third input: breadth of a rectangle in integer

Fourth input: radius of a circle in float

Output format:

The first output should be the area of a square in integer

The second output should be the area of a rectangle in integer

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:

Input consists of 2 integers.

The first integer corresponds to the length of the ground

The second integer corresponds to the breadth of the ground.


Output Format:

First output corresponds to the length of the rope.

Second output corresponds to the quantity of the carpet.

SAMPLE INPUT:

50

20

SAMPLE OUTPUT:

Required length is 140 m

Required quantity of carpet is 1000 sqm

>> l = int(input())

>> b = int(input())

>> print(“Required length is”, (2*(l+b))+”m”)

>> print(“Required quantity of carpet is”,(l*b)+”sqm”)

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:

The first line containing integer denotes the base(X)

The second line containing integer denotes the power(N)

Output format:

Print the power of a number

Sample Input:

3
Sample Output:

>> import math

>> 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:

Print the simple interest with 2 decimal places.

Sample Input:

15000

2.8

Sample Output: 840.00


>> p = int(input())

>> n = int(input())

>> r = float(input())

>> si = p*n*r/100

>> print(“%.2f” %si)

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:

Input consist of 2 integers

The first input corresponds to the first number(a)

The second input corresponds to the second number(b)

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:

The input consist of a character

Output format:

The output consists of a below-given string “Vowel” / “Consonant” / “Not an alphabet”


Sample Input:

Sample Output:

Vowel

3. The newly appointed Vice Chancellor of Anna University wanted to create an


automated grading system for the students to check their grade. When a student enters a
mark, the grading system displays the corresponding grade.

Write a program to solve the given problem.

Marks scored Grade

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 and Output Format:

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.

Refer sample input and output for formatting specifications.

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.

Input and Output Format:

Input consists of 2 floating point numbers which correspond to X and Y.

Refer sample input and output for formatting specifications. .

Sample Input1:

60.0

Sample Output1:

Enter the price of a dozen mangoes

Enter the price at which 1 mango is being sold

Loss : Rs.12.00

Sample Input 2:

60.0

Sample Output 2:

Enter the price of a dozen mangoes

Enter the price at which 1 mango is being sold

Profit : Rs.12.00

Sample Input 3:

60.0

Sample Output 3:

Enter the price of a dozen mangoes


Enter the price at which 1 mango is being sold

No profit nor loss

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.

Refer to the table below for fee details.

Student Type Student Type denoted as Fee Details

Merit Seat Day Scholar MSDS Tuition fee + Bus fee

Merit Seat Hosteller MSH Tuition fee + Hostel


fee

Management Seat Day Scholar MGSDS 150% of Tuition fee +


Bus fee

Management Seat Hosteller MGSH 150% of Tuition fee +


Hostel fee
Input and Output Format:

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.

Refer sample input and output for formatting specifications.

All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output:

Enter the student type

MSH

Enter tuition fee

40000

Enter hostel fee

50000

The fees to be paid by the student is Rs.90000.00


6. The catering staff in the SECE mess are known for their good cooking skills as well as
hospitality. We all know that the dining table arrangement needs to be different for left handed
and right handed persons. So whenever any VIP guests come to SECE, they would make the
table arrangements based on whether they are left handed or right handed. The mess is
situated on the 15th floor of the hostel building. The SECE hostel building has superfast
elevators to help you travel from one floor to another. Each elevator has 2 doors, the front one
and the rear one. If a person enters the elevator through the front door, he goes out through
the rear door and vice-versa. The elevator has 2 rails numbered as 1 and 2. Rail 1 is located to
the left of the entrance to the front door (or correspondingly, to the right of the entrance to the
rear door). Rail 2 is located opposite it, to the right of the entrance to the front door and to the
left of the entrance to the rear door. We know that each person holds at the rail with his/her
strongest hand. There is an IP camera in the elevator and based on the camera output, the
catering staff will be easily able to identify whether a guest is left-handed or right-handed.
They have decided to automate this task and they asked the help of the Image Processing
Group. The Image Processing Group has written a program to perform this task and the
program will output the door through which the person entered and the rail number which the
person held. Based on this input, can you write a program to determine whether a person is
left handed or right handed?

Input Format:

The first line of the input is a string. The input may be “front” or “rear”.

The second line of the input is an integer. The input may be 1 or 2.

Output Format:

Output consists of the string “Left Handed” or “Right Handed”.

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:

Refer sample input and output for further formatting specifications.

Note: All text in bold corresponds to input and the rest corresponds to output

Sample Input and Output 1:

Enter the number of items

Enter the single item heating time

5.0

The recommended heating time is 7.50

Sample Input and Output 2:

Enter the number of items

Enter the single item heating time

5.0

Number of items is more

CONTROL STATEMENTS:

1. Write a program to determine whether n is a factorial number or not. A factorial number is a


number that is a factorial of another number.

Input Format:

Input consists of a single integer which corresponds to n.

Output Format:

Output consists of a string - “yes” or “no”

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

Input & Output Format:

Input consists of one integer.

Output consists of a sum of digits.

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.

Input & Output format:

● Input consists of 2 integers.


● The first integer corresponds to number 1 and the second integer corresponds to
number 2.
● If it is a Friendly Pair display Friendly Pair or displays Not Friendly Pair.

For example,6 and 28 are Friendly Pair.

(Sum of divisors of 6)/6 = (Sum of divisors of 28)/28.

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.

Input & Output Format:

Input consists of two integers.

Output consists of the lcm of two numbers.

Sample Input:

30

Sample Output:

LCM of 5 and 30 is 30

5. Program to check whether the given number is a Palindrome or Not a Palindrome is


discussed here. Any number is said to be a palindrome if the original number and the reverse
of the original number are the same.

For example, 1234321 is a palindrome.

Original number = 1234321

The reverse of the number = 1234321

Sample Input:

454

Sample Output:

Palindrome

6. Write a program to generate the following series --- 1,4,9,16,25, ....

Input format:

The input containing an integer which denotes 'n'

Output format:
Print the series and refer the sample output for formatting

Sample Input :

Sample Output:

1 4 9 16 25 36 49

7. Write a program to generate the following series --- 6,11,21,36,56,...

Input format:

The input containing an integer which denotes 'n'

Output format:

Print the series and refer the sample output for formatting.

Sample Input:

Sample Output:

6 11 21 36 56

8. Write a Python Program to solve the pattern in dynamic.

Sample Input 1:
5
Sample Output 1:
*****

*****

*****

*****

*****

Sample Input 2:
7
Sample Output 2:
*******
*******
*******
*******
*******
*******
*******

DATE: 28.08.2024 DURATION: 45 MINUTES

III B.TECH - AIML/CSE MARKS: 15

I INTERNAL ASSESMENT - AUG 2024

PLEASE BE NOTED THAT ALL PROGRAMS ARE MANDATORY

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:

The circles are tangential


Sample Input 2:

Sample Output 2:

The circles overlap

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:

● our current speed in meter per seconds (s)


● the distance to the light in meters (d)
● the time until it turns red in seconds (t)

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:

Input consists of 3 lines.

● 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:

Output consists of a string ("yes" or "no").

Output yes if we will be able to beat the light at current speed.


Output no if we won't be able to beat light at current speed.

Sample Input 1:

59.99

1.0

60.0

Sample Output 1:

no

Sample Input 2:

60.01

60

Sample Output 2:

Yes

3. (a) Write a program to find the sum of first N natural numbers.

Input & Output Format:

Input consists of one integer.

Output consists of a integer.

Sample Input & Output:

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.

Input & Output Format:

Input consists of a integer.

Output consists of reversed input.

Sample Input:
13579

Sample Output:

97531

You might also like