Summary

This document provides several simple programming examples using Python. The examples include calculating the sum of two numbers, finding the greatest of two numbers, determining leap years, calculating the average of three numbers, and calculating factorials. It also demonstrates creating and using functions, and importing modules in programming.

Full Transcript

Example Programs: **1. Addition of Two numbers:**\ int a =10;\ int b = 15\ c = a+b;\ print c\ Output:\ 25 **2. Addition of Two numbers:**\ a=int(input("Enter the values of a")\ b=int(input("Enter the values of b")\ c = a+b;\ print c\ Output:\ Enter the values of a\ 15\ Enter the values of b\ 10\ 2...

Example Programs: **1. Addition of Two numbers:**\ int a =10;\ int b = 15\ c = a+b;\ print c\ Output:\ 25 **2. Addition of Two numbers:**\ a=int(input("Enter the values of a")\ b=int(input("Enter the values of b")\ c = a+b;\ print c\ Output:\ Enter the values of a\ 15\ Enter the values of b\ 10\ 25 **3. Greatest of two numbers:**\ a=int(input("Enter the value of a"))\ b=int(input("Enter the value of b"))\ if(a\>b) print("A is greater")\ else\ print("B is greater")\ Output:\ Enter the value of a\ 15\ Enter the value of b\ 25\ B is greater **4. Finding a Leap year**: year=int(input("Enter the year"))\ if(year%4==0)\ print("Year is Leap Year")\ else\ print("Year is not a Leap Year")\ Output:\ Enter the Year\ 2022\ Year is not a Leap Year **5.AVERAGE of 3 Numbers:** a = int (input(\" Please Enter the First Number: \")) b = int (input(\" Please Enter the second number: \")) c = int (input(\" Please Enter the third number: \")) average=(a+b+c)/3 print(\"The average of three numbers is \", average) **Output:** Please Enter the First Number: 3 Please Enter the second number: 5 Please Enter the third number: 1 **The average of three numbers is 3** **6.FACTORIAL of a Number:** def factorial(x): \"\"\"This is a recursive function to find the factorial of an integer\"\"\" if x == 1: return 1 else: \# recursive call to the function return (x \* factorial(x-1)) \# change the value for a different result num = 7 \# to take input from the user \# num = int(input(\"Enter a number: \")) \# call the factorial function result = factorial(num) print(\"The factorial of\", num, \"is\", result) **7.QUATRATIC EQUATION:** import cmath a = 1 b = 5 c = 6 \# calculate the discriminant d = (b\*\*2) - (4\*a\*c) \# find two solutions sol1 = (-b-cmath.sqrt(d))/(2\*a) sol2 = (-b+cmath.sqrt(d))/(2\*a) print(\'The solution are {0} and {1}\'.format(sol1,sol2)) **ANOTHER METHOD:** +-----------------------------------------------------------------------+ | \# import complex math module | | | | import cmath | | | |    | | | | a = 1 | | | | b = 4 | | | | c = 2 | | | |    | | | | \# calculating  the discriminant | | | | dis = (b\*\*2) - (4 \* a\*c) | | | |    | | | | \# find two results | | | | ans1 = (-b-cmath.sqrt(dis))/(2 \* a) | | | | ans2 = (-b + cmath.sqrt(dis))/(2 \* a) | | | |    | | | | \# printing the results | | | | print(\'The roots are\') | | | | print(ans1) | | | | print(ans2) | +-----------------------------------------------------------------------+ **Output:** The roots are (-3.414213562373095+0j) (-0.5857864376269049+0j)

Use Quizgecko on...
Browser
Browser