|
| 1 | +# 1)This program prints Hello, world! |
| 2 | +#print('Hello, world!') |
| 3 | + |
| 4 | + |
| 5 | +#2) This program adds two numbers |
| 6 | +num1 = 1.5 |
| 7 | +num2 = 6.3 |
| 8 | +# Add two numbers |
| 9 | +sum = float(num1) + float(num2) |
| 10 | +# Display the sum |
| 11 | +#print('The sum of {0} and {1} is {2}'.format(num1, num2, sum)) |
| 12 | + |
| 13 | + |
| 14 | +"""# Store input numbers |
| 15 | +num1 = input('Enter first number: ') |
| 16 | +num2 = input('Enter second number: ') |
| 17 | +# Add two numbers |
| 18 | +sum = float(num1) + float(num2) |
| 19 | +# Display the sum |
| 20 | +print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))""" |
| 21 | + |
| 22 | +#print('The sum is %.1f' %(float(input('Enter first number: '))+float(input('Enter second number: ')))) |
| 23 | + |
| 24 | + |
| 25 | +# 3)Python Program to calculate the square root |
| 26 | +# Note: change this value for a different result |
| 27 | +num = 8 |
| 28 | +# uncomment to take the input from the user |
| 29 | +#num = float(input('Enter a number: ')) |
| 30 | +num_sqrt = num ** 0.5 |
| 31 | +#print('The square root of %0.3f is %0.3f'%(num ,num_sqrt)) |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +# Find square root of real or complex numbers |
| 36 | +# Import the complex math module |
| 37 | +import cmath |
| 38 | +# change this value for a different result |
| 39 | +num = 1+2j |
| 40 | +# uncommment to take input from the user |
| 41 | +#num = eval(input('Enter a number: ')) |
| 42 | +num_sqrt = cmath.sqrt(num) |
| 43 | +#print('The square root of {0} is {1:0.3f}+{2:0.3f}j'.format(num ,num_sqrt.real,num_sqrt.imag)) |
| 44 | + |
| 45 | + |
| 46 | +#4) Python Program to find the area of triangle |
| 47 | +a = 5 |
| 48 | +b = 6 |
| 49 | +c = 7 |
| 50 | +# Uncomment below to take inputs from the user |
| 51 | +# a = float(input('Enter first side: ')) |
| 52 | +# b = float(input('Enter second side: ')) |
| 53 | +# c = float(input('Enter third side: ')) |
| 54 | +# calculate the semi-perimeter |
| 55 | +s = (a + b + c) / 2 |
| 56 | +# calculate the area |
| 57 | +area = (s*(s-a)*(s-b)*(s-c)) ** 0.5 |
| 58 | +#print('The area of the triangle is %0.2f' %area) |
| 59 | + |
| 60 | + |
| 61 | +# 5)Solve the quadratic equation ax**2 + bx + c = 0 |
| 62 | +# import complex math module |
| 63 | +import cmath |
| 64 | +a = 1 |
| 65 | +b = 5 |
| 66 | +c = 6 |
| 67 | +# To take coefficient input from the users |
| 68 | +# a = float(input('Enter a: ')) |
| 69 | +# b = float(input('Enter b: ')) |
| 70 | +# c = float(input('Enter c: ')) |
| 71 | +# calculate the discriminant |
| 72 | +d = (b**2) - (4*a*c) |
| 73 | +# find two solutions |
| 74 | +sol1 = (-b-cmath.sqrt(d))/(2*a) |
| 75 | +sol2 = (-b+cmath.sqrt(d))/(2*a) |
| 76 | +#print('The solution are {0} and {1}'.format(sol1,sol2)) |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +#6) Python program to swap two variables |
| 82 | +# To take input from the user |
| 83 | +# x = input('Enter value of x: ') |
| 84 | +# y = input('Enter value of y: ') |
| 85 | +x = 5 |
| 86 | +y = 10 |
| 87 | +# create a temporary variable and swap the values |
| 88 | +temp = x |
| 89 | +x = y |
| 90 | +y = temp |
| 91 | +#print('The value of x after swapping: {}'.format(x)) |
| 92 | +#print('The value of y after swapping: {}'.format(y)) |
| 93 | + |
| 94 | +#7) Program to generate a random number between 0 and 9 |
| 95 | +# import the random module |
| 96 | +import random |
| 97 | +#print(random.randint(0,9)) |
| 98 | + |
| 99 | +#8) Python Program to Convert Kilometers to Miles |
| 100 | + |
| 101 | +kilometers = 5.5 |
| 102 | +# To take kilometers from the user, uncomment the code below |
| 103 | +#kilometers = input("Enter value in kilometers") |
| 104 | +# conversion factor |
| 105 | +conv_fac = 0.621371 |
| 106 | +# calculate miles |
| 107 | +miles = kilometers * conv_fac |
| 108 | +#print('%0.3f kilometers is equal to %0.3f miles' %(kilometers,miles)) |
| 109 | + |
| 110 | +#9) Python Program to convert temperature in celsius to fahrenheit |
| 111 | +# change this value for a different result |
| 112 | +celsius = 37.5 |
| 113 | +# calculate fahrenheit |
| 114 | +fahrenheit = (celsius * 1.8) + 32 |
| 115 | +#print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit' %(celsius,fahrenheit)) |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
0 commit comments