The number which is prime and also palindrome.


After many days of gap…  back to blogging…….

Q.) An integer is said to be a palindrome if it is equal to its reverse. For example, 79197 and 324423 are palindromes. In this task you will be given an integer N, 1 ≤ N ≤ 1000000. You must find the smallest integer M ≥ N such that M is a prime number and M is a palindrome. For example, if N is 31 then the answer is 101.

Input format
A single integer N, (1 ≤ N ≤ 1000000), on a single line.
Output format
Your output must consist of a single integer, the smallest prime palindrome greater than or equal to N

Sol.)

#include<stdio.h>
void main()
{
   long int a;
   printf("Enter number: ");
   scanf("%li",&a);
   if(a <= 1000000)
    while(1)
    {
     if(palindrome(a)){
     if(prime(a)) {printf("Number is: %d",a);getch();return;}
    }
  a=a+1;
}
else printf("Plz enter number less than 1000000 next time");
getch();
}
int prime(long int a){
long int i;
for(i=2;i<=a/2;i++) if(a%i==0)return 0;

return 1;
}

int palindrome(long int a){
  long int temp=a,reverse=0;
 while( temp != 0 ){
  reverse = reverse * 10;
  reverse = reverse + temp%10;
  temp = temp/10;
 }
  if(reverse == a) return 1;
  else return 0;
}

Shivaji varma… 😀

Recursive Descent Parsing


The following c program parser the given string for the given grammar::

E->E+T

E->T

T->T*F

T->F

F->(E)

F->i


Before writing writing recursive descent parser first eliminate left recursion and left factoring.

E->TEplus

Eplus->+TEplus / ∈ (epsilon)

T ->FTplus

Tplus->*FTplus / ∈ (epsilon)

F-> (E)

F-> i


#include<stdio.h>
#include<string.h>
char string[' '];
int len;
int pos=0;
int E();
int Eplus();
int T();
int Tplus();
int F();
int main()
{
printf("Enter the string:: ");
scanf("%s",&string);
len=strlen(string);
if(E()==0) printf("\nNot Accepted");
else if(pos<len) {printf("\nNot Accepted");}
else printf("\nAccepted");
printf("\n");
return 0;
}
int E()
{
if(T()==0) return 0;
if(Eplus()==0) return 0;
return 1;
}
int Eplus()
{
if(string[pos]=='+')
{ pos++;
if(T()==0) return 0;
if(Eplus()==0) return 0;
}
return 1;
}
int T()
{
if(F()==0) return 0;
if(Tplus()==0) return 0;
return 1;
}
int Tplus()
{
if(string[pos]=='*')
{ pos++;
if(F()==0) return 0;
if(Tplus()==0) return 0;
}
return 1;
}
int F()
{
if(string[pos]=='('){
pos++;
if(E()==0) return 0;
if(string[pos]==')') pos++;
else return 0;
}
else if(string[pos]=='i'){
pos++;}
else {return 0;}
return 1;
}

Don’t copy the code directly from this page it may contain HTML meta content.

Just download this text file(code)>>>>Download<<<<

Operator precedence parser


#include<stdio.h>
#include<string.h>

FILE *fp;
char prodFile[]="test.txt";
char relFile[]="table.txt";
int numProductions,numTerminals,top=0,terTop=0,ip=0;
int relationTable[' '][' '];
char stack[' ']="$";
char inputBuffer[' '];
char string[' '];
char handle[' '],handTopTer;
char term[' '];

struct
{
  char left;
  char right[' '];
}prod[' '];

void readProductions()
{
  int i,j;
  char line[30];

 fp=fopen(prodFile,"r");

  for(i=0;fscanf(fp,"%s",line)==1;i++)
  {
    prod[i].left=line[0];
    for(j=2;line[j]!='';j++)
       prod[i].right[j-2]=line[j];

      prod[i].right[j-2]='';
  }

  numProductions=i;

    printf("Productions::\n");
  for(i=0;i<numProductions;i++){
   printf("%c>",prod[i].left);
   printf("%s\n",prod[i].right);
   }
}

int validateNonTer(char z)
{
int i;
   for(i=0;i<numProductions;i++)
   {
     if(z==prod[i].left) {return 1;}
   }
   return 0;
}

void findTerminals()
{
int i,j;
numTerminals=0;

   for(i=0;i<numProductions;i++)
     for(j=0;prod[i].right[j]!='';j++)
        if(!validateNonTer(prod[i].right[j])) 
           {term[numTerminals]=prod[i].right[j];numTerminals++;}

    printf("\nTerminals::\n");
    for(i=0;i<numTerminals;i++)
      printf("%c\n",term[i]);
}

void readRelationTable()
{
   int i,j,k=0;
   fp=fopen(relFile,"r");
    for(i=0;i<=numTerminals;i++)
    for(j=0;j<=numTerminals;j++)
    {
      fscanf(fp,"%d",&relationTable[i][j]);
    }

  printf(" ");

  for(i=0;i<=numTerminals;i++)
  if(i==numTerminals)  printf(" $");
   else printf("%2c ",term[i]);

  for(i=0;i<=numTerminals;i++)
  {
   if(numTerminals==i) printf("\n$");
    else printf("\n%c",term[i]);

    for(j=0;j<=numTerminals;j++)  
         printf("%2d ",relationTable[i][j]);
  }

}

int searchProd(char z[50])
{
  int i;

  for(i=0;i<numProductions;i++)
    if(strcmp(z,prod[i].right)==0) return i;

return -1;
}

int pos(char z)
{
int i;

  for(i=0;i<numTerminals;i++)
   if(term[i]==z) {return i;}
    if(z=='$') return i;
     else return -1;

}

void push(char z)
{
top++;
stack[top]=z;
if(pos(stack[top]) != -1) terTop=top;
printf("push::%c\n",z);
}

void terToprec()
{
terTop--;
while(pos(stack[terTop])== -1) terTop--;
}

char pop()
{
if(pos(stack[top])!= -1) 
        {terToprec();handTopTer=stack[top];}
char temp=stack[top];
printf("pop::%c\n",temp);
top--;
return temp;
}

void reverse(char *r)
{
int len=strlen(r),i;
  for(i=0;i<len/2;i++)
  {
   char temp=r[i];
   r[i]=r[len-1-i];
   r[len-1-i]=temp;
  }
}

int Parse()
{
  int i;
  char a,b;

   printf("\n\nEnter string::");
   scanf("%s",string);

for(i=0;string[i]!='';i++)
  inputBuffer[i]=string[i];

  inputBuffer[i]='$';

  while(1)
  {
    if(stack[terTop]=='$' && inputBuffer[ip]=='$') return 1;

   else {
     a=stack[terTop];
     b=inputBuffer[ip];
     printf("\na::%c  b::%c\n",a,b);
     if(relationTable[pos(a)][pos(b)]== 1 ||
relationTable[pos(a)][pos(b)]==0)
     {push(b);ip++;}

     else if(relationTable[pos(a)][pos(b)]==2)
       {
        int k=0;
         handle[k]=pop();
         if(pos(handle[k])== -1) {k++;handle[k]=pop();}
          k++;

  while(relationTable[pos(stack[terTop])][pos(handTopTer)] 
       != 1)
          {handle[k]=pop();k++;}

          while(terTop!=top) {handle[k]=pop();k++;}

          handle[k]='';
          reverse(handle);

          printf("Handle  %s\n",handle);

          if(searchProd(handle)!=-1)
            push(prod[searchProd(handle)].left);
          else return 0;
     }
   else return 0;
  }
  }
}

int main()
{
  readProductions();
  findTerminals();
  readRelationTable();
   if(Parse()) printf("Accepted");
   else printf("InValid");

getch();
}

Don’t copy the code directly from this page it may contain HTML meta content.

Just download this text file(code)>>>>Download<<<<