Numbers Code#

Section Title: Numbers Code

Please solve the following questions using Python code.  

Question-1#

Write a program that prompts the user for their weight in pounds.

  • Convert the weight to kilograms.

  • Print the converted weight.

Solution

pound = float(input('Enter your weight in pounds: '))

kilogram = pound*0.46

print('Your weight is', kilogram, 'kg.')

Question-2#

Find the area of a circle with a radius of 5.

  • Round the area to the nearest hundredth.

  • Area = \(\pi r^2\)

Solution

import math
r = 5

area = math.pi * r**2
print(round(area,2))

Question-3#

Write a program that prompts the user for a Fahrenheit temperature, converts the temperature to Kelvin, and prints out the converted temperature.

  • Hint: \(\displaystyle K = \frac{F-32}{1.8}+273 \)

Solution

fahrenheit = float(input('Enter the Fahrenheit Temperature: '))

kelvin = (fahrenheit-32)/1.8+273   # convert to kelvin

print('Kelvin Temparature is', kelvin)

Question-4#

Write a program that prompts the user for height (cm) and weight (kg), computes the body mass index, and prints it.

  • Hint: \( \displaystyle BMI = \frac{weight}{height^2}\)

Solution

height = int ( input('Enter the Height: ') )
weight = int ( input('Enter the Weight: ') )

bmi = weight/(height**2)

print('Body Mass Index is',bmi)

Question-5#

Write a program that prompts the user for a 3-digit positive number and displays the sum of its digits.

  • Use only one input function.

Solution-1

num = int( input('Enter a  3 digit number: ') )

n1 = num%10
n2 = int(((num-n1)/10)%10)
n3 = num//100      # or n3=int((num-n1-n2)/100)

sum_digit = n1+n2+n3

print('Sum of digits is', sum_digit)

Solution-2

num = int( input('Enter a  3 digit number: ') )

n3 = num//100
n2 = (num-n3*100)//10
n1=  num%10        # or(num-n3*100-n2*10)

sum_digit = n1+n2+n3

print('Sum of digits is', sum_digit)

Question-6#

Write a program that prompts the user for three different integers one by one, sorts these numbers, and prints them from smallest to largest.

  • Use the built-in functions max() and min(), and do not use any sorting function.

Solution-1

num1 = int ( input('Enter the First Number: ') )
num2 = int ( input('Enter the Second Number: ') )
num3 = int ( input('Enter the Third Number: ') )

minimum = min(num1,num2,num3)
maximum = max(num1,num2,num3)
middle  = num1+num2+num3-minimum-maximum

print(minimum,middle,maximum,sep = ',')

Solution-2

num1 = int ( input('Enter the First Number: ') )
num2 = int ( input('Enter the Second Number: ') )
num3 = int ( input('Enter the Third Number: ') )

minimum = min(num1,num2,num3)
maximum = max(num1,num2,num3)

middle = min(max(num1,num2), max(num2,num3), max(num3,num1))

print('Smallest to largest integer: ',min_num,mid,max_num)

Question-7#

Write a program that prompts the user for a 2-digit positive number. Swap the digits of the given number and print it.

  • Example 1: If the given number is 53, then print 35.

    • Print format: 53 —- swap—> 35

  • Example 2: If the given number is 71, then print 17.

    • Print format: 71 —- swap—> 17

Solution

number = int(input("Enter a two digit number: "))

ones = number%10
tens = number//10

result = ones*10 + tens

print(number,'---- swap--->', result)

Question-8#

Write a program that prompts the user for a 3-digit positive number. Swap the digits of the given number and print it.

  • Example 1: If the given number is 153, then print 351.

    • Print format: 153 —- swap—> 351

  • Example 2: If the given number is 571, then print 175.

    • Print format: 571 —- swap—> 175

Solution

number = int(input("Enter a three digit number: "))

ones = number%10
tens = (number//10)%10
hundreds = number//100

result = ones*100 + tens*10 + hundreds

print(number,'---- swap--->', result)

Question-9#

Write a program that prompts the user for two numbers using two input functions. Assign the entered values to variables with the names x and y.

  • Find \(\displaystyle f(x, y) = \frac{2x^3+3y^2}{x^2+y^2+1}\times5\)

  • Round this value to the second decimal place and print the result in the following format.

    • for \(x=1, y=2\) display \(f(1,2)=11.67\)

Solution

x = int(input('x: '))
y = int(input('y: '))

f_x_y = (2*x**3+3*y**2)/(x**2+y**2+1)*5

print('f(', x, ',' ,y, ')=', round(f_x_y,2), sep='')

Question-10#

Write a program that prompts the user for 5 numbers using five different input functions.

  • Find the average of these numbers without using any built-in function.

  • Round it to the nearest integer and display the result.

  • Example: If the given numbers are 2, 8, 9, 6, 5, then display average(2, 8, 9, 6, 5) = 6.

Solution

number1 = float(input("Enter 1st number: "))
number2 = float(input("Enter 2nd number: "))
number3 = float(input("Enter 3rd number: "))
number4 = float(input("Enter 4th number: "))
number5 = float(input("Enter 5th number: "))

average = sum([number1, number2, number3, number4, number5])/5

print('average(', number1,',' ,number2,',' , number3,',' , number4,',' , number5,') =', round(average), sep='')

Question-11#

Write a program that prompts the user for 5 numbers using five different input functions.

  • Find the sum of these numbers excluding the largest and smallest ones.

  • Display the sum.

Solution

number1 = float(input("Enter 1st number: "))
number2 = float(input("Enter 2nd number: "))
number3 = float(input("Enter 3rd number: "))
number4 = float(input("Enter 4th number: "))
number5 = float(input("Enter 5th number: "))

max_number = max(number1, number2, number3, number4, number5)
min_number = min(number1, number2, number3, number4, number5)

total = sum([number1, number2, number3, number4, number5])

print(total-(max_number+min_number))

Question-12#

  • Choose a 3-digit random number as the dividend.

  • Choose a 1-digit random number as the divisor.

  • Display these numbers.

  • Find the remainder and quotient when the dividend is divided by the divisor.

  • After 5 seconds, show the correct remainder and quotient.

Solution

import random
import time

divisor  = random.randint(1,9)         # random one digit number
dividend = random.randint(100,999)     # random three digit number

print('Divide', dividend, 'by', divisor)
print()

time.sleep(5)

print('Quotient  :', dividend//divisor)
print('Remainder :', dividend%divisor)