Assignment - Python Programs

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Program 1: Marks:5

Write a program to perform 1+2+3…+n in the given input and output format

n=(int(input("Enter the value of n:")))


s=0
for i in range(1,n+1):
s=s+i
print(s)
Sample 1:
Input
Enter the value of n
5
Output
15

Sample 2:

Input
Enter the value of n
3
Output
6

----------------------------------------------------------------------------------------------------------------------------- ----------
Program 2: Marks:5
----------------------------------------------------------------------------------------------------------------

Given a number n, you have to print the factorial of this number. To know about factorial

Input Format:
A number n.
Output Format:
Print the factorial of n.
Example:
Input:
4
Output:
24
Sample Test Cases
Input Output
Test Case 1 5 120
Test Case 2 4 24
Test Case 3 6 720
n=(int(input("Enter:")))
s=1
for i in range(1,n+1):
s=s*i
print("The factorial is",s)
----------------------------------------------------------------------------------------------------------------------------- ---------
Program 3: Marks:5
--------------------------------------------------------------------------------------------------------------------------
Write a program to find triangle is right angle or not

Program:

a=(int(input("First side:")))
b=(int(input("Second side:")))
c=(int(input("Third side:")))
if ((a*a==b*b+c*c) or (b*b==a*a+c*c) or (c*c==b*b+a*a)):
print("It is right angle triangle")
else:
print("Not right angle triangle")
Input Format:
First two input integers are the length smallest sides of triangle.
Next value consist of length longest sides of triangle.
[Assume length of sides of triangle will be within 100]

Output Format:
print whether the triangle is right angled or not.
[All text in bold refers to input Values]
Sample Input and Output 1:
Enter the length two smallest sides of triangle
3
4
Enter the length of longest side of triangle
5
Triangle is Right Angled Triangle

Sample Input and Output 2:


Enter the length two smallest sides of triangle
3
5
Enter the length of longest side of triangle
6
Triangle is not Right Angled Triangle

---------------------------------------------------------------------------------------------------------------------------
Program 4: Marks:5
-------------------------------------------------------------------------------------------------------------------------
cp=(int(input("cost price:")))
sp=(int(input("selling price:")))
if(sp>cp):
p=sp-cp
pp=(100*(p/cp))
print("Profit=",pp)
elif(sp <cp):
l=cp-sp
lp=(100*(l/cp))
print("Loss=",lp)
else:
print("No Loss , No Gain")
Input format:
First line of input is an integer, that corresponds to the cost price of the product.
Second line of input is an integer, that corresponds to the selling price of the product.
Output format:
The output consists, "profit" or "loss" along with the percentage of it.
Refer the sample input and output for better understanding.
Sample input and Output 1:
Enter the cost price
4
Enter the selling price
5
Profit 25.0

Sample input and Output 2:


Enter the cost price
5
Enter the selling price
4
Loss 20.0

Sample input and Output 3:


Enter the cost price
5
Enter the selling price
5
No Profit No Loss
----------------------------------------------------------------------------------------------------------------
Program 5: Marks:5
----------------------------------------------------------------------------------------------------------------
Write the program to help the airport staff to make a bill for extra luggage weight .

w=(int(input("Enter Weight:")))
aw=abs(15-w)
if(aw <0 and aw >=30):
rs=aw*50
print(“Need to pay extra amount of Rs.”,rs)
elif(aw >30 and aw <=60):
rs=aw*150
print(“Need to pay extra amount of Rs.”,rs)
elif(aw >60):
rs=aw*250
print(“Need to pay extra amount of Rs.”,rs)
elif(aw <15):
print("No need to pay - Invalid input")
Input Format:
Input consists of single integer value for luggage weight .

Output Format:
Output consists of integer value of the extra amount that customer need to pay for the
luggages exceeding minimum weight .
Print "Invalid Input" if the weight is less than or equal to 0.
Assumptions:
The minimum weight of the luggage is assumed to be 15 Kg .
W is extra weight in addition to 15Kg .
If the value of W is between the range (0 < W ≤ 30), need to pay Rs.50 per Kg .
If it is between the range (30 <W ≤ 60), need to pay Rs.150 per Kg .
If value of W is more then 60 , need to pay Rs.250 per Kg .

------------------------------------------------------------------------------------------------------------------------
Program 6: Marks:5
-------------------------------------------------------------------------------------------------------------------------
The first line of input is a integer corresponding to the number of students in the class.
The second line of input is a integer corresponding to the number of rows in the
auditorium.
The third line of input is a integer corresponding to the number of students that can sit in
one row.

ns=(int(input("Enter no. of students:")))


ra=(int(input("Enter no. of rows:")))
rs=(int(input("Enter no. of students per row:")))
t=ra*rs
to=abs(t-ns)
if(to < ns):
print(to," has to attend lecturer standing")
Output format:
The output is of a String either "Everyone can sit" or "1 has to attend lecture standing"
corresponding to tie seats available.
Sample input:
Enter the number of students in the class
50
Enter number of rows
7
Enter the number of students can sit in each row
7

Sample output:
1 has to attend lecture standing
---------------------------------------------------------------------------------------------------------------------
Program 7 Marks:5
Write a program to meet following input and output

n=(int(input("Enter numerator:")))
d=(int(input("Enter denominator:")))
if(n%d==0):
print("Yes")
else:
print("No")
Input format:
Input contains two integers.
First Integer is the one to be checked if its odd.
Second integer is the deviser of first integer.
Output format:
The output contains a yes or no.

Sample input:
5
3
Sample output:
No
----------------------------------------------------------------------------------------------------------------------
Program 8 Marks:5

There were three government officials who were in the same designation. After years they were
equally qualified for promotion. So that the government decided that the person whose elder
gets the promotion. Since he’ll be retiring first and so that everyone will get a chance to work in
the higher designation before their retirement. So help them by writing a python program in
which based on age input-output should be who has to be promoted.

A=(int(input("Enter First age:")))


B=(int(input("Enter Second age::")))
C=(int(input("Enter Third age::")))
if(A > B and A > C):
print("A should be promoted First")
elif( B >C and B > A):
print("B should be promoted First")
else:
print("C should be promoted First")
Input format:
Input consists of three integers A,B & C which is the age of there candidates and should
be distinct.
Output format:
The output will contain a string saying who should be promoted first.
Sample input:
65
45
55
Sample output:
A should be promoted first

You might also like