Skip to main content

Posts

Showing posts from November, 2019
Prime Number: A prime number is a natural number greater than 1 that has no positive divisors other  than 1 and itself. Example 2,3,5,7,11,13,17 Program-1 # Python code to check a given number is prime or not num = 15 if num > 1 :     for i in range ( 2 , num / / 2 ):         if (num % i) = = 0 :             print (num, "is not a prime number" )             break     else :         print (num, "is a prime number" )     else :     print (num, "is not a prime number" )    Program-2   # Python code to print all the prime numbers within an interval   low = 6   high = 51   print ( "Prime numbers between" , low , "&" , high , "are:" )    ...
Factorial: The factorial of a number is the product of all the integers from 1 to that number. Program-1   num=5   fact = 1   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):           fact = fact*i       print("The factorial of",num,"is",fact) Program-2 num=int(input("Enter any number:")) fact=1 while num:    fact=fact*num    num=num-1 print("factorial is ",fact) Program-3 # Factorial of a number using recursion   def fact(n):    if n == 0 or n==1:           return 1     else:        ...
Fibonacci sequence : A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8….. The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms. # Program-1 def fibo(n):     a, b = 0, 1     while a < n:         print(a, end=' ')         a, b = b, a+b     print() fibo(1600)  # Output: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 # Program-2 # Using Recursion def recfibo(n):      if n <= 1:          return n      else:          return(recfibo(n-1) + recfibo(n-2)) nums = 10 print("Fibonacci sequence:") for i in range(nums):          print(recfibo(i)) #Output: Fibonacci sequence: 0 1 1 2 3 5 8 13 21 ...