Python Tutorial: If Related Program

Saturday, 4 February 2023

If Related Program

Question 2

Write a program to input the cost price and the selling price of an article. If the selling price is more than the cost price then calculate and display actual profit and profit per cent otherwise, calculate and display actual loss and loss per cent. If the cost price and the selling price are equal, the program displays the message 'Neither profit nor loss'.

cp =float(input("Enter cost price of the article: "))
sp =float(input("Enter selling price of the article: "))
pl = sp - cp
percent = abs(pl) / cp * 100
if (pl > 0):
    print("Profit = " , pl)
    print("Profit % = " , percent)
elif(pl < 0):
    print("Loss = " , abs(pl))
    print("Loss % = " , percent)
else:
    print("Neither profit nor loss")
Output

Enter cost price of the article: 80

Enter selling price of the article: 60

Loss =  20.0

Loss % =  25.0

Enter cost price of the article: 70

Enter selling price of the article: 100

Profit =  30.0

Profit % =  42.85714285714284

Question 4

Write a program to accept a number and check whether the number is divisible by 3 as well as 5. Otherwise, decide:
(a) Is the number divisible by 3 and not by 5?
(b) Is the number divisible by 5 and not by 3?
(c) Is the number neither divisible by 3 nor by 5?
The program displays the message accordingly.

num=int(input("Enter number: "))

if (num % 3 == 0 and num % 5 == 0):

    print("Divisible by 3 and 5")

elif (num % 3 == 0):

    print("Divisible by 3 but not by 5")

elif (num % 5 == 0):

    print("Divisible by 5 but not by 3")

else:

    print("Neither divisible by 3 nor by 5")

Output

============================ RESTART: E:/Python/Q4.py ============================

Enter number: 45

Divisible by 3 and 5

============================ RESTART: E:/Python/Q4.py ============================

Enter number: 21

Divisible by 3 but not by 5

============================ RESTART: E:/Python/Q4.py ============================

Enter number: 23

Neither divisible by 3 nor by 5

Question 5

Write a program to input year and check whether it is:
(a) a Leap year (b) a Century Leap year (c) a Century year but not a Leap year
Sample Input: 2000
Sample Output: It is a Century Leap Year.

yr=int(input("Enter the year to check: "))
if (yr % 4 == 0 and yr % 100 != 0):
    print("It is a Leap Year")
elif (yr % 400 == 0):
    print("It is a Century Leap Year")
elif (yr % 100 == 0):
    print("It is a Century Year but not a Leap Year")
else:
    print("It is neither a Century Year nor a Leap Year")
Output

============================ RESTART: E:/Python/APC Java/Q5.py ============================

Enter the year to check: 2000

It is a Century Leap Year

============================ RESTART: E:/Python/APC Java/Q5.py ============================

Enter the year to check: 2019

It is neither a Century Year nor a Leap Year

Question 6

Write a program to input two unequal positive numbers and check whether they are perfect square numbers or not. If the user enters a negative number then the program displays the message 'Square root of a negative number can't be determined'.
Sample Input: 81, 100
Sample Output: They are perfect square numbers.
Sample Input: 225, 99
Sample Output: 225 is a perfect square number.
                          99 is not a perfect square number.

import math
a=int(input("Enter first number: "))
b=int(input("Enter second number: "))
if (a < 0 or b < 0) :
    print("Square root of a negative number can't be determined")
else:
    sqrtA = math.sqrt(a)
    sqrtB = math.sqrt(b)
    isAPerfectSq = sqrtA - math.floor(sqrtA)
    isBPerfectSq = sqrtB - math.floor(sqrtB)
    if (isAPerfectSq == 0 and isBPerfectSq == 0):
        print("They are perfect square numbers.")
    elif (isAPerfectSq == 0) :
        print(a , " is a perfect square number.")
        print(b , " is not a perfect square number.")
    elif (isBPerfectSq == 0):
        print(a , " is not a perfect square number.")
        print(b , " is a perfect square number.")
    else:
        print("Both are not perfect square numbers.")Output

Output

============================ RESTART: E:/Python/APC Java/Q6.py ============================

Enter first number: 81

Enter second number: 100

They are perfect square numbers.

============================ RESTART: E:/Python /Q6.py ============================

Enter first number: 225

Enter second number: 99

225  is a perfect square number.

99  is not a perfect square number.

 

Question 7

Without using if-else statement and ternary operators, accept three unequal numbers and display the second smallest number.
[Hint: Use Math.max( ) and Math.min( )]
Sample Input: 34, 82, 61
Sample Output: 61

print("Enter 3 unequal numbers")
a=int(input("Enter first number: "))
b=int(input("Enter second number: "))
c=int(input("Enter third number: "))
sum = a + b + c;
big = max(a, b);
big = max(big, c);
small = min(a, b);
small = min(small, c);
result = sum - big - small;
print("Second Smallest Number = " ,result)
Output

Enter 3 unequal numbers

Enter first number: 56

Enter second number: 23

Enter third number: 45

Second Smallest Number =  45

Question 8

Write a program to input three unequal numbers. Display the greatest and the smallest number.
Sample Input: 28, 98, 56
Sample Output: Greatest Number: 98
                           Smallest Number: 28

a=int(input("Enter first number: "))
b=int(input("Enter second number: "))
c=int(input("Enter third number: "))
min = a; max = a
if b < min:
    min= b
if c < min:
    min=c
if b> max:
    max=b
if c> max:
    max=c
print("Greatest Number: " , max)
print("Smallest Number: " , min)
Output

Enter first number: 43

Enter second number: 78

Enter third number: 23

Greatest Number:  78

Smallest Number:  23

Question 9

A Pre-Paid taxi charges from the passenger as per the tariff given below:

Distance

Rate

Up to 5 km

₹ 100

For the next 10 km

₹ 10/km

For the next 10 km

₹ 8/km

More than 25 km

₹ 5/km

Write a program to input the distance covered and calculate the amount paid by the passenger. The program displays the printed bill with the details given below:
Taxi No.                  :
Distance covered   :
Amount                  :

taxiNo=input("Enter Taxi Number: ")
dist=int(input("Enter distance travelled: "))
fare  = 0
if (dist <= 5):
    fare = 100
elif (dist <= 15):
    fare = 100 + (dist - 5) * 10
elif (dist <= 25):
    fare = 100 + 100 + (dist - 15) * 8
else:
    fare = 100 + 100 + 80 + (dist - 25) * 5
print("Taxi No: " , taxiNo)
print("Distance covered: " , dist)
print("Amount: " , fare)
Output

Enter Taxi Number: UP717321

Enter distance travelled: 23

Taxi No:  UP717321

Distance covered:  23

Amount:  264

Question 10

A cloth showroom has announced festival discounts and the gifts on the purchase of items, based on the total cost as given below:

Total Cost

Discount

Gift

Up to ₹ 2,000

5%

Calculator

₹ 2,001 to ₹ 5,000

10%

School Bag

₹ 5,001 to ₹ 10,000

15%

Wall Clock

Above ₹ 10,000

20%

Wrist Watch

Write a program to input the total cost. Compute and display the amount to be paid by the customer along with the gift.

cost=float(input("Enter total cost: "))

gift=0;amt=0

if(cost <= 2000.0):

    amt = cost - (cost * 5 / 100)

    gift = "Calculator"

elif (cost <= 5000.0):

    amt = cost - (cost * 10 / 100)

    gift = "School Bag"

elif (cost <= 10000.0):

    amt = cost - (cost * 15 / 100)

    gift = "Wall Clock"

else:

    amt = cost - (cost * 20 / 100)

    gift = "Wrist Watch"

print("Amount to be paid: ", amt)

print("Gift: " , gift)

Output

Enter total cost: 8000

Amount to be paid:  6800.0

Gift:  Wall Clock

Question 11

Given below is a hypothetical table showing rate of income tax for an India citizen, who is below or up to 60 years.

Taxable income (TI) in ₹

Income Tax in ₹

Up to ₹ 2,50,000

Nil

More than ₹ 2,50,000 and less than or equal to ₹ 5,00,000

(TI - 1,60,000) * 10%

More than ₹ 5,00,000 and less than or equal to ₹ 10,00,000

(TI - 5,00,000) * 20% + 34,000

More than ₹ 10,00,000

(TI - 10,00,000) * 30% + 94,000

Write a program to input the name, age and taxable income of a person. If the age is more than 60 years then display the message "Wrong Category". If the age is less than or equal to 60 years then compute and display the income tax payable along with the name of tax payer, as per the table given above.

name=input("Enter Name: ")
age=int(input("Enter age: "))
ti=float(input("Enter taxable income: "))
if (age > 60):
    print("Wrong Category")
else:
    if (ti <= 250000):
        tax = 0
    elif (ti <= 500000):
        tax = (ti - 160000) * 10 / 100
    elif (ti <= 1000000):
        tax = 34000 + ((ti - 500000) * 20 / 100)
    else:
        tax = 94000 + ((ti - 1000000) * 30 / 100)
print("Name: " , name)
print("Tax Payable: " , tax)
Output

====================== RESTART: E:/Python/APC Java/Q11.py ======================

Enter Name: Narain Ji Srivastava

Enter age: 43

Enter taxable income: 120000

Name:  Narain Ji Srivastava

Tax Payable:  0

====================== RESTART: E:/Python/APC Java/Q11.py ======================

Enter Name: Narain Ji Srivastava

Enter age: 43

Enter taxable income: 3000000

Name:  Narain Ji Srivastava

Tax Payable:  694000.0

Question 12

An employee wants to deposit certain sum of money under 'Term Deposit' scheme in Syndicate Bank. The bank has provided the tariff of the scheme, which is given below:

No. of Days

Rate of Interest

Up to 180 days

5.5%

181 to 364 days

7.5%

Exact 365 days

9.0%

More than 365 days

8.5%

Write a program to calculate the maturity amount taking the sum and number of days as inputs.

s=float(input("Enter s of money: "))
days=int(input("Enter number of days: "))
if (days <= 180):
    interest = s * 5.5 / 100.0
elif (days <= 364):
    interest = s * 7.5 / 100.0
elif (days == 365):
    interest = s * 9.0 / 100.0
else:
    interest = s * 8.5 / 100.0
amt = s + interest
print("Maturity Amount = " , amt)
Output

Enter s of money: 100000

Enter number of days: 366

Maturity Amount =  108500.0

Question 13

Mr. Kumar is an LIC agent. He offers discount to his policy holders on the annual premium. However, he also gets commission on the sum assured as per the given tariff.

Sum Assured

Discount

Commission

Up to ₹ 1,00,000

5%

2%

₹ 1,00,001 and up to ₹ 2,00,000

8%

3%

₹ 2,00,001 and up to ₹ 5,00,000

10%

5%

More than ₹ 5,00,000

15%

7.5%

Write a program to input name of the policy holder, the sum assured and first annual premium. Calculate the discount of the policy holder and the commission of the agent. The program displays all the details as:
Name of the policy holder :
Sum assured :
Premium :
Discount on the first premium :
Commission of the agent :

name=input("Enter Name: ")
s=int(input("Enter sum Assured: "))
pre=int(input("Enter First Premium: "))
if(s <= 100000):
    disc = pre * 5 / 100
    comm = s * 2 / 100
elif(s <= 200000):
    disc = pre * 8 / 100
    comm = s * 3 / 100
elif(s <= 500000):
    disc = pre * 10.0 / 100
    comm = s * 5 / 100
else:
    disc = pre * 15 / 100
    comm = s * 7.5 / 100
 
print("Name of the policy holder: " , name)
print("sum assured: " , s)
print("Premium: " , pre)
print("Discount on the first premium: " , disc)
print("Commission of the agent: " , comm)
Output

====================== RESTART: E:/Python/APC Java/Q13.py ======================

Enter Name: Narain Ji Srivastava

Enter sum Assured: 400000

Enter First Premium: 5000

Name of the policy holder:  Narain Ji Srivastava

sum assured:  400000

Premium:  5000

Discount on the first premium:  500.0

Commission of the agent:  20000.0

Question 14

A company announces revised Dearness Allowance (DA) and Special Allowances (SA) for their employees as per the tariff given below:

Basic

Dearness Allowance (DA)

Special Allowance (SA)

Up to ₹ 10,000

10%

5%

₹ 10,001 - ₹ 20,000

12%

8%

₹ 20,001 - ₹ 30,000

15%

10%

₹ 30,001 and above

20%

12%

Write a program to accept name and Basic Salary (BS) of an employee. Calculate and display gross salary.
Gross Salary = Basic + Dearness Allowance + Special Allowance
Print the information in the given format:
Name    Basic    DA    Spl. Allowance    Gross Salary
  xxx        xxx      xxx            xxx                      xxx

name=input("Enter name: ")
bs=float(input("Enter basic salary: "))
if (bs <= 10000):
    da = bs*10/100
    sa = bs*5/100
elif (bs <= 20000):
    da = bs*12/100
    sa = bs*8/100
elif (bs <= 30000):
    da = bs*15/100
    sa = bs*10/100
else:
    da = bs*20/100
    sa = bs*12/100
gs=bs+da+sa
print("Name\tBasic\tDA\tSpl. Allowance\tGross Salary")
print(name , "\t" , bs , "\t" , da , "\t" , sa , "\t",  gs)
Output

============================ RESTART: E:/Python/APC Java/Q14.py ===========================

Enter name: Aviral

Enter basic salary: 70000

Name    Basic      DA          Spl. Allowance   Gross Salary

Aviral      70000.0                 14000.0                 8400.0  92400.0

Question 15

Using a if statement, write a menu driven program to convert a given temperature from Fahrenheit to Celsius and vice-versa. For an incorrect choice, an appropriate message should be displayed.
Hint:
c = 5/9*(f-32) and f=1.8*c+32

Output

print("Type 1 to convert from Fahrenheit to Celsius")

print("Type 2 to convert from Celsius to Fahrenheit ", end="")

choice=int(input())

if choice==1:

    ft=float(input("Enter temperature in Fahrenheit: "))

    ct = 5 / 9 * (ft - 32)

    print("Temperature in Celsius: " ,ct)

elif choice==2:

    ct=float(input("Enter temperature in Celsius: "))

    ft = 1.8 * ct + 32

    print("Temperature in Fahrenheit: " , ft)

else:

    print("Incorrect Choice")

Output

============================ RESTART: E:/Python/APC Java/Q15.py ===========================

Type 1 to convert from Fahrenheit to Celsius

Type 2 to convert from Celsius to Fahrenheit 1

Enter temperature in Fahrenheit: 100

Temperature in Celsius:  37.77777777777778

 

============================ RESTART: E:/Python/APC Java/Q15.py ===========================

Type 1 to convert from Fahrenheit to Celsius

Type 2 to convert from Celsius to Fahrenheit 2

Enter temperature in Celsius: 38

Temperature in Fahrenheit:  100.4

 

Question 16

The volume of solids, viz. cuboid, cylinder and cone can be calculated by the formula:

  1. Volume of a cuboid (v = l*b*h)
  2. Volume of a cylinder (v = π*r2*h)
  3. Volume of a cone (v = (1/3)*Ï€*r2*h)

Using a if statement, write a program to find the volume of different solids by taking suitable variables and data types.

import math
print("1. Volume of cuboid")
print("2. Volume of cylinder")
print("3. Volume of cone")
choice =int(input("Enter your choice: "))
if choice==1:
    l=float(input("Enter length of cuboid: "))
    b=float(input("Enter breadth of cuboid: "))
    h=float(input("Enter height of cuboid: "))
    vol = l*b*h
    print("Volume of cuboid = ", vol)   
elif choice==2:
    rCylinder=float(input("Enter radius of cylinder: "))
    hCylinder=float(input("Enter height of cylinder: "))
    vCylinder = (22 / 7.0) * math.pow(rCylinder, 2) * hCylinder
    print("Volume of cylinder = " , vCylinder)
elif choice==3:
    rCone = float(input("Enter radius of cone: "))
    hCone=float(input("Enter height of cone: "))
    vCone = (1/3)*(22/7)*math.pow(rCone,2) * hCone
    print("Volume of cone = " , vCone)
else:
    print("Wrong choice! Please select from 1 or 2 or 3.")
Output

============================ RESTART: E:/Python/APC Java/Q16.py ===========================

1. Volume of cuboid

2. Volume of cylinder

3. Volume of cone

Enter your choice: 2

Enter radius of cylinder: 5

Enter height of cylinder: 10

Volume of cylinder =  785.7142857142857

============================ RESTART: E:/Python/APC Java/Q16.py ===========================

1. Volume of cuboid

2. Volume of cylinder

3. Volume of cone

Enter your choice: 1

Enter length of cuboid: 2

Enter breadth of cuboid: 5

Enter height of cuboid: 6

Volume of cuboid =  60.0

 

 

Question 17

A Mega Shop has different floors which display varieties of dresses as mentioned
below:

  1. Ground floor : Kids Wear
  2. First floor : Ladies Wear
  3. Second floor : Designer Sarees
  4. Third Floor : Men's Wear

The user enters floor number and gets the information regarding different items of the Mega shop. After shopping, the customer pays the amount at the billing counter and the shopkeeper prints the bill in the given format:

Name of the Shop: City Mart
Total Amount:
Visit Again!!

Write a program to perform the above task as per the user's choice.

print("1. Ground floor")
print("2. First floor")
print("3. Second floor")
print("4. Third floor")
floor=int(input("Select a floor: "))
isFloorValid = True
if floor==1:
    print("Kids Wear")
elif floor==2:
    print("Ladies Wear")
elif floor==3:
    print("Designer Sarees")
elif floor==4:
    print("Men's Wear")
else:
    isFloorValid = false
    print("Incorrect Floor")
if (isFloorValid):
    amt=float(input("Enter bill amount: "))
    print("Name of the Shop: City Mart")
    print("Total Amount: " , amt)
    print("Visit Again!!")
Output

============================ RESTART: E:/Python/APC Java/Q17.py ===========================

1. Ground floor

2. First floor

3. Second floor

4. Third floor

Select a floor: 1

Kids Wear

Enter bill amount: 1000

Name of the Shop: City Mart

Total Amount:  1000.0

Visit Again!!

============================ RESTART: E:/Python/APC Java/Q17.py ===========================

1. Ground floor

2. First floor

3. Second floor

4. Third floor

Select a floor: 3

Designer Sarees

Enter bill amount: 3000

Name of the Shop: City Mart

Total Amount:  3000.0

Visit Again!!

Question 18

The equivalent resistance of series and parallel connections of two resistances are given by the formula:

(a) R1 = r1 + r2 (Series)
(b) R2 = (r1 * r2) / (r1 + r2) (Parallel)

Using a if statement, write a program to enter the value of r1 and r2. Calculate and display the equivalent resistances accordingly.

print("1. Series")

print("2. Parallel")

choice=int(input("Enter your choice: "))

isChoiceValid = True

r1=float(input("Enter r1: "))

r2=float(input("Enter r2: "))

if choice==1:

        eqr = r1 + r2

elif choice==2:

        eqr = (r1 * r2) / (r1 + r2)

else:

        isChoiceValid = False

        print("Incorrect choice")

if (isChoiceValid):

    print("Equivalent resistance = " , eqr)

Output

============================ RESTART: E:/Python/APC Java/Q18.py ===========================

1. Series

2. Parallel

Enter your choice: 1

Enter r1: 10

Enter r2: 20

Equivalent resistance =  30.0

 

============================ RESTART: E:/Python/APC Java/Q18.py ===========================

1. Series

2. Parallel

Enter your choice: 2

Enter r1: 20

Enter r2: 10

Equivalent resistance =  6.666666666666667

Question 19

The Simple Interest (SI) and Compound Interest (CI) of a sum (P) for a given time (T) and rate (R) can be calculated as:

(a) SI = (p * r * t) / 100        (b) CI = P * ((1 + (R / 100))T - 1)

Write a program to input sum, rate, time and type of Interest ('S' for Simple Interest and 'C' for Compound Interest). Calculate and display the sum and the interest earned.

import math
p=float(input("Enter the sum: "))
r=float(input("Enter rate: "))
t=int(input("Enter time: "))
print("Enter type of Interest")
Type=input("'S'- Simple Interest 'C'- Compound Interest): ").upper()
isTypeValid = True
if Type=='S':
    interest = p * r * t / 100
elif Type=='C':
    interest = p * (math.pow((1 + (r / 100)), t) - 1)
else:
    isTypeValid = False
    print("Incorrect Interest type")
if (isTypeValid):
    amt = p + interest
    print("Sum = " , p)
    print("Interest = " , interest)
    print("Sum + Interest = " , amt)
Output

============================ RESTART: E:/Python/APC Java/Q19.py ===========================

Enter the sum: 10000

Enter rate: 5

Enter time: 10

Enter type of Interest

'S'- Simple Interest 'C'- Compound Interest): s

Sum =  10000.0

Interest =  5000.0

Sum + Interest =  15000.0

============================ RESTART: E:/Python/APC Java/Q19.py ===========================

Enter the sum: 10000

Enter rate: 5

Enter time: 10

Enter type of Interest

'S'- Simple Interest 'C'- Compound Interest): c

Sum =  10000.0

Interest =  6288.94626777442

Sum + Interest =  16288.94626777442

 

 

Question 20

'Kumar Electronics' has announced the following seasonal discounts on purchase of certain items.

Purchase Amount

Discount on Laptop

Discount on Desktop PC

Up to ₹ 25000

0.0%

5.0%

₹ 25,001 to ₹ 50,000

5%

7.5%

₹ 50,001 to ₹ 1,00,000

7.5%

10.0%

More than ₹ 1,00,000

10.0%

15.0%

Write a program to input name, amount of purchase and the type of purchase (`L' for Laptop and 'D' for Desktop) by a customer. Compute and print the net amount to be paid by a customer along with his name.
(Net amount = Amount of purchase - discount)

name=input("Enter Name: ")
amt=float(input("Enter Amount of Purchase: "))
print("Enter Type of Purchase")
Type=input("'L'- Laptop or 'D'- Desktop: ").upper()
if Type=='L':  
    if (amt <= 25000):
        disc=0
    elif(amt <= 50000):
        disc =5
    elif (amt <= 100000):
        disc = 7.5
    else:
        disc =10
if Type=='D':  
    if (amt <= 25000):
        disc=5
    elif(amt <= 50000):
        disc =7.5
    elif (amt <= 100000):
        disc = 10
    else:
        disc =15  
netAmt = amt - (disc * amt / 100)
print("Name: " , name)
print("Net Amount: " , netAmt)
Output

============================ RESTART: E:/Python/APC Java/Q20.py ===========================

Enter Name: Narain Ji

Enter Amount of Purchase: 50000

Enter Type of Purchase

'L'- Laptop or 'D'- Desktop: L

Name:  Narain Ji

Net Amount:  47500.0

============================ RESTART: E:/Python/APC Java/Q20.py ===========================

Enter Name: Aviral

Enter Amount of Purchase: 50000

Enter Type of Purchase

'L'- Laptop or 'D'- Desktop: D

Name:  Aviral

Net Amount:  46250.0

 

No comments:

Post a Comment