Posts

Showing posts from July, 2023

Random number generate between two number using Python.

Image
 Q. Input two numbers and generate a Random Number between them. Ans: import random a= int ( input ( "Enter starting num:" )) b= int ( input ( "Enter end number:" )) num1 = random.randint(a, b) print (num1) OUTPUT

Area of triangle using python.

 Q.  Input three sides of a triangle and calculate the area of a triangle and display it . Ans: # Three sides of the triangle is a, b and c: a = float ( input ( 'Enter first side: ' )) b = float ( input ( 'Enter second side: ' )) c = float ( input ( 'Enter third side: ' )) # calculate the semi-perimeter s = (a + b + c) / 2 # calculate the area area = (s * (s - a) * (s - b) * (s - c)) ** 0.5 print ( 'The area of the triangle is %0.2f' % area)

Distance between two points using pythagoras theorem.

 Q. write a program to compute distance between two points taking inputs from the user ( pythagoras theorem). Ans: x1 = int ( input ( "enter x1 : " )) x2 = int ( input ( "enter x2 : " )) y1 = int ( input ( "enter y1 : " )) y2 = int ( input ( "enter y2 : " )) result = ((((x2 - x1 )** 2 ) + ((y2-y1)** 2 ) )** 0.5 ) print ( "distance between" ,(x1,x2), "and" ,(y1,y2), "is : " ,result)

Basic Mathematical Operations using Python.

 Q.  write a program using python to do the following operations taking integer inputs: 1. addition operation 2. subtraction operation 3. multiplication operation 4. division operation 5. modulus operation 6. exponent operation 7. floor division operation. Ans: def addition (x, y): return x + y def subtraction (x, y): return x - y def multiplication (x, y ): return x * y1 def division (x, y): if y != 0 : return x / y else : return "Error: Cannot divide by zero!" def modulus (x, y): if y != 0 : return x % y else : return "Error: Cannot perform modulus with zero!" def exponent (x, y): return x ** y def floor_division (x, y): if y != 0 : return x // y else : return "Error: Cannot perform floor division with zero!" def get_integer_input (message): while True : try : num = int ( input (message)) return num except ValueError : ...