Tuesday, December 9, 2014

Digit lucky number and Linear search or Sequential in C++ Programming



Calculate anybody’s single digit lucky number in C++ Programming

 # include 
 # include 
 # include <string.h>
 # include 
 # include 
 # include 
 int main( )
    {
       clrscr( );
       fstream File("CP-07.txt",ios::in|ios::nocreate);
       if(!File)
      {
         cout<<"Unable to open the input file."<
         cout<<"Press any key to exit."<
         getch( );
         exit(exit_failure);
      }
       char Data[100]={NULL};
       do
      {
         strset(Data,NULL);
         File.getline(Data,80,'\n');
         if(strcmpi(Data,"0")==0)
        break;
         int flag=0;
         int name_length=strlen(Data);
         int code_array[100]={0};
         for(int count_1=0;count_1
        {
           if(!isalpha(Data[count_1]))
              {
             flag=1;
 
             break;
              }
           else
              {
             Data[count_1]=toupper(Data[count_1]);
             code_array[count_1]=(int(Data[count_1])-64);
              }
        }
         if(!flag)
        {
           int lucky_number=0;
           for(int count_2=0;count_2
              lucky_number+=code_array[count_2];
           char lucky_number[100]={NULL};
           while(lucky_number>9)
              {
             strset(Lucky_number,NULL);
             itoa(lucky_number,Lucky_number,10);
             lucky_number=0;
             int number_length=strlen(Lucky_number);
             char Digit[5]={NULL};
             for(int count_3=0;count_3
                {
                   Digit[0]=Lucky_number[count_3];
                   lucky_number+=atoi(Digit);
                }
              }
           cout<
        }
         else
        cout<<"invalid"<
      }
       while(1);
       File.close( );
       getch( );
       return 0;
    }

Search an element in an array using Linear search or Sequential Search in C++ Programming

 # include 
 # include 
 int linear_search(int [],int,int);
 int main( )
    {
       clrscr( );
       constint array_size=10;
       int array[array_size]={25,36,2,48,0,69,14,22,7,19};
       cout<<" *******************************************"<
       cout<<" **************  Linear Search**************"<  
       cout<<" *******************************************"<
       gotoxy(1,24);
       cout<<" ******************************************"<
                 cout<<" ********************************************";
       gotoxy(1,5);
       cout<<" The contents of the array are : "<
       cout<<"\n     Elements :"<<"\t\t     Value:"<
       for(int count=0;count
      {
         cout<<"\t"<<" array ["<"]"<<"\t\t";
         cout<
      }
       int searching_element=0;
       int flag=0;
       cout<<"\n\n Enter the element you want to find  =  ";
       cin>>searching_element;
       flag=linear_search(array,array_size,searching_element);
       if(flag!=-1)
      cout<<"\n The given element is found at the position : array["<<
       flag<<"]";
       else
      cout<<"\n The given element is not found. ";
       getch( );
       return 0;
    }
/*************************************************************************//***********************  linear_search(int [],int,int)  *****************//*************************************************************************/
int linear_search(int array[],int array_size,int element)
    {
       int flag=-1;
       for(int count=0;count
      {
         if(element==array[count])
        {
           flag=count;
           break;
        }
      }
       return flag;
    }

Code for Program to solve the Towers of Hanoi Problem in C++ Programming

# include 
          # include 
 void move(constint n,constint fromtower, constint totower,constint sparetower)                
 {
    if(n>0)
    {
       move((n-1),fromtower,sparetower,totower);
       cout<<"\t Move the Top Disk from Tower-"<
         <<" to tower-"<"\n";
       move((n-1),sparetower,totower,fromtower);
    }
 }
 int main( )
 {
    clrscr( );
    cout<<"\n\t **************   Towers of hanoi   **************\n"<
    cout<<"\t The Mystery of Towers of Hanoi is as follows : \n"<
    move(4,1,3,2);
    cout<<"\n\t *******************************************";
    getch( );
    return 0;
 }
 

No comments:

Post a Comment