# 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… 😀

# Network programming – Using ‘PIPE’ to communicate between two processes

```#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
int main()
{
int desc;
if(pipe(desc)==-1)
{
perror("pipe failed"); exit(0);
}

if(!fork())
{
write(desc,"hello",6);
}
else{
char temp;
printf("\n%s\n",temp);
}
}```

# 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;
}```

# 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[' '];

{
int i,j;
char line;

fp=fopen(prodFile,"r");

for(i=0;fscanf(fp,"%s",line)==1;i++)
{
prod[i].left=line;
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]);
}

{
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)
{
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()
{
findTerminals();
if(Parse()) printf("Accepted");
else printf("InValid");

getch();
}```

# Introduction to C

Content of this Post:

• Introduction to C
• History of C
• Features of C
• Introduction to Programming
• Components of C programing
• Structure of C

.

Introduction to C:

What is C?

.                     C is a Computer Programming Language. It means , you can use C to create set of instructions that a computer can follow. C is probably the most popular language. C is a widely used programming language. C has won widespread acceptance because it gives programmers maximum control and efficiency. C is an easy language to learn. C is called as a compiled language.

.

History of C:

.                     C was developed at Bell Telephone Laboratories by Dennis Ritchie in the year 1970. C is the result two earlier languages called BCPL and B, Which were also developed at Bell Laboratories.

.                     Until 1978 C was restricted to use within Bell Laboratories. Later many computer professionals got impressed with the features of C and begun to use the language. My mid of 1980’s the popularity of C became widespread by a book written by Dennis Ritchie and Brain Kernighan. Many commercial software products were rewritten in C in-order to take its efficiency and its portability.

.                     ANSI, American National Standard Institute, developed a standardized version of C. C was invented to develop UNIX operating system.

.

Features of C:

C is a structured programming language:

C program has a specific structure, using which you can write programs.

C is very simple and easy to use:

C has got simple instructions and certain keywords. This makes our programming easier.

C encourages modularity:

C programs are written in modules. Modules are nothing but small set code performing a task. When a program is complex we split the program into modules , each performing a specific task.

C is highly portable:

In ‘C’ program written for one machine can be run on another machine without any or slight modification. C programs can be executed irrespective of the hardware environment.

C is case sensitive:

C is case sensitive, use lower-case for keywords and for the built-in functions.

C is an intermediate language:

All programming languages can be classified into two categories.

.

 High-level language or program level language: These are the languages, which are developed for the application programs to perform efficiently. Low-level language or machine level language: These are the languages, which are developed for the application machines to perform efficiently.

.                     C is neither a high-level language nor a low-level language. It is middle level language. Using ‘C’, we can write both system programs and application programs, which is not possible in most of the high level language.

.

Steps involved in executing a C program:

In order to run the program need to go through several stages. They are

1. Type the text of the program into the computer
2. Translate the program text into form the computer can use
3. Run the translated program

.

Source program:

The text of a program is known the source program. It is in the human readable form

.

Compilation:

The translation of the source program into machine code is known as compilation. It is performing by a special program known as a compiler. .

Components Of C Program:

The following are the components of the C program.

The header file consists of all the library functions. So the appropriate header file should be included in any C program.

.

Function:

Any C program should contain at-least one function (main). The execution of program starts from the main function.

.

Delimiters:

The delimiters are used to mark the beginning and the end of the program. It can also be used to mark the beginning and end of a statement block (set of statements). The symbol for delimiters is ‘{‘ and ‘}‘.

.

Statements:

Each line in a program is a statement.

.

Keywords:

Keywords are otherwise called as reserved words. They have predefined meaning in C. Keywords are used for specific purposes.

.

Comment Entry:

While writing a C program, Explanation about each and every program statements can be enclosed between /* and */. The comment statements are include in the program, so as to enable the user or anyone to easily understand the code. The statements between the symbol /* and */ are  not compiled.

For example

/*This is a sample program*/(this is a comment about a program)

.

Structure of C:

• Every C program must contain one or more functions. There should be at-least one function called main( ). The execution of the C program starts with main( ). The other functions can either follow or precede main( ) function.
• The statements within any function should be enclosed within the pair of braces{ }.

The opening brace ({ ) of the main( ) function indicates the starting of the program. The closing brace ( }) of the main( ) function indicates the closing of the program. The execution of the program stops on the occurrence of the closing brace of the main( ) function.