Sunday, July 22, 2012

16 Bit BIN - HEX Converter in C


#include
#include
void hex16(char bin[16]);

int main()
{
char bin[16];
char hex[4];
printf("Enter 16 digit binary number : ");
scanf("%s", &bin);
hex16(bin);
return 0;
}

void hex16(char bin[16])
{
int i,k,j,dec1;
char hex[4];
int dec[4];
dec1=0;
j=0;
//printf("Entered 16 digit number : %s", bin);
for(i=0;i<4;i++)
{
dec[dec1]=0;
for(k=0;k<4;k++)
{
printf("%c",bin[j]);
if(bin[j]=='1')
{
switch (k)
{
case 0:
dec[dec1]=dec[dec1] + 8;
break;
case 1:
dec[dec1]=dec[dec1] + 4;
break;
case 2:
dec[dec1]=dec[dec1] + 2;
break;
case 3:
dec[dec1]=dec[dec1] + 1;
break;
}
}
j++;
}
printf(" : %d", dec[dec1]);
switch(dec[dec1])
{
case 0:
hex[i]='0';
break;
case 1:
hex[i]='1';
break;
case 2:
hex[i]='2';
break;
case 3:
hex[i]='3';
break;
case 4:
hex[i]='4';
break;
case 5:
hex[i]='5';
break;
case 6:
hex[i]='6';
break;
case 7:
hex[i]='7';
break;
case 8:
hex[i]='8';
break;
case 9:
hex[i]='9';
break;
case 10:
hex[i]='A';
break;
case 11:
hex[i]='B';
break;
case 12:
hex[i]='C';
break;
case 13:
hex[i]='D';
break;
case 14:
hex[i]='E';
break;
case 15:
hex[i]='F';
break;
}
printf("\n");
dec1++;
}
printf("Final Hex : %s", hex);
}

Output

Enter 16 digit binary number : 1100111100001010
1100 : 12
1111 : 15
0000 : 0
1010 : 10
Final Hex : CF0A

Note - Code is written in DevC++5.2.

Monday, April 11, 2011

A sea shore of Alibaug (Mandava Beach)

हा सागरी किनारा











Monday, April 4, 2011

JDBC Sample Code, Using DSN & MS-Access

import java.sql.*;
import java.io.*;

class Database
{
Connection con;
ResultSet rs;
Statement stmt;
BufferedReader br;
String str,temp;
Database()
{
try
{
con=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:ACCESS_DB");
stmt=con.createStatement();
str=new String();
temp=new String();
br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Connection Ok... Tested...");
}
catch(Exception E)
{
System.out.println("Connection Error : " + E.getMessage());
}
}
public void search()
{
System.out.println();
try
{
System.out.print("Enter string to be search : ");
str=br.readLine();
temp="Select * from Mobile Where Name Like '%" + str + "%' OR Num Like '%" + str + "%'";
System.out.println(temp);
rs=stmt.executeQuery(temp);
while(rs.next())
{
for(int i=1;i<=2;i++)
{
System.out.print(rs.getObject(i)+"\t");
}
System.out.println();
}
}
catch(Exception E)
{
System.out.println("SQL Error : " + E.getMessage());
}
}
public void add_new()
{
System.out.println();
String no,name;
try
{
System.out.print("Enter Mobile No : ");
no=br.readLine();
System.out.print("Enter Name : ");
name=br.readLine();
temp="Insert into Mobile Values('"+no+"','"+name+"')";
System.out.println(temp);
if(!stmt.execute(temp))
{
System.out.println("Record Successfully Inserted...");
}
else
{
System.out.println("Record Insertion failure...");
}
}
catch(Exception E)
{
System.out.println("SQL Error : " + E.getMessage());
}
}
public void select_all()
{
System.out.println();
try
{
rs=stmt.executeQuery("Select * from Mobile");
while(rs.next())
{
for(int i=1;i<=2;i++)
{
System.out.print(rs.getObject(i)+"\t");
}
System.out.println();
}
}
catch(Exception E)
{
System.out.println("SQL Error : " + E.getMessage());
}
}
public void con_close()
{
try
{
con.close();
}
catch(Exception E)
{
System.out.println("Connection Closing Error : " + E.getMessage());
}
}
public void update_record()
{
System.out.println("1.Search by Name");
System.out.println("1.Search by Number");
System.out.print("Enter your choice : ");
int ch1;
try
{
ch1=Integer.parseInt(br.readLine());
switch(ch1)
{
case 1:
System.out.print("Enter Name : ");
str=br.readLine();
System.out.print("Enter New Number : ");
temp="Update Mobile Set Num='"+br.readLine()+"' Where Name='"+str+"'";
System.out.println(temp);
if(!stmt.execute(temp))
{
System.out.println("Updation Successful...");
}
else
{
System.out.println("Updation Unsuccessful...");
}
break;

case 2:
System.out.print("Enter Number : ");
str=br.readLine();
System.out.println("Enter New Name : ");
temp="Update Mobile Set Name='"+br.readLine()+"' Where Num='"+str+"'";
System.out.println(temp);
if(!stmt.execute(temp))
{
System.out.println("Updation Successful...");
}
else
{
System.out.println("Updation Unsuccessful...");
}
break;

default:
System.out.println("Wrong Choice... Try Again...");
}
}
catch(Exception E)
{
System.out.println("Updation Error : "+E.getMessage());
}
}
public void delete_record()
{
System.out.println("1. Search by Name");
System.out.println("2. Search by Number");
System.out.print("Enter your choice : ");
int ch1;
try
{
ch1=Integer.parseInt(br.readLine());
switch(ch1)
{
case 1:
System.out.print("Enter Name to be deleted : ");
str=br.readLine();
temp="Delete From Mobile where Name='"+str+"'";
System.out.println(temp);
if(!stmt.execute(temp))
{
System.out.println("Deletion Successful...");
}
else
{
System.out.println("Deletion failure...");
}
break;

case 2:
System.out.print("Enter Number to be deleted : ");
str=br.readLine();
temp="Delete From Mobile where Num='"+str+"'";
System.out.println(temp);
if(!stmt.execute(temp))
{
System.out.println("Deletion Successful...");
}
else
{
System.out.println("Deletion failure...");
}
break;

default :
System.out.println("Wrong Choice...");
}
}
catch(Exception E)
{
System.out.println("Record Delection Error : " + E.getMessage());
}
}
}

public class DB_SAMPLE
{
public static void main(String args[])
{
Database db=new Database();
BufferedReader br;
int ch=0;
System.out.println("");
System.out.println("Mobile Contact Database for Personal Desktop Use");
System.out.println("");
System.out.println("");
System.out.println("1. See Entire Database");
System.out.println("2. Search");
System.out.println("3. Add new Record");
System.out.println("4. Delete");
System.out.println("5. Update");
System.out.print("Enter your Choice : ");
try
{
br=new BufferedReader(new InputStreamReader(System.in));
ch=Integer.parseInt(br.readLine());
}
catch(Exception E)
{
System.out.println("Console Reading Error : " + E.getMessage());
}

switch (ch)
{
case 1:
db.select_all();
break;

case 2:
db.search();
break;

case 3:
db.add_new();
break;

case 4:
db.delete_record();
break;

case 5:
db.update_record();
break;

default:
System.out.println("Wrong Choice... Try Again...");
}
db.con_close();
}
}

Friday, March 18, 2011

C++ problem

Hello Everybody I tried this code. Code gives 0 warning and 0 errors. I tested under Turbo C++ environment. My Question is How Constructor of all classes is called without creating an object? I know once constructor of class C is called it is automatically called upper class’s constructor. Basic question is how Constructor of class C is called with creating an object of that class?

**********CODE**********

#include<iostreams.h>

#include<conio.h>

#include<stdio.h>


class A

{

public :

A()

{

cout<<"A\n";

}

};


class B : public A

{

public :

B()

{

cout<<"B\n";

}

};


class C : public B

{

public :

C()

{

cout<<"C\n";

}

};


void main()

{

clrscr();

C();

getch();

}



**********OUTPUT**********


A

B

C

Sunday, February 27, 2011

FB chat as Text Message on your Mobile

Many times you may see my chat status is available. But mostly I m off-line. Your all IM send on my phone. So please understand this fact, if I am not replied on chat...

Now don't ask hows this possible. I m giving the answer.
Use yahoo messenger and register your mobile on it. And register your mobile for yahoo mobile contact. Then import all your facebook contact into that yahoo account. And done. You receive a text message from +5824119 or else.

*Tip :
1. This feature is available only for Yahoo India Messenger 11
2. You receive only 3 messages from a single contact at a single day...

Tuesday, December 21, 2010

Wednesday, November 17, 2010

TOEFL Links

Hello Friends,
I found some of the important links. These links will helpful for TOEFL. Please check out, it may help u lot. If you found it is good tell others. Else shut your mouth.


http://gotoefl.blogspot.com/

http://www.stuff.co.uk/toefl.htm

http://www.thetoefl.com/

http://www.ets.org/Media/Tests/TOEFL/exe/toeflSample.exe






Saturday, September 25, 2010

GRE Important Link

Hello Friends,
I found some of the important links. These links will helpful for GRE. Please check out, it may help u lot. If you found it is good tell others. Else shut your mouth.


http://gredownloads.blogspot.com/

http://gredownloads.blogspot.com/2008/07/gre-barrons-audio-wordlist-by-profm.html

http://rs13.rapidshare.com/files/101496851/advanced_english_dictionary.rar

http://www.achieverspoint.com/winoverwords.htm

http://www.blogcatalog.com/blog/gre-picture-dictionary

http://www.usingenglish.com/

http://www.enchantedlearning.com/alphabet/matchwordsandpix/

http://www.manythings.org/lulu/

http://allaboutgre.blogspot.com/2006/01/sample-essays.html

http://gre-download.blogspot.com/

http://www.greguide.com

http://www.online-languages.info/_ud2/dictionary.php?l1=english&l2=marathi

http://www.statementofpurpose.com/essay_samples.html

http://www.accesseducationindia.com/gre-engineering-ms.html

http://greghost.blogspot.com/2006/09/free-gre-downloads_05.html

http://grebag.blogspot.com/

http://www.codecoax.com/grerc/

http://www.info-gre.blogspot.com/

http://gre.complore.com/

http://gre.learnhub.com/

Saturday, May 8, 2010

NIS HMAC Implementation

#include
#include
#include

#define SIZE 64

typedef unsigned char uchar ;
int main ( int argc, char * argv[] )
{
FILE * kfp ;
FILE * ifp ;
FILE * ofp ;
FILE * tfp ;
uchar c[2] ;
uchar ch;
uchar key[SIZE] = { 0x00 } ;
uchar ipad[SIZE] ;
uchar opad[SIZE] ;
uchar command[100] ;
int i;

if ( 4 != argc )
{
printf( "Incorrect Usage!\n" ) ;
printf( "Correct Usage is ./hmac " ) ;
}
for ( i = 0; i < SIZE; i++ )
{
ipad[i] = 0x36 ;
opad[i] = 0x5C ;
}


kfp = fopen( argv[1], "r+b" ) ;
ofp = fopen( argv[3], "w+b" ) ;
tfp = fopen( "temp", "w+b" ) ;

fread( key, sizeof( uchar ), SIZE, kfp ) ;
fclose ( kfp );

for ( i = 0; i < SIZE; i++ )
{
key[i] = key[i] ^ ipad[i] ;
}

fwrite( key, sizeof( uchar ), SIZE, tfp ) ;
fclose( tfp ) ;

sprintf( command, "cat %s >> temp", argv[2]) ;
system( command ) ;
system( "./md5 temp > temp1" ) ;

kfp = fopen( argv[1], "r+b" ) ;
fread( key, sizeof( uchar ), SIZE, kfp ) ;
fclose ( kfp );
for ( i = 0; i < SIZE; i++ )
{
key[i] = key[i] ^ opad[i] ;
fwrite( &key[i], sizeof( uchar ), 1, ofp ) ;
}

tfp = fopen( "temp1", "r+b" ) ;
for ( i = 0; i < 16; i++ )
{
fread( c, sizeof( uchar ), 2, tfp );
c[0] -= '0' ;
if ( c[0] > 9 )
{
c[0] = c[0] + '0' - 'a' + 10 ;
}

c[1] -= '0' ;
if ( c[1] > 9 )
{
c[1] = c[1] + '0' - 'a' + 10 ;
}
ch = ( c[0] << 4 ) | c[1] ;
fwrite( &ch, sizeof( uchar ), 1, ofp ) ;
}
fclose( ofp ) ;

sprintf( command, "./md5 %s > temp", argv[3] ) ;
system( command ) ;
system( "cat temp" ) ;
printf( "\n" ) ;

return 0;
}

//OUTPUT:

root@root:~$ ./a.out key.txt hmac.c output.txt
7d1ef72ee877ec646f700288e0693462


Kaeyfile contents:
MHCVGFDC&^$$#@%^MHCVGFDC&^$$#@%^MHCVGFDC&^$$#@%^MHCVGFDC&^$$#@%^

NIS SDES Implementation

#include
#include

typedef unsigned short int uint16 ;
typedef unsigned char uchar;

#define BIT( b, n ) ( ( b >> n ) & 0x01 )

uint16 p10( uint16 );
uint16 p8( uint16 );
uint16 p4( uint16 ) ;
uint16 ip( uint16 );
uint16 ip_inv( uint16 );
uint16 ep( uint16 );
uint16 swap( uint16 );
void print( uint16 );
void genkeys( uint16, uint16*, uint16* );
uint16 encrypt( uint16, uint16, uint16) ;



uint16 S0[4][4] =
{
{ 1, 0, 3, 2 },
{ 3, 2, 1, 0 },
{ 0, 2, 1, 3 },
{ 3, 1, 0, 3 }
};
uint16 S1[4][4] =
{
{ 0, 1, 2, 3 },
{ 2, 0, 1, 3 },
{ 3, 0, 1, 0 },
{ 2, 1, 0, 3 }
};


void dispCorrectUsage()
{
printf( "Incorrect Usage!\n" ) ;
printf( "Correct Usage is:

Program for sum of series

//Program to calculate the sum of series

#include
#include
long int factorial(int n);
void main()
{
int n,i;
float s,r;
char c;
clrscr();
repeat : printf("\nYou have this series:- 1/1! + 2/2! + 3/3! + 4/4! ...");
printf("\n\nTo which term you want its sum? ");
scanf("%d",&n);
s=0;
for (i=1;i<=n;i++)
{ s=s+((float)i/(float)factorial(i)); }
printf("\nThe sum of '[%d]' terms is : %f",n,s);
fflush(stdin);
printf ("\n\nDo you want to continue?(y/n):- ");
scanf("%c",&c);
if (c=='y')
goto repeat;
getch();
}

long int factorial(int n)
{
if (n<=1)
return(1);
else
n=n*factorial(n-1);
return(n);
}

Pyramid of nos

//Pyramid of Numbers

#include
#include

int pyramid( int length )
{
clrscr();
int line = 1;
int j = 1, i, k = length;

while( length-- )
{
if( j == 1 )
{
for( i = 1; i <= length + 5; i++ )
printf( "" );
printf( "%d", j++ );printf("\n");
k--;
line++;
}

else if( i % 2 )
{
for( i = 1; i <= length + 5; i++ )
printf( "" );
for( i = 1; i <= line; i++ )
printf( "%d ", ( j++ % 10 ) );
printf( "\n" );
line++;
k--;
}

else
{
for( i = 1; i <= length + 5 ; i++ )
printf( "" );
for( i = 1; i <= line; i++ )
printf( "%d ", ( j++ % 10 ) );
printf( "\n" );
line++;
k--;
}
}

return 0;
}

int main( )
{
clrscr();
int length;

printf( "Enter the list length" );
scanf( "%d", &length );

pyramid( length );
getch( );
return 0;
}

Polynomial Multiplication

//Polynomial Multiplication

#include
#include
#include

typedef struct poly
{
int coeff,expo;
}P;
P p1[10],p2[10],p3[20]={0};
int t1,t2;

void multiply();

void main()
{
int i,ch;
char cho;
clrscr();

printf("Please enter number of terms in p1: ");
scanf("%d",&t1);
printf("\nPlease enter coeff. & expo of terms in p1\n");
for(i=0;i {
scanf("%d%d",&p1[i].coeff,&p1[i].expo);
}
printf("\n\nPlease enter number of terms in p2: ");
scanf("%d",&t2);
printf("\nPlease enter coeff. & expo of terms in p2\n");
for(i=0;i {
scanf("%d%d",&p2[i].coeff,&p2[i].expo);
}

do
{
clrscr();
printf("First polynomial is\n");
for(i=0;i<(t1-1);i++)
{
printf("(%d x^%d) + ",p1[i].coeff,p1[i].expo);
}
printf("(%d x^%d)",p1[i].coeff,p1[i].expo);
printf("\n\nSecond polynomial is\n");
for(i=0;i<(t2-1);i++)
{
printf("(%d x^%d) + ",p2[i].coeff,p2[i].expo);
}
printf("(%d x^%d)",p2[i].coeff,p2[i].expo);

printf("\n\n\tYour choices are:\n");
printf(" \t1:Multiplication\n \t2:Exit\n\n");
printf("\n\nPlease enter your choice: ");
scanf("%d",&ch);

switch(ch)
{
case 1:
multiply();
break;

case 2:
exit();

default:
printf("\n\nInvalid choice");
}
printf("\nPress y to continue and any other key to stop:");
fflush(stdin);
scanf("%c",&ch);
}
while(cho=='y' || cho=='Y');
getch();
}

void multiply()
{
int i=0,j=0,k=0,cnt=0,exp,coef,flag;
for(i=0;i<20;i++)
{
p3[i].coeff=0;
p3[i].expo=0;
}
i=0;
while(i {
j=0;
while(j {
flag=0;
coef=p1[i].coeff*p2[j].coeff;
exp=p1[i].expo+p2[j].expo;
for(cnt=0;cnt {
if(p3[cnt].expo==exp)
{
p3[cnt].coeff=p3[cnt].coeff+coef;
flag=1;
}
}
if(flag==0)
{
p3[k].expo=exp;
p3[k].coeff=coef;
k++;
}
j++;
}
i++;
}
printf("\nMultiplication is \n");
for(i=0;i<(k-1);i++)
{
printf("(%d x^%d) + ",p3[i].coeff,p3[i].expo);
}
printf("(%d x^%d)",p3[i].coeff,p3[i].expo);
}

Vowels Count

//Program to calculate frequency of vowels in a string

#include
#include
void main()
{
int a=0,e=0,i=0,o=0,u=0,sum=0;
char c;
clrscr();
printf("\nEnter string:- ");
printf("\nString will be terminated if you press Ctrl-Z & then ENTER.");
printf("\nSTRING:- ");
while ((c=getchar())!=EOF)
{
if (c=='a'||c=='A')
a=a+1;
if (c=='e'||c=='E')
e=e+1;
if (c=='i'||c=='I')
i=i+1;
if (c=='o'||c=='O')
o=o+1;
if (c=='u'||c=='U')
u=u+1;
}

sum=a+e+i+o+u;
printf("\n\nFrequency of vowel 'a' is %d.",a);
printf("\nFrequency of vowel 'e' is %d.",e);
printf("\nFrequency of vowel 'i' is %d.",i);
printf("\nFrequency of vowel 'o' is %d.",o);
printf("\nFrequency of vowel 'u' is %d.",u);
printf("\nTotal no. of vowels in the text is %d.",sum);
printf("\n\nHAVE A NICE DAY! BYE.");
getch();
}

Matrix Implementation

#include
#include
#include
int a[10][10],b[10][10],x[10][10];
void add()
{
int r,c,i,j,n;
cout<<"ENTER THE ORDER OF MATRIX : ";
cin>>n;
r=n;
c=n;
cout<<"ENTER THE ELEMENTS FOR MATRIX 'A' :"< for(i=0;i {
for(j=0;j {
cin>>a[i][j];
}
}
cout<<"ENTER THE ELEMENTS FOR MATRIX 'B' : "< for(i=0;i {
for(j=0;j {
cin>>b[i][j];
}
}
clrscr();

cout<<"MATRIX 'A' IS "< for(i=0;i {
for(j=0;j {
cout< }
cout< }

cout<<"MATRIX 'B' IS "< for(i=0;i {
for(j=0;j {
cout< }
cout< }
//.....................ADDITION OF MATRIX........................

for(i=0;i {
for(j=0;j {
x[i][j]=a[i][j]+b[i][j];
}
}

cout<<"THE SUM OF TWO ENTERED MATRIX IS "< for(i=0;i {
for(j=0;j {
cout< }
cout< }
}

void multiply()
{
int r1,c1,r2,c2,i,j,k;
clrscr();
cout<<"ENTER THE NO.OF ROWS OF MATRIX (A) : ";
cin>>r1;
cout<<"ENTER THE NO.OF COLOUMS OF MATRIX (A) :";
cin>>c1;
cout<<"ENTER THE ELEMENTS FOR MATRIX (A) :"< for(i=0;i {
for(j=0;j {
cin>>a[i][j];
}
}

cout<<"ENTER THE NO.OF ROWS OF MATRIX (B) : ";
cin>>r2;
cout<<"ENTER THE NO.OF COLOUMS OF MATRIX (B) : ";
cin>>c2;
cout<<"ENTER THE ELEMENTS FOR MATRIX (B) :"< for(i=0;i {
for(j=0;j {
cin>>b[i][j];
}
}

clrscr();
cout<<"MATRIX 'A' IS "< for(i=0;i {
for(j=0;j {
cout< }
cout< }

cout<<"MATRIX 'B' IS "< for(i=0;i {
for(j=0;j {
cout< }
cout< }

if(c1!=r2)
{
cout<<"MULTIPLICATION IS NOT POSSIBLE ";
}
else
{
for(i=0;i {
for(j=0;j {
x[i][j]=0;
for(k=0;k {
x[i][j]=x[i][j]+(a[i][k]*b[k][j]);
}
}
}
}

cout<<"MULTIPLICATION OF TWO MATRIX IS "< for(i=0;i {
for(j=0;j {
cout< }
cout< }
}

void main()
{
clrscr();
while(1)
{
int ch;
cout<<"

";
cout<<"...ENTER YOUR CHOICE........"<cout<<"1. ADDITION OF TWO MATRIX "<cout<<"2. MULTIPLICATION OF TWO MATRIX "<cout<<"3. EXIT "<cin>>ch;
switch(ch)
{
case 1:
add();
break;
case 2:
multiply();
break;
case 3:
exit(0);
default :
cout<<"INVALID CHOICE !! ENTER CORRECT CHOICE";
}
//getch();
}
}

LCM GCD implementation

//Finding LCM and GCD

#include
#include

void main()
{
int a[20],n,i,j,c,max,min;
unsigned long prod;
clrscr();
printf("Enter the no. of entries: ");
scanf("%d",&n);
printf("Enter the entries:");
for(i=0;i {
scanf("%d",&c);
if(c>0)
a[i]=c;
else
{
printf("Invalid Entry");
return;
}
}

max=a[0];
for(i=0;i if(a[i]>=max)
max=a[i];
min=a[0];
for(i=0;i if(a[i] min=a[i];

for(i=0,prod=1;i prod=prod*a[i];

for(i=max;i<=prod;i+=max)
{

c=0;
for(j=0;j if(i%a[j]==0)
c+=1;
if(c==n)
{ printf("The LCM of the nos: %d",i);
break;
}
}

for(i=min;i>0;i--)
{
if (min%i==0)
{
c=0;
for(j=0;j if(a[j]%i==0)
c+=1;
}
if(c==n)
{
printf("The GCD of the nos: %d",i);
break;
}
}
getch();
}

Factorial

//Factorial Function

#include
#include

long int factorial(int n);

void main()
{
int n,i;
float s,r;
char c;
clrscr();
repeat : printf("You have this series:- 1/1! + 2/2! + 3/3! + 4/4!..");
printf("To which term you want its sum? ");
scanf("%d",&n);
s=0;
for (i=1;i<=n;i++)
{
s=s+((float)i/(float)factorial(i));
}
printf("The sum of %d terms is %f",n,s);
fflush(stdin);
printf ("Do you want to continue?(y/n):- ");
scanf("%c",&c);
if (c=='y')
goto repeat;
getch();
}

long int factorial(int n)
{
if (n<=1)
return(1);
else
n=n*factorial(n-1);
return(n);
}