Table of Contents
Factorial, Fibonacci series, Armstrong, Palindrome , Recursion
Python program for factorial, reverse, palindrome, armstrong, basic syntax, fibonacci series, recursive function, even odd.
Practical 1a : Create a program that asks the user to enter their name and their age. Print out a message addressed to them that tells them the year that they will turn 100 years old.
""" Author : ITVoyagers (https://itvoyagers.in) Date :08th August 2018 Description : Create a program that asks the user to enter their name and their age. Print out a message addressed to them that tells them the year that they will turn 100 years old. """ name = input("What is your name: ") age = int(input("How old are you: ")) cur_year=int(input("Enter current year ")) year = str((cur_year - age)+100) print(name + " will be 100 years old in the year " + year)
OUTPUT
>>> What is your name: ITVoyagers How old are you: 1 Enter current year 2019 ITVoyagers will be 100 years old in the year 2118
Practical 1b : Program to check the given number is even or odd.
""" Author : ITVoyagers (https://itvoyagers.in) Date :08th August 2018 Description : Program to check the given number is even or odd. """ value = int(input("Enter a number: ")) if (value % 2) == 0: print(value , " is Even") else: print(value," is Odd")
OUTPUT
>>> >>> Enter a number: 6 6 is Even >>> ================================ RESTART ================================ >>> Enter a number: 5 5 is Odd >>>
Practical 1c : Program to display the Fibonacci sequence up to n-th term where n is provided by the user.
""" Author : ITVoyagers (https://itvoyagers.in) Date :08th August 2018 Description : Program to display the Fibonacci sequence up to n-th term where n is provided by the user. """ # change this value for a different result #nterms = 10 # uncomment to take input from the user nterms = int(input("How many terms? ")) # first two terms n1 = 0 n2 = 1 count = 2 # check if the number of terms is valid if nterms <= 0: print("Please enter a positive integer") elif nterms == 1: print("Fibonacci sequence upto",nterms,":") print(n1) else: print("Fibonacci sequence upto",nterms,":") print(n1,",",n2,end=', ') while count < nterms: nth = n1 + n2 print(nth,end=' , ') # update values n1 = n2 n2 = nth count += 1
OUTPUT
>>> How many terms? 10 Fibonacci sequence upto 10 : 0 , 1, 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 ,
Practical 1d : Python Program to Reverse a Number using While loop.
""" Author : ITVoyagers (https://itvoyagers.in) Date :08th August 2018 Description : Python Program to Reverse a Number using While loop. """ Number = int(input("Please Enter any Number: ")) Reverse = 0 while(Number > 0): Reminder = Number %10 Reverse = (Reverse *10) + Reminder Number = Number //10 print("n Reverse of entered number is =" , Reverse)
OUTPUT
>>> Please Enter any Number: 951 Reverse of entered number is = 159 >>>
Practical 1e : Python program to check if the number provided by the user is an Armstrong number or not.
""" Author : ITVoyagers (https://itvoyagers.in) Date :08th August 2018 Description : Python program to check if the number provided by the user is an Armstrong number or not. """ # take input from the user num = int(input("Enter any number: ")) power=len(str(num)) # initialize sum sum = 0 # find the sum of the cube of each digit temp = num while temp > 0: digit = temp % 10 sum += digit ** power temp //= 10 # display the result if num == sum: print(num,"is an Armstrong number") else: print(num,"is not an Armstrong number")
OUTPUT
>>> Enter any number: 371 371 is an Armstrong number >>> ================================ RESTART ================================ >>> Enter any number: 1634 1634 is an Armstrong number >>> ================================ RESTART ================================ >>> Enter any number: 121 121 is not an Armstrong number
Practical 1f : Python program to find the factorial of a number using recursion.
""" Author : ITVoyagers (https://itvoyagers.in) Date :08th August 2018 Description : Python program to find the factorial of a number using recursion. """ def recur_facto(n): """Function to return the factorial of a number using recursion""" if n == 1: return n else: return n*recur_facto(n-1) num = int(input("Enter a number: ")) # check is the number is negative if num < 0: print("Sorry, factorial does not exist for negative numbers") elif num == 0: print("The factorial of 0 is 1") else: print("The factorial of",num,"is",recur_facto(num))
OUTPUT
Enter a number: 5 The factorial of 5 is 120
Practical 1g : Python program to find the given number is palindrome or not.
""" Author : ITVoyagers (https://itvoyagers.in) Date :08th August 2018 Description : Python program to find the given number is palindrome or not. """ num=int(input("Enter a number")) sum1=0 n1=num while num!=0: rem=num%10 sum1=sum1*10+rem num=num//10 if sum1==n1: print (n1, "is palindrome") else: print (n1, "is not palindrome")
OUTPUT
>>> Enter a number121 121 is palindrome >>> ================================ RESTART ================================ >>> Enter a number120 120 is not palindrome >>>
Practical 1h : Python program to find the given number is palindrome or not.
""" Author : ITVoyagers (https://itvoyagers.in) Date :08th August 2018 Description : Python program to find the given number is palindrome or not. """ class palindrome_armstrong_combo: def palindrome(): num=int(input("Enter a number to check if palindrome or not:n")) sum1=0 n=num while num!=0: rem=num%10 sum1=(sum1*10)+rem num=num//10 if sum1==n: print (n, "is palindrome") else: print (n, "is not palindrome") def armstrong(): no = int(input("Enter a number to check if armstrong or not:n ")) sum = 0 temp = no while temp > 0: digit = temp % 10 sum += digit ** 3 temp //= 10 if no == sum: print(no,"is an Armstrong number") else: print(no,"is not an Armstrong number") p=palindrome_armstrong_combo print("1.palindromen2.armstrongn") ch=int(input("enter your choicen")) while ch != 4: if ch is 1: p.palindrome() elif ch is 2: p.armstrong() else: print("enter valid choice") ch=int(input("enter your choicen"))
OUTPUT
1.palindrome 2.armstrong enter your choice 1 Enter a number to check if palindrome or not: 212 212 is palindrome enter your choice 2 Enter a number to check if armstrong or not: 371 371 is an Armstrong number enter your choice 5 enter valid choice enter your choice
For other python practicals:
For other python basics related posts:
For other advanced python related posts: