C++ Program for Subnet Calculator
Below is the implementation of the Subnet Calculator in C++ programming language.
C++
// C++ Program to implement // Subnet Calculator #include <bitset> #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; // function void GFG( const string& ipAddress, const string& subnetMask) { // Parse IP address and subnet mask vector< int > ipParts; vector< int > maskParts; istringstream ipStream(ipAddress); istringstream maskStream(subnetMask); string ipPart; while (getline(ipStream, ipPart, '.' )) { ipParts.push_back(stoi(ipPart)); } string maskPart; while (getline(maskStream, maskPart, '.' )) { maskParts.push_back(stoi(maskPart)); } // Calculate network address vector< int > networkAddress; for ( size_t i = 0; i < ipParts.size(); i++) { networkAddress.push_back(ipParts[i] & maskParts[i]); } vector< int > broadcastAddress; for ( size_t i = 0; i < ipParts.size(); i++) { broadcastAddress.push_back( ipParts[i] | (~maskParts[i] & 0xFF)); } // Calculate usable IP range networkAddress[3]++; broadcastAddress[3]--; // Display subnet details cout << "Subnet Details:" << endl; cout << "Network Address: " ; for ( int i = 0; i < 4; i++) { cout << networkAddress[i]; if (i < 3) { cout << "." ; } } cout << endl; cout << "Broadcast Address: " ; for ( int i = 0; i < 4; i++) { cout << broadcastAddress[i]; if (i < 3) { cout << "." ; } } cout << endl; cout << "Subnet Mask: " << subnetMask << endl; cout << "Usable IP Range: " ; for ( int i = 0; i < 4; i++) { cout << networkAddress[i]; if (i < 3) { cout << "." ; } } cout << " - " ; for ( int i = 0; i < 4; i++) { cout << broadcastAddress[i]; if (i < 3) { cout << "." ; } } cout << endl; } // main function int main() { string ipAddress, subnetMask; cout << "Enter IP Address: " ; cin >> ipAddress; cout << "Enter Subnet Mask: " ; cin >> subnetMask; GFG(ipAddress, subnetMask); return 0; } |
Output
Enter IP Address: 192.168.1.10
Enter Subnet Mask: 255.255.255.0
Subnet Details:
Network Address: 192.168.1.1
Broadcast Address: 192.168.1.254
Subnet Mask: 255.255.255.0
Usable IP Range: 192.168.1.1 - 192.168.1.254
Subnet Calculator in C++
A Subnet Calculator in C++ is a program that allows users to calculate subnet masks, network addresses, broadcast addresses, and the range of the valid host addresses within a given IP address range and subnet mask and Subnetting is the technique used in computer networking to divide a large IP address space into smaller or more manageable subnetworks and The purpose of subnetting is to allocate IP addresses and optimize network resources efficiently. It enables the creation of multiple smaller networks within the larger network.