You are on page 1of 5

DIGITAL ASSIGNMENT 3

BCSE308P
COMPUTER NETWORKS

Name: Harshil Nanda


Reg No: 21BCE0918
1.Write a C/C++/Python/Java program to read a
classful address in a block as input. (like
180.8.17.9). Display the number of addresses in
the block, the first address, and the last address.

Code:

def calculate_address_range(ip_address):
# Split the IP address into octets
octets = ip_address.split('.')

# Calculate the number of addresses


num_addresses = 256 ** (4 - len(octets))

# Calculate the first address


first_address = '.'.join(octets[:4 - len(octets)] + ['0'] * len(octets))

# Calculate the last address


last_address = '.'.join(octets[:4 - len(octets)] + ['255'] * len(octets))

return num_addresses, first_address, last_address

# Read the IP address from the user


ip_address = input("Enter the IP address: ")
# Calculate the address range
num_addresses, first_address, last_address =
calculate_address_range(ip_address)

# Display the results


print("Number of addresses in the block:", num_addresses)
print("First address:", first_address)
print("Last address:", last_address)

Output:
2.Write a C/C++/Python/Java program to read a
classless address in a block as input. (like
x.y.z.t/n). Display the number of addresses in the
block, the first address, and the last address.

Code:

import ipaddress

def calculate_address_range(ip_block):
# Split the IP block into address and prefix length
ip_address, prefix_length = ip_block.split('/')

# Create an IPv4 network object


network = ipaddress.IPv4Network(ip_block, strict=False)

# Calculate the number of addresses


num_addresses = network.num_addresses

# Calculate the first address


first_address = network.network_address

# Calculate the last address


last_address = network.broadcast_address
return num_addresses, first_address, last_address

# Read the IP block from the user


ip_block = input("Enter the IP block (x.y.z.t/n): ")

# Calculate the address range


num_addresses, first_address, last_address =
calculate_address_range(ip_block)

# Display the results


print("Number of addresses in the block:", num_addresses)
print("First address:", first_address)
print("Last address:", last_address)

Output:

You might also like