Ticker

6/recent/ticker-posts

Python Subjective question for competitive exam (2)

Q1.Program to find factorial using recursion

>>> def factorial(x): if(x==1): return 1 else: return(x*factorial(x-1)) >>> print(factorial(5)) 120

Q2.Program to find gcd of two number

>>> def gcd(p,q): if q==0: return p else: return gcd(q,p%q) >>> print(gcd(6,3)) 3



Q3.Program to print fibonacci numbers range(1,6)

>>> def fibn(n): if n==1: return 0 elif n==2: return 1 else: return fibn(n-1)+fibn(n-2) >>> for i in range(1,6): print(fibn(i)) 0 1 1 2 3

Q4.Program to find number is prime or not

>>> def prime(n): for i in range(2,n): if(n%i==0): print("number is not prime") break else: print("prime") >>> prime(5) prime >>> prime(4) number is not prime

Q5.program to find cube of each list number

>>> def cube(arr): for i in range(0,len(arr)): print(arr[i],arr[i]*arr[i]*arr[i]) >>> cube([2,3,4,5,6]) 2 8 3 27 4 64 5 125



Post a Comment

0 Comments