Saturday, November 29, 2014

Reverse a Number & recursive function c++



Linear Search in C++ Program Example Code

#include
#include
     int main()
    {
cout<<"enter the size of array:   ";
int size;
cin>>size;
int array[size], key,i;
 taking input in array
 for(int j=0;j
{
 cout<<"enter "<
 cin>>array[j];
 }
your entered array is
 for(int a=0;a
{
{
     if(key==array[i])
{
  cout<<"key found at index number :  "<
  break;
   }
 }

if(i != size)
{
cout<<"key found at index :  "<
}
else
{
cout<<"key not found in array  ";
}
   return 0;
}

Reverse a Number in C++ Program Code
   
     #include
     #include
int main()
{
int number, reverse = 0;
cout<<"input a number to reverse and press enter: ";
 cin>> number; 
 taking input number in variable number
   for( ; number!= 0 ; )
   {
      reverse = reverse * 10;
      reverse = reverse + number%10;
      number = number/10;
   }
   cout<<"new reversed number is:  "<    return 0;
}

C++ program linear search program using recursive function

 #include
 #include
int recursivelinearsearch(int array[],int key,int size)
{
    size=size-1;
      if(size <0 span="">
{
      return -1;
      }
      else if(array[size]==key)
{
      return 1;
      }
      else
{
      return recursivelinearsearch(array,key,size);
      }
    }


    int main()
 {
    cout<<"enter the size of array:   ";
    int size;
    cin>>size;
    int array[size], key,i;
   taking input in array
    for(int j=0;j
{
    cout<<"enter "<
    cin>>array[j];
 }
   your entered array is
  for(int a=0;a
{
       cout<<"array[ "<  =  ";
       cout<
    }
    cout<<"enter key to search  in array";
    cin>>key;
    int result;
result=recursivelinearsearch(array,key,size--);
    if(result==1)
{
    cout<<"key found in array  ";
    }
    else
{
    cout<<"key not found in array  ";
    }
       return 0;
    }

No comments:

Post a Comment