Tag Archives: programs

Computerized Crime Register:

<br />#include

#include
#include
#include
#include
#include using namespace std;
class project
{
public:
char crime[20];
char name[20];

void getdata()
{
cout << "\nEnter name= "; cin>>name;

cout << "Enter the crime= "; cin>>crime;
}

void showdata()
{
cout << "\nName= " << name << endl ;
cout << "crime= " << crime<<endl;
}
void writedata()
{
ofstream fout("crime.txt", ios::app | ios::out);
project obj;
obj.getdata();
fout.write((char*)&obj, sizeof(obj));
fout.close();
}
};
int main()
{
project obj;
int num, input, flag = 0;
char pname[20],pcrime[20];
lable:
system("cls");
cout << "1.Add data\n";
cout << "2.Search by name\n";
cout << "3.Search by crime\n";
cout << "4.See all data\n";
cout << "5.Exit\n";
cout << "Enter choice: "; cin >> input;
ifstream fin("crime.txt",ios::in);
switch(input)
{ //adding new data
case 1:
obj.writedata();
fin.close();
goto lable;
case 2: //searching by name
cout << "\nEnter name= "; cin>>pname;
while (fin.read((char*)&obj,sizeof(obj)))
if (strcmp(pname,obj.name)==0)
{
obj.showdata();
flag = 1;
}
fin.close();
if (flag == 0)
cout << "\n\nDATA NOT FOUND!!!";
getch();
goto lable;
case 3: //searching by crime
cout << "\nEnter crime= "; cin>>pcrime;
while (fin.read((char*)&obj, sizeof(obj)))
if (strcmp(pcrime,obj.crime)==0)
{
obj.showdata();
flag = 1;
}
fin.close();
if (flag == 0)
cout << "\n\nDATA NOT FOUND!!!";
getch();
goto lable;
case 4: //show the list of all data
cout << "\nList of data::\n";
while (fin.read((char*)&obj, sizeof(obj)))
{
obj.showdata();
cout << endl;
flag = 1;
}
fin.close();
if (flag == 0)
cout << "\n\nDATA NOT FOUND!!!";
getch();
goto lable;
case 5:
exit(0);
default:
goto lable;
}
}

output:

c1 c2 c3c4

Integer To English Word Converter :

<br />#include<iostream>
using namespace std;
int main()
{
int a,c,i=0;
int b[6];
cout<<"Enter the number=";
cin>>a;
if(a==0)
{
cout<<"Zero";
exit;
}
while(1)
{
b[i]=a%10;
a=a/10;
if(a==0)
{
break;
}
i++;
}
while(i>0)
{
if(i==5)
{
switch(b[i])
{
case 1:
{
cout<<"one lakh ";
i--;
break;
}
case 2:
{
cout<<"two lakh ";
i--;
break;
}
case 3:
{
cout<<"three lakh ";
i--;
break;
}
case 4:
{
cout<<"four lakh ";
i--;
break;
}
case 5:
{
cout<<"five lakh ";
i--;
break;
}
case 6:
{
cout<<"six lakh ";
i--;
break;
}
case 7:
{
cout<<"seven lakh ";
i--;
break;
}
case 8:
{
cout<<"eight lakh ";
i--;
break;
}
case 9:
{
cout<<"nine lakh ";
i--;
break;
}
default:
{
i--;
break;
}
}
}
if(i==4)
{
if(b[3]==0)
{
switch(b[4])
{
case 1:
{
cout<<"ten thousand ";
i--;
break;
}
case 2:
{
cout<<"twenty thousand ";
i--;
break;
}
case 3:
{
cout<<"thirty thousand ";
i--;
break;
}
case 4:
{
cout<<"forty thousand ";
i--;
break;
}
case 5:
{
cout<<"fifty thousand ";
i--;
break;
}
case 6:
{
cout<<"sixty thousand ";
i--;
break;
}
case 7:
{
cout<<"seven thhousand ";
i--;
break;
}
case 8:
{
cout<<"eight thousand ";
i--;
break;
}
case 9:
{
cout<<"ninty thousand ";
i--;
break;
}
default:
{
i--;
}
}
}
else
{
if(b[4]==1)
switch(b[3])
{
case 1:
{
cout<<"eleven thousand ";
i--;
i--;
break;
}
case 2:
{
cout<<"tweleve thousand ";
i--;
i--;
break;
}
case 3:
{
cout<<"thirteen thousand";
i--;
i--;
break;
}
case 4:
{
cout<<"fourteen thousand ";
i--;
i--;
break;
}
case 5:
{
cout<<"fifteen thousand ";
i--;
i--;
break;
}
case 6:
{
cout<<"sixteen thousand ";
i--;
i--;
break;
}
case 7:
{
cout<<"seventeen thousand ";
i--;
i--;
break;
}
case 8:
{
cout<<"eighteen thousand ";
i--;
i--;
break;
}
case 9:
{
cout<<"nineteen thousand ";
i--;
i--;
break;
}
default:
{
i--;
break;
}
}
else
{
switch(b[4])
{
case 2:
{
cout<<"twenty ";
i--;
break;
}
case 3:
{
cout<<"thirty ";
i--;
break;
}
case 4:
{
cout<<"forty ";
i--;
break;
}
case 5:
{
cout<<"fifty ";
i--;
break;
}
case 6:
{
cout<<"sixty ";
i--;
break;
}
case 7:
{
cout<<"seventy ";
i--;
break;
}
case 8:
{
cout<<"eighty ";
i--;
break;
}
case 9:
{
cout<<"ninty ";
i--;
break;
}
default:
{
i--;
break;
}
}
}
}
}
if(i==3)
{
switch(b[3])
{
case 1:
{
cout<<"one thousand ";
i--;
break;
}
case 2:
{
cout<<"two thousand ";
i--;
break;
}
case 3:
{
cout<<"three thousand ";
i--;
break;
}
case 4:
{
cout<<"four thousand ";
i--;
break;
}
case 5:
{
cout<<"five thousand ";
i--;
break;
}
case 6:
{
cout<<"six thousand ";
i--;
break;
}
case 7:
{
cout<<" seven thousand ";
i--;
break;
}
case 8:
{
cout<<"eight thousand ";
i--;
break;
}
case 9:
{
cout<<"nine thousand ";
i--;
break;
}
default:
{
i--;
break;
}
}
}
if(i==2)
{
switch(b[2])
{
case 1:
{
cout<<"one hundred ";
i--;
break;
}
case 2:
{
cout<<"two hundred ";
i--;
break;
}
case 3:
{
cout<<"three hundred ";
i--;
break;
}
case 4:
{
cout<<"four hundred ";
i--;
break;
}
case 5:
{
cout<<"five hundred ";
i--;
break;
}
case 6:
{
cout<<"six hundred ";
i--;
break;
}
case 7:
{
cout<<"seven hundred ";
i--;
break;
}
case 8:
{
cout<<"eight hundred ";
i--;
break;
}
case 9:
{
cout<<"nine hundred ";
i--;
break;
}
default:
{
i--;
break;
}
}
}
if(i==1)
{
if(1)
{
switch(b[1])
{
case 1:
{
if(b[0]==0)
{
cout<<"ten ";
i--;
}
break;
}
case 2:
{
cout<<"twenty ";
i--;
break;
}
case 3:
{
cout<<"thirty ";
i--;
break;
}
case 4:
{
cout<<"forty ";
i--;
break;
}
case 5:
{
cout<<"fifty ";
i--;
break;
}
case 6:
{
cout<<"sixty ";
i--;
break;
}
case 7:
{
cout<<"sevety ";
i--;
break;
}
case 8:
{
cout<<"eighty ";
i--;
break;
}
case 9:
{
cout<<"ninty ";
i--;
break;
}
default:
{
i--;
break;
}
}
}
if(b[1]==1)
{
switch(b[0])
{
case 1:
{
cout<<"eleven ";
i--;
break;
}
case 2:
{
cout<<"twelve ";
i--;
break;
}
case 3:
{
cout<<"thirteen ";
i--;
break;
}
case 4:
{
cout<<"forteen ";
i--;
break;
}
case 5:
{
cout<<"fifteen ";
i--;
break;
}
case 6:
{
cout<<"sixteen ";
i--;
break;
}
case 7:
{
cout<<"seventeen ";
i--;
break;
}
case 8:
{
cout<<"eighteen ";
i--;
break;
}
case 9:
{
cout<<"nineteen ";
i--;
break;
}
default:
{
i--;
break;
}
}
}
}
if(i==0&&b[1]!=1)
{
switch(b[0])
{
case 1:
{
cout<<"one";
i--;
break;
}
case 2:
{
cout<<"two";
i--;
break;
}
case 3:
{
cout<<"three";
i--;
break;
}
case 4:
{
cout<<"four";
i--;
break;
}
case 5:
{
cout<<"five";
i--;
break;
}
case 6:
{
cout<<"six";
i--;
break;
}
case 7:
{
cout<<"seven";
i--;
break;
}
case 8:
{
cout<<"eight";
i--;
break;
}
case 9:
{
cout<<"nine";
i--;
break;
}
default:
{
i--;
break;
}
}
}
else
{
i--;
break;
}
}
return 0;
}

output:-

capture
Output

Program that determines the number of trailing zeros at the end of X! (X factorial), where X is an arbitrary number.

<br />#include<iostream>
using namespace std;
int main()
{
long long a,b=1;
cout<<“Enter the number whose factorial you want to calculate=”;
cin>>a;
if(a==0)
{
cout<<endl<<“The factorial of the given number is=1″<<endl;
}
else
{

while(a>1)
{
b=b*a;
a–;
}

}
cout<<“The factorial of the given number is=”<<b<<endl;
a=0;
while(1)
{
if(b%10==0)
{
b=b/10;
a++;
}
else
{
break;
}
}
cout<<“The number of the trailing zeroes in the given factorial is=”<<a;
return 0;
}

Lex : Check Valid Email

%{
#include<stdio.h>
#include<stdlib.h>
int flag=0;
%}
%%
[a-z . 0-9]+@[a-z]+".com"|".in" { flag=1; }
%%
int main()
{
yylex();
if(flag==1)
printf("Accepted");
else
printf("Not Accepted");
}

/*
Enter input
press Enter & ctrl+d to get output
*/

Output :-
abc@gmail.com
Accepted

Lex : Number is prime or not

%{
#include<stdio.h>
#include<stdlib.h>
int flag,c,j;
%}
%%
[0-9]+ {c=atoi(yytext);}
%%
int main()
{
yylex();
if(c==2)
{
printf("\n Prime number");
}
else if(c==0 || c==1)
{
printf("\n Not a prime number");
}
else
{
for(j=2;j<c;j++)
{
if(c%j==0)
flag=1;
}
if(flag==1)
printf("\n Not a prime number");
else if(flag==0)
printf("\n Prime number");
}
}

Conversion of arithmatic expression into postfix expression for given grammar

Grammars Are
E=E+T      E=T    T=T*F    T=F    F=NUM

// ( .y  File )

%{
#include<stdlib.h>
#include<stdio.h>
void yyerror(char *s);
%}
%token NUM
%%
E : E '+' T {printf("+");}
| T ;
T : T '*' F {printf("*");}
| F ;
F : NUM {printf("%d",yylval);};
%%
int main()
{
yyparse();
}
void yyerror(char *s)
{
fprintf(stdout,"\n%s",s);
}

// (.l File )

%{
#include<stdio.h>
#include<stdlib.h>
#include"y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext);return NUM;}
[ \t] {;}
\n return 0;
. return yytext[0];
%%

/* To Compile :-
1) yacc -d filename.y
2) lex filename.l
3) gcc lex.yy.c y.tab.c -ll
4) ./a.out
Enter Expression like:  1+2*3
*/

Lex : Check Whether number is Even or Odd

%{
#include<stdio.h>
#include<stdlib.h>
int i;
%}
%%
[0-9]+ {i=atoi(yytext); if(i%2==0) printf("Even !"); else printf("Odd !");};
%%
int main()
{
yylex();
}
/*  To Compile :-
1) lex filename.l
2) gcc lex.yy.c -ll
3) ./a.out
Enter Input and press enter
*/

Output :-
145
Odd !
204
Even !

Conversion from binary to decimal

<br />//(Yacc File : Save as .y)

//to compile and Run
//:-> yacc -d FileName.y
//:-> lex FileName.l
//:-> gcc lex.yy.c y.tab.c -ll
//:-> ./a.output
//:-> enter input as - 101
//:-> output:-> 5

%{
#include<stdio.h>
#include<stdlib.h>
void yyerror(char *s);
%}
%token ZERO ONE
%%
N: L {printf("\n%d",$$);}
L: L B {$$=$1*2+$2;}
| B {$$=$1;}
B:ZERO {$$=$1;}
|ONE {$$=$1;};
%%
int main()
{
while(yyparse());
}
yyerror(char *s)
{
fprintf(stdout,"\n%s",s);
}

// (Lex File : save as .l)

%{
#include<stdio.h>
#include<stdlib.h>
#include"y.tab.h"
extern int yylval;
%}
%%
0 {yylval=0;return ZERO;}
1 {yylval=1;return ONE;}

[ \t] {;}
\n return 0;
. return yytext[0];
%%

Output :-
101
5