WRITE A PROGRAM TO CONVERT DECIMAL IP ADDRESS TO BINARY IP ADDRESS
#include<iostream.h>
#include<conio.h>
#include<string.h>
void dec_bin(int dec,int n[8])
{
int a,c,k=7;
a=dec;
while(a>0 && k>=0)
{
c=a%2;
n[k--]=c;
a=a/2;
}
while(k>=0)
{
n[k--]=0;
}
}
int main()
{ clrscr();
char wr[20];
int l=0,t=0;
int k[4]={0,0,0,0},no[4][8];
char str[36];
cout<<"Enter ip address:";
cin>>wr;
l=strlen(wr);
for(int i=0;i<l;i++)
{
if(wr[i]!='.')
k[t]=10*k[t]+(wr[i]-48);
else
t++;
}
dec_bin(k[0],no[0]);
dec_bin(k[1],no[1]);
dec_bin(k[2],no[2]);
dec_bin(k[3],no[3]);
t=0;
while(t<4)
{
for(int p=0;p<8;p++)
cout<<no[t][p];
cout<<" ";
t++;
}
getch();
return 0;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment