Sunday, 21 December 2014

programs


                                          Fibonacci series


        In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the                     following sequence:
       1 , 1 , 2 , 3 , 5 , 13 , 18 ,21 , 34 ,55 , 89 , .....
      or (oftern in modern usage)
0  1 , 1 , 2 , 3 , 5 , 13 , 18 ,21 , 34 ,55 , 89 , ....
.in mathematical terms any term of fibonacci series is given by
a(n) = a(n-1) + a(n-2)
  • Non recursive c program to print fibonacci series
#include<stdio.h>
void fib(int n)
{
int a=1,b=1,s,i;
if(n==1)
{
printf(" %d ",a);
return;
}
if(n==2)
    {
    printf(" %d %d ",a,b);
    return;
    }
    printf(" %ld %d ",a,b);
for(i=2;i<n;i++)
{
s=a+b;
a=b;
b=s;
printf("%d ",s);
    }
}
main()
{
int num;
printf("Enter the number of terms : ");
scanf("%d",&num);
fib(num);
}
recursive c program to print fibonacci series
#include<stdio.h>
int fib(int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
return (fib(n-1)+fib(n-2));
}
main()
{
int i,num;
printf("Enter the number of terms : ");
scanf("%d",&num);
for(i=0;i<num;i++)
{
                       printf(" %d",fib(i));
             }
  }

Program for finding factorial of a large number


logic:- store the result in array with each element of array as a single digit
so we first multiply each element with num and then for each element we store them as the rightmost digit remains at it's location while the remaining part is added to the next element and the last element is again stored as single digit and the remaining is stored on next till remaining part becomes 0

#include<stdio.h>
void factorial(int num)
{
int temp,i,k=0,a[200]={0};
a[0]=num--;
while(num)
{
for(i=0;i<=k;i++)
a[i]*=num;
for(i=0;i<=k;i++)
{
temp=a[i]/10;
a[i]=a[i]%10;
a[i+1]=temp+a[i+1];
if(i==k)
{
temp=a[k+1];
while(temp>0)
{
k++;
a[k]=temp%10;
temp=temp/10;
}
i=k;
}
}
num--;
}
for(i=k;i>=0;i--)
printf("%d",a[i]);
}

main()
{
int num;
    printf("Enter the number : ");
    scanf("%d",&num);
    factorial(num);
}

 Function to check substring in a string

 int findstring(char *str,char *temp)
{
int i,j,len1,len2,valid=2;
len1=strlen(str);
len2=strlen(temp);
for(i=0;i<len1;i++)
{
valid=2;
if(str[i-1]==' ')
if(str[i]==temp[0])
for(j=0;j<len2;j++)
{
if(str[j+i]==temp[j])
{valid=1;}
else
{
valid=0;
break;
}
}
if(valid==1 && (str[i+len2]==' ' || str[i+len2]=='.'                    || str[i+len2]==',' || str[i+len2]=='\0'))
{
  
  return i;
   }
}
if(valid!=1)
  cout<<"\nstring not found ";
  return -1;
        }
}

function to insert a string temp into a              string str at index index


void insertstring(char *str,char *temp,int index)

{
   int j,i,len2,len1;
   len1=strlen(str);
   len2=strlen(temp);
   char temp2[100];
   strcpy(temp2,str+index);
   str[index]='\0';
   strcat(str+index,temp);
   str[index+len2]='\0';
   strcat(str," ");
   strcat(str,temp2);
}

Function to replace a substring(temp) of a string(str) with another substring(rep)


void replace(char *str,char *temp,char *rep)
{
 int index =findstring(str,temp);
int len2=strlen(temp);
char temp2[200];
strcpy(temp2,str+index+len2);
str[index]='\0';
strcat(str,rep);
strcat(str,temp2);
}

    Fuction to append a string 

VOID APPENDSTRING(char *str,char *strappend)
{

  strcat(str,strappend);
}



Q/A

1) Linear insertion sort has just correctly position x[60] in the following array x;
x= 20 40 60 30 10 50       show x after each of x[30] and x[10] is correctly positioned
2) In what situation will linear insertion sort make the fewest interchanges?
answer :
              1) when x[30] correcctly positioned x= 20 30 40 60 10 50
                      when x[10] correcctly positioned x= 10 20 30 40 60 50
                  2) when it is already sorted
Write a recursive java function that copies a binary tree. (Use java only , and please describe your code)
answer:  
              public BinarySearchTree<E> rcopy(){
                          BinarySearchTree<E> newTree = new BinarySearchTree<E>();
                         newTree.root = rcopy(root);
                         newTree.size=newTree.nodes();
                         return newTree;
                         }
                        private Entry <E> rcopy(Entry <E> current){
                       Entry <E> b=new Entry<E>();
                       if(current!=null){
                       if(current.left!=null)b.left=rcopy(current.left);
                       if(current.right!=null)b.right=rcopy(current.right);
                       b.element = current.element;
                       b.parent = successor(current);
                       }
                       return b;
                       }
1. A Boolean expression is one that is either:
   a. true or false 
   b.x or y
   c.Positive or negative
   d.None of these
answer: a