Skip to main content

Posts

9. BINARY SEARCH:python program, to search a number without using recursion

def binsearch(ar,key):     low=0     high=len(ar)-1     while low<=high:         mid=int((low+high)/2)         if key==ar[mid]:             return mid         elif key<ar[mid]:             high=mid-1         else:             low=mid+1     else:         return-263256 #main program ra=[12,36,45,58,65,95,99] item=int(input('enter the number to be searched:-')) res=binsearch(ra,item) if res>=0:     print(item,'found at index',res) else:     print('SORRY',item,'NOT FOUND IN ARRAY')

6. Program for binary search using recurison

#program for the binary search def binary(seq,item,l,u):       if l>u:             return -55       mid=int((l+u)/2)       if item== seq[mid] :             return(mid)       elif item< seq[mid] :             u=mid-1             binary(seq,item,l,u)       elif item> seq[mid] :             l=mid+1             binary(seq,item,l,u)       else:             print('you entered wrong number') series=[5,10,12,15,22,28,44,78,89,99,121,143,178,199,231] print('your series is:',series) n=len(series) number=int(input('enter the number from the series to be searched')) result=binary(series,number,0,n-1) if result < 0:       print('you entered negative value')...

5. Python Program to print the sum to factorial of numbers

# write a program to calculate the sum of the factorial of a number. n=int(input('enter the POSITIVE no for which you want factorial')) s=0 if n==0:       print('the factorial of 0 = 1') if n==1:       print('the factorial of 1 = 1') if n>=2:     c=1     for i in range(1,n+1):           for j in range (i,0,-1):                 c=c*j                 s=s+c     print('The factorial of ',n,'is',c) else:       print('You entered wrong value')

4. write a program to find the factorial of any no. in python

3. Certificate for various subjects