Posts

Showing posts with the label Python Programming Examples

Python Program to Find the Factorial of a Number

Python Program to Find the Factorial of a Number  # Python program to find the factorial of a number provided by the user. # change the value for a different result num = 7 # uncomment to take input from the user #num = int(input("Enter a number: ")) factorial = 1 # check if the number is negative, positive or zero if num < 0:    print("Sorry, factorial does not exist for negative numbers") elif num == 0:    print("The factorial of 0 is 1") else:    for i in range(1,num + 1):        factorial = factorial*i    print("The factorial of",num,"is",factorial)

Python Program to Print all Prime Numbers in an Interval

Python Program to Print all Prime Numbers in an Interval  # Python program to display all the prime numbers within an interval # change the values of lower and upper for a different result lower = 800 upper = 1000 # uncomment the following lines to take input from the user #lower = int(input("Enter lower range: ")) #upper = int(input("Enter upper range: ")) print("Prime numbers between",lower,"and",upper,"are:") for num in range(lower,upper + 1):    # prime numbers are greater than 1    if num > 1:        for i in range(2,num):            if (num % i) == 0:                break        else:            print(num) output: Prime numbers between 800 and 1000 are: 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 ...

Python Program to Check Prime Number

Python Program to Check Prime Number # Python program to check if the input number is prime or not num = 407 # take input from the user # num = int(input("Enter a number: ")) # prime numbers are greater than 1 if num > 1:    # check for factors    for i in range(2,num):        if (num % i) == 0:            print(num,"is not a prime number")            print(i,"times",num//i,"is",num)            break    else:        print(num,"is a prime number")        # if input number is less than # or equal to 1, it is not prime else:    print(num,"is not a prime number") output: 407 is not a prime number 11 times 37 is 407  In this program, variable num is checked if it's prime ...

Add Two Numbers Provided by The User

Add Two Numbers Provided by The User # Store input numbers num1 = input ( 'Enter first number: ' ) num2 = input ( 'Enter second number: ' ) # Add two numbers sum = float ( num1 ) + float ( num2 ) # Display the sum print ( 'The sum of {0} and {1} is {2}' . format ( num1 , num2 , sum ))      output:   Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.8

Python Program to Add Two Numbers

Python Program to Add Two Numbers # This program adds two numbers num1 = 1.5 num2 = 6.3 # Add two numbers sum = float(num1) + float(num2) # Display the sum print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))  output The sum of 1.5 and 6.3 is 7.8

Python Program to Print Hello world!

Python Program to Print Hello world! # This program prints Hello, world! print('Hello, world!')