Posts

Showing posts with the label python program

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