Sunday, July 29, 2018

PROGRAMS

PROGRAM 1

Python script to print Hello World on screen

Printing a message on screen is very simple in Python programming. In Python you can print a message on screen with a single line of code (print statement).
# This prints Hello World on the output screen
print('Hello World')
Output:
Hello World

PROGRAM 2

Get String input from user

In this program we will see how to receive string input from user in Python.
# Python Program - Get String Input from User
str = input("Enter any string: ")
print(str)


Get Integer Input from user

# Python Program - Get Integer Input from User
num = int(input("Enter an Integer: "))
print(num)

Get Float Input from user

This is similar to what we have seen above, except that we use float() function to convert the received input into a float value.
# Python Program - Get Float Input from User
num = float(input("Enter a float value: "))
print(num)

PROGRAM 3

Python code

The user is asked to enter the number, the input number is stored in a variable number and then we have checked the number using if..elif..else statement.
# User enters the number
number = int(input("Enter number: "))

# checking the number
if number < 0: print("The entered number is negative.") elif number > 0:
    print("The entered number is positive.")
elif number == 0:
    print("Number is zero.")
else:
    print("The input is not a number")


Python Programs
Python program to do arithmetical operations
1.    # Store input numbers:  
2.    num1 = input('Enter first number: ')  
3.    num2 = input('Enter second number: ')  
4.      
5.    # Add two numbers  
6.    sum = float(num1) + float(num2)  
7.    # Subtract two numbers  
8.    min = float(num1) - float(num2)  
9.    # Multiply two numbers  
10. mul = float(num1) * float(num2)  
11. #Divide two numbers  
12. div = float(num1) / float(num2)  
13. # Display the sum  
14. print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))  
15.   
16. # Display the subtraction  
17. print('The subtraction of {0} and {1} is {2}'.format(num1, num2, min))  
18. # Display the multiplication  
19. print('The multiplication of {0} and {1} is {2}'.format(num1, num2, mul))  
20. # Display the division  
21. print('The division of {0} and {1} is {2}'.format(num1, num2, div))  

Python program to find the area of a triangle

1     1.    # Three sides of the triangle is a, b and c:  
2.    a = float(input('Enter first side: '))  
3.    b = float(input('Enter second side: '))  
4.    c = float(input('Enter third side: '))  
5.      
6.    # calculate the semi-perimeter  
7.    s = (a + b + c) / 2  
8.      
9.    # calculate the area  
10. area = (s*(s-a)*(s-b)*(s-c)) ** 0.5  
11. print('The area of the triangle is %0.2f' %area)   
Python program to solve quadratic equation
1    1.    # import complex math module  
2.    import cmath  
3.    a = float(input('Enter a: '))  
4.    b = float(input('Enter b: '))  
5.    c = float(input('Enter c: '))  
6.      
7.    # calculate the discriminant  
8.    d = (b**2) - (4*a*c)  
9.      
10. # find two solutions  
11. sol1 = (-b-cmath.sqrt(d))/(2*a)  
12. sol2 = (-b+cmath.sqrt(d))/(2*a)  
13. print('The solution are {0} and {1}'.format(sol1,sol2))   

Python program to swap two variables
1.    # Python swap program   
2.    x = input('Enter value of x: ')  
3.    y = input('Enter value of y: ')  
4.      
5.    # create a temporary variable and swap the values  
6.    temp = x  
7.    x = y  
8.    y = temp  
9.      
10. print('The value of x after swapping: {}'.format(x))  
11. print('The value of y after swapping: {}'.format(y))  

Python program to convert kilometers to miles
Here, we are going to see the python program to convert kilometers to miles. Let's understand kilometers and miles first.
Kilometer: The kilometer is a unit of length in the metric system. It is equivalent to 1000 meters.
Miles:  Mile is also the unit of length. It is equal to 1760 yards.
Conversion formula:
1 kilometer is equal to 0.62137 miles.
1.    # Collect input from the user  
2.    kilometers = float(input('How many kilometers?: '))  
3.    # conversion factor  
4.    conv_fac = 0.621371  
5.    # calculate miles  
6.    miles = kilometers * conv_fac  
7.    print('%0.3f kilometers is equal to %0.3f miles' %(kilometers,miles))  

Python program to convert Celsius to Fahrenheit
Conversion formula:
T() = T() x 9/5 + 32 Or, T() = T() x 1.8 + 32
1.    # Collect input from the user  
2.    celsius = float(input('Enter temperature in Celsius: '))  
3.      
4.    # calculate temperature in Fahrenheit  
5.    fahrenheit = (celsius * 1.8) + 32  
6.    print('%0.1f  Celsius is equal to %0.1f degree Fahrenheit'%(celsius,fahrenheit))  

Python program to display calendar
import calendar  
 And then apply the syntax  
(calendar.month(yy,mm))  

1.    import calendar  
2.    # Enter the month and year  
3.    yy = int(input("Enter year: "))  
4.    mm = int(input("Enter month: "))  
5.      
6.    # display the calendar  
7.    print(calendar.month(yy,mm))  
 

Python Program to check if a Number is Positive, Negative or Zero

1.    num = float(input("Enter a number: "))  
2.      
3.    if num > 0:  
4.     print("{0} is a positive number".format(num))  
5.    elif num == 0:  
6.       print("{0} is zero".format(num))   
7.    else:  
8.       print("{0} is negative number".format(num))   

Python Program to Check if a Number is Odd or Even

1.    num = int(input("Enter a number: "))  
2.    if (num % 2) == 0:  
3.       print("{0} is Even number".format(num))  
4.    else:  
5.       print("{0} is Odd number".format(num))  
 

Python Program to Check Leap Year

1.    year = int(input("Enter a year: "))  
2.    if (year % 4) == 0:  
3.       if (year % 100) == 0:  
4.           if (year % 400) == 0:  
5.               print("{0} is a leap year".format(year))  
6.           else:  
7.               print("{0} is not a leap year".format(year))  
8.       else:  
9.           print("{0} is a leap year".format(year))  
10. else:  
11.    print("{0} is not a leap year".format(year))  

Python Program to Check Prime Number

Prime numbers:
A prime number is a natural number greater than 1 and having no positive divisor other than 1 and itself. For example: 3, 7, 11 etc are prime numbers.
Composite number:
Other natural numbers that are not prime numbers are called composite numbers.
For example: 4, 6, 9 etc. are composite numbers.

 

1.    num = int(input("Enter a number: "))  
2.      
3.    if num > 1:  
4.       for i in range(2,num):  
5.           if (num % i) == 0:  
6.               print(num,"is not a prime number")  
7.               print(i,"times",num//i,"is",num)  
8.               break  
9.       else:  
10.        print(num,"is a prime number")  
11.          
12. else:  
13.    print(num,"is not a prime number")  

 

Python Program to Print all Prime Numbers between an Interval

1.    #Take the input from the user:   
2.    lower = int(input("Enter lower range: "))  
3.    upper = int(input("Enter upper range: "))  
4.      
5.    for num in range(lower,upper + 1):  
6.       if num > 1:  
7.           for i in range(2,num):  
8.               if (num % i) == 0:  
9.                   break  
10.        else:  
11.            print(num)  

Python Program to Find the Factorial of a Number

1.    num = int(input("Enter a number: "))  
2.    factorial = 1  
3.    if num < 0:  
4.       print("Sorry, factorial does not exist for negative numbers")  
5.    elif num == 0:  
6.       print("The factorial of 0 is 1")  
7.    else:  
8.       for i in range(1,num + 1):  
9.           factorial = factorial*i  
10.    print("The factorial of",num,"is",factorial)  

Python Program to Display the multiplication Table

  1. 1.    num = int(input("Show the multiplication table of? "))  
  2. 2.    # using for loop to iterate multiplication 10 times   
  3. 3.    for i in range(1,11):  
  4. 4.       print(num,'x',i,'=',num*i)  

Python Program to Print the Fibonacci sequence

Fibonacci sequence:
The Fibonacci sequence specifies a series of numbers where the next number is found by adding up the two numbers just before it.
For example:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on....
1.    nterms = int(input("How many terms you want? "))  
2.    # first two terms  
3.    n1 = 0  
4.    n2 = 1  
5.    count = 2  
6.    # check if the number of terms is valid  
7.    if nterms <= 0:  
8.       print("Plese enter a positive integer")  
9.    elif nterms == 1:  
10.    print("Fibonacci sequence:")  
11.    print(n1)  
12. else:  
13.    print("Fibonacci sequence:")  
14.    print(n1,",",n2,end=', ')  
15.    while count < nterms:  
16.        nth = n1 + n2  
17.        print(nth,end=' , ')  
18.        # update values  
19.        n1 = n2  
20.        n2 = nth  
21.        count += 1  

Python Program to Check Armstrong Number

Armstrong number:
A number is called Armstrong number if it is equal to the sum of the cubes of its own digits.
For example: 153 is an Armstrong number since 153 = 1*1*1 + 5*5*5 + 3*3*3.
The Armstrong number is also known as narcissistic number.
1.     num = int(input("Enter a number: "))  
2.    sum = 0  
3.    temp = num  
4.      
5.    while temp > 0:  
6.       digit = temp % 10  
7.       sum += digit ** 3  
8.       temp //= 10  
9.      
10. if num == sum:  
11.    print(num,"is an Armstrong number")  
12. else:  
13.    print(num,"is not an Armstrong number")  

Python Program to Find Armstrong Number between an Interval

1.    lower = int(input("Enter lower range: "))  
2.    upper = int(input("Enter upper range: "))  
3.      
4.    for num in range(lower,upper + 1):  
5.       sum = 0  
6.       temp = num  
7.       while temp > 0:  
8.           digit = temp % 10  
9.           sum += digit ** 3  
10.        temp //= 10  
11.        if num == sum:  
12.             print(num)  

Python Program to Find the Sum of Natural Numbers

1.    num = int(input("Enter a number: "))  
2.      
3.    if num < 0:  
4.       print("Enter a positive number")  
5.    else:  
6.       sum = 0  
7.       # use while loop to iterate un till zero  
8.       while(num > 0):  
9.           sum += num  
10.        num -= 1  
11.    print("The sum is",sum)