Sunday, 5 November 2017

Java Program to find class & Subnet mask based on IP address (CN)

import java.util.*;
class FindClass{
public static void main(String args[]){
System.out.println("Enter the IP address for recongization of the class");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String s1[]=s.split("\\.");
int a[]=new int[s1.length];
for(int i=0;i<s1.length;i++)
a[i]=Integer.parseInt(s1[i]);

int first=a[0];
String mask="";

if(s1.length==4 && first!=0){
if(first<=127){
System.out.println("Class A");
mask = "255.0.0.0";
System.out.println("The Subnet mask is: "+mask);
}
else if(first>=128 && first <=191){
System.out.println("Class B");
mask = "255.255.0.0";
System.out.println("The Subnet mask is: "+mask);
}
else if(first>=192 && first <=223){
System.out.println("Class C");
mask = "255.255.255.0";
System.out.println("The Subnet mask is: "+mask);
}
else if(first>=224 && first <=239){
System.out.println("Class D");
mask = "255.0.0.0";
System.out.println("The Subnet mask is: "+mask);
}
else if(first>=240 && first <=254){
System.out.println("Class E");
mask = "255.0.0.0";
System.out.println("The Subnet mask is: "+mask);
}
}
else
System.out.println("Invalid");
}

}

Output:

Enter the IP address for recongization of the class
192.168.0.1
Class C
The Subnet mask is: 255.255.255.0

Enter the IP address for recongization of the class
127.0.0.1
Class A
The Subnet mask is: 255.0.0.0

No comments:

Post a Comment