Posts

Showing posts from 2020

program to input some numbers repeatedly and print their sum. the program ends when the users say no more to enter (normal termination) or program aborts when the number entered is less than zero.

DOUBLE PENDULUM ANIMATION PROBLEM

from numpy import sin , cos import numpy as np import matplotlib.pyplot as plt import scipy.integrate as integrate import matplotlib.animation as animation G = 9.8 # acceleration due to gravity, in m/s^2 L1 = 1.0 # length of pendulum 1 in m L2 = 1.0 # length of pendulum 2 in m M1 = 1.0 # mass of pendulum 1 in kg M2 = 1.0 # mass of pendulum 2 in kg def derivs ( state , t ): dydx = np . zeros_like ( state ) dydx [ 0 ] = state [ 1 ] del_ = state [ 2 ] - state [ 0 ] den1 = ( M1 + M2 ) * L1 - M2 * L1 * cos ( del_ ) * cos ( del_ ) dydx [ 1 ] = ( M2 * L1 * state [ 1 ] * state [ 1 ] * sin ( del_ ) * cos ( del_ ) + M2 * G * sin ( state [ 2 ]) * cos ( del_ ) + M2 * L2 * state [ 3 ] * state [ 3 ] * sin ( del_ ) - ( M1 + M2 ) * G * sin ( state [ 0 ])) / den1 dydx [ 2 ] = state [ 3 ] den2 = ( L2 / L1 ) * den1 dydx [ 3 ] = ( - M2 ...

program to print sum of natural numbers between 1 to 7.

Sum=0 For n in range(1,8):          sum+=n print("sum of natural numbers<=",n,'is',sum) Output Sum of natural numbers <= 7 is 28

program to print table of anumber, say 5

num=5 For a in range (1,11):   Print(num,'x'a,'=',num*a) Output 5×1=5 5×2=10 5×3=15 5×4=20 5×5=25 5×6=30 5×7=35 5×8=40 5×9=45 5×10=50

program to calculate and print roots of a quadratic equation: ax²+bx+c=0

import math print ("For quadnatic equation, ax**2+ bx+ c.6, enter coefficlents below")  a=int( input ( "Enter a :") ) b°=int( input ( "Enter b ;"))  c= int( input ("Enter c :"))  1f a==0:      print (value of", a, ' should not be zero')          print ("n Aborting !!!111")  else:       delta=b*b-4a*c  if delta>0:           root1=(-b+math.sart (delta)) / (2*a)           root2 =(-b-math. sqrt (delta))/ (2*a)         print ("Roots are REAL and UNEQUAL")       print ("Root1=", root1, ", Root2=", root2)  elif delta==0:        root1=-b/ (2*a);        print ("Roots are REAL and EQUAL")        print ("Root1 ", root1, ", Root2 a", root1)  else:       print ("Roots are COMPLEX and IMAGINARY") Output  For quadratic equation, ax*2 + bx...

program that reads two numbers and an arithmetic operator and displays the computed result.

num1=float(input("Enter first number:")) num2=float(input("Enter second number:")) op=input("Enter operator[ + - * / % ]:") result=0 If op == '+':         result=num1+num2 else op =='-':        result=num1-num2 else op =='*':        result=num1*num2 else op =='/':        result=num1/num2 else op =='%':        result=num1%num2 else:       print ("Invalid operator!!") print(num1,op,num2,'=',result) Output Enter first number:5 Enter second number:2 Enter operator[ + - * / % ]: * 5.0*2.0=10.0

program to display a menu for calculating area of a circle or perimeter of a circle.

radius=float(input("Enter radius of the circle:")) print("1.calculate area") print("2.calculate perimeter") Choice=int(input("Enter your choice (1or2):")) if choice == 1:    area=3.14159*radius*radius    print("area of circle with radius", radius,'is',area)     else:          perm=2*3.14159*radius          print ("perimeter of circle with radius", radius,'is',perm) Output Enter radius of circle : 2.5         1.calculate area          2.calculate perimeter Enter your choice(1or 2):1 area of circle with radius 2.5 is 19.6349375

program to test the divisibility of a number with another number.

Number 1 : int (input ("Enter first number:")) Number 2 : int (input ("Enter second number:")) remainder = number1%number 2 If remainder == 0 :    Print(number1,"is divisible by", number 2)  else :      Print(number1,"is not divisible by", number 2) Output Enter first number : 119 Enter second number :3 119.0 is not divisible by 3.0 ============================== Enter first number : 119 Enter second number : 17 119.0 is divisible by 17.0

program to find the multiples of a number ( the divisor) out of given five numbers.

Print ("Enter five numbers below") num1= float (input("First number:")) num2= float (input("Second number:")) num3= float (input("Third number:")) num4= float (input("Fourth number:")) num5= float (input("Fifth number:")) divisor= float (input("Enter divisor number:")) Count=0 print("Multiples of",divisir, "are:") remainder=num1%divisor if remainder ==0:       print(num1,sep=" ")        Count +=1 remainder=num2%divisor if remainder ==0:       print(num2,sep=" ")        Count +=1 remainder=num3%divisor if remainder ==0:       print(num3,sep=" ")        Count +=1 remainder=num4%divisor if remainder ==0:       print(num4,sep=" ")        Count +=1 remainder=num5%divisor if remainder ==0:       print(num5,sep=" ")        Count +=1 Print() Print(Count,"multiples of",divisor,"found") Output Enter five numbers below First ...

WAP to obtain temperature in Celsius and convert it into Fahrenheit using formula. °C×9/5+32=°F

#celsius to fahrenheit  Cels= float(input("Enter temp in Celsius:")) Print(" Temperature in Celsius is:",Cels) Fahr=Cels×9/5+32 Print("Temperature in Fahrenheit is:",Fahr)

program to calculate BMI (body mass index) of a person.

#to calculate BMI = Kg/m square Weight _in_kg = float (input("Enter weight in kg:")) Height_in_meter = float(input("Enter height in meter :")) Bmi = Weight_in_kg / ( Height_in_meter* Height_in_meter) Print("BMI is:", Bmi) Output Enter weight in kg  : 66 Enter height in meter : 1.6 BMI is : 25.781249999999996

program to obtain three numbers and print their sum.

Image
#to input 3 numbers and print their sum. Num1=int(input("enter number1;")) Num2=int(input("enter number2;")) Num3=int(input("enter number3;")) Sum= Num1+Num2+Num3 Print("Three numbers are:",num1,num2,num3) Print("sum is :",sum)

To find average number in python programming.

# Get three test score round1 = int(input("Enter score for round 1: ")) round2 = int(input("\nEnter score for round 2: ")) round3 = int(input("\nEnter score for round 3: "))     # Calculate the average average = (round1 + round2 + round3) / 3 # Print out the test score print("\nThe average score is: ", average) Output: 4 37 64 Enter score for round 1: Enter score for round 2: Enter score for round 3: The average score is: 35.0

To find area of triangle in python programming

# Find the area of triangle a = 5 b = 6 c = 7 # 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) Output : The area of the triangle is 14.70

To find the sum of two numbers in python programming

# This program adds two numbers value1 = 14.0 value2 = 56.60 # Add two numbers result = float(value1) + float(value2) # Display the sum print('The sum of {0} and {1} is {2}'.format(value1, value2, result)) Output: The sum of 14.0 and 56.6 is 70.6

moving circle on canvas by tkinter

Image
#!/usr/bin/python from tkinter import * import time gui = Tk() var=IntVar() gui.geometry("800x800") c = Canvas(gui ,width=800 ,height=800) c.pack() oval = c.create_oval(5,5,60,60,fill='pink') a = 5 b = 5 for x in range(0 ,100):   c.move(oval,a,b)   gui.update()   time.sleep(.01) gui.title("First title") gui.mainloop()

oval in canvas

Image
from tkinter import * import time gui = Tk() gui.geometry("400x400") gui.title(" First title") canvas = Canvas(gui, width=1000,height=1200,bg='red') canvas.pack() ov1 = canvas.create_oval(25,25,60,60, fill='yellow') gui.mainloop()

analog clock

Image
# import winsound from turtle import Turtle, Screen, Terminator from datetime import datetime screen = Screen() screen.mode('logo') screen.setup(700, 700) screen.title('Alarm Clock') tag_list = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] monat = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'June', 'July', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.'] FONT_SIZE = 14 FONT = ('Courier', FONT_SIZE, 'bold') BUTTON_SIZE = 40 CURSOR_SIZE = 20 def jump(turtle, distance):     turtle.penup()     turtle.forward(distance)     turtle.pendown() def hand(turtle, laenge, spitze):     turtle.fd(laenge * 1.10)     turtle.rt(90)     turtle.fd(spitze / 20.0)     turtle.lt(120)     turtle.fd(spitze)     turtle.lt(120)     turtle.fd(spitze) ...