Monday, November 16, 2009

Write a program to enter an 8-bit binary number as an IP address and find the class of the address

Write a program to enter an 8-bit binary number as an IP address and find the class of the address.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>
void main()
{ int i,l;
char ch,num[8];
cout<<"\n Enter a 8-bit binary number:";
gets(num); // num=binary number
l=strlen(num); // l=number of bits in the binary number
for(i=0;i<l;i++)
{ ch=num[i];
if(!(ch=='0'||ch=='1'))
{ cout<<"\n NOT A BINARY NUMBER! \n"<<"Enter only 1 and 0.";
getch();
exit(0);
} }
if(l<=0)
{ cout<<"\n Enter the correct 8-bit binary number.\n";
cout<<"\n Underflow is occurred.\n";
getch();
exit(0);
}
else if(l<8)
{ cout<<"\n The number of bits are less than 8.\n";
cout<<"\n Please Enter all the 8-bits of the number.";
getch();
exit(0);
}
else if(l>8)
{ cout<<"\n Enter the correct 8-bit binary number.\n";
cout<<"\n Overflow is occurred.\n";
getch();
exit(0);
}
else
{cout<<"\n The entered number :"<<num<<"\n";
if(num[0]=='0')
cout<<"\n The number belongs to CLASS A.";
if(num[0]=='1'&&num[1]=='0')
cout<<"\n The number belongs to CLASS B.";
if(num[0]=='1'&&num[1]=='1'&&num[2]=='0')
cout<<"\n The number belongs to CLASS C.";
if(num[0]=='1'&&num[1]=='1'&&num[2]=='1'&&num[3]=='0')
cout<<"\n The number belongs to CLASS D.";
if(num[0]=='1'&&num[1]=='1'&&num[2]=='1'&&num[3]=='1')
cout<<"\n The number belongs to CLASS E.";
getch();
}
}

OUTPUT
1.
Enter a 8-bit binary number:111100
The number of bits are less than 8.
Please Enter all the 8-bits of the number.
2.
Enter a 8-bit binary number:01115656
NOT A BINARY NUMBER!
Enter only 1 and 0.
3.
Enter a 8-bit binary number:01111111
The entered number :01111111
The number belongs to CLASS A.
4.
Enter a 8-bit binary number:10111111
The entered number :10111111
The number belongs to CLASS B.
5.
Enter a 8-bit binary number:11011010
The entered number :11011010
The number belongs to CLASS C.
6.
Enter a 8-bit binary number:11101110
The entered number :11101110
The number belongs to CLASS D.
7. Enter a 8-bit binary number:11110000
The entered number :11110000
The number belongs to CLASS E.