C++ Programming Blog

 
 
 

Image

 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <conio.h>

 int is_prime(const long);


 int main( )
    {
       clrscr( );

       fstream File(\"CP-23.txt\",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<\"\\n Unable to open the input file.\"<<endl;
         cout<<\"\\n Press any key to exit.\";

         getch( );
         exit(EXIT_FAILURE);
      }

       char Data[100]={NULL};

       do
      {
         strset(Data,NULL);

         File.getline(Data,80);

         if(strcmp(Data,NULL)==0)
        break;

         int length=strlen(Data);

         long largest_prime=0;

         char Number[15]={NULL};

         for(int i=1;i<=length;i++)
        {
           for(int j=0;j<=(length-i);j++)
              {
             strset(Number,NULL);

             for(int k=j,l=0;k<(i+j);k++,l++)
                Number[l]=Data[k];

             long number=atol(Number);

             if(is_prime(number))
                {
                   if(number>largest_prime)
                  largest_prime=number;
                }
              }
        }

         if(largest_prime!=0)
        cout<<largest_prime<<endl;

         else
        cout<<\"No Primes\"<<endl;
      }
       while(1);

       File.close( );

       getch( );
       return 0;
    }


 //-----------------------------  is_prime( )  ---------------------------//


 int is_prime(const long number)
    {
       for(long count=2;count<number;count++)
      {
         if((number%count)==0)
        return 0;
      }

       return 1;
    }