package codes;import java.util.Scanner;public class Subnet { public static void main(String args){ Scanner sc = new Scanner(System.in); System.out.print(“Enter the ip address: “); String ip = sc.nextLine(); String split_ip = ip.split(“\.”); //SPlit the string after every . String split_bip = new String4; //split binary ip String bip = “”; for(int i=0;i<4;i++){ split_bipi = appendZeros(Integer.toBinaryString(Integer.parseInt(split_ipi))); // "18" => 18 => 10010 => 00010010 bip += split_bipi; } System.out.println(“IP in binary is “+bip); System.out.print(“Enter the number of addresses: “); int n = sc.nextInt(); //Calculation of mask int bits = (int)Math.ceil(Math.log(n)/Math.log(2)); //eg if address = 120, log 120/log 2 gives log to the base 2 => 6.9068, ceil gives us upper integer System.out.println(“Number of bits required for address = “+bits); int mask = 32-bits; System.out.println(“The subnet mask is = “+mask); //Calculation of first address and last address int fbip = new int32; for(int i=0; i<32;i++) fbipi = (int)bip.charAt(i)-48; //convert cahracter 0,1 to integer 0,1 for (int i = 31; i > 31-bits; i–) fbipi &= 0; //Get first address by ANDing last n bits with 0 String fip = {“”,””,””,””}; for(int i=0;i<32;i++) fipi/8 = fipi/8+fbipi; System.out.print("First address is = "); for(int i=0;i<4;i++){ System.out.print(Integer.parseInt(fipi,2)); if(i!=3) System.out.print("."); } System.out.println(); int lbip = new int32; for(int i=0; i<32;i++) lbipi = (int)bip.charAt(i)-48; //convert cahracter 0,1 to integer 0,1 for (int i = 31; i > 31-bits; i–) lbipi |= 1; String lip = {“”,””,””,””}; for(int i=0;i<32;i++) lipi/8 = lipi/8+lbipi; System.out.print("Last address is = "); for(int i=0;i<4;i++){ System.out.print(Integer.parseInt(lipi,2)); if(i!=3) System.out.print("."); } System.out.println(); } static String appendZeros(String s){ String temp = "00000000"; return temp.substring(s.length())+ s; }}