Found 7197 Articles for C++

Program for Decimal to Binary Conversion in C++

Sunidhi Bansal
Updated on 18-Oct-2019 08:13:18

290 Views

Given with a decimal number as an input, the task is to convert the given decimal number into a binary number.Decimal number in computers is represented with base 10 and binary number is represented with base 2 as it has only two binary digits 0 and 1 whereas decimal numbers can be any numeric digit starting from 0 – 9.To convert a decimal number into a binary number follow the given steps −Firstly divide the given number with the base value of conversion number e.g. dividing 42 by 2 because we need to convert 42 into a binary numbers which ... Read More

Program for Binary To Decimal Conversion in C++

Sunidhi Bansal
Updated on 18-Oct-2019 08:08:55

364 Views

Given with a binary number as an input, the task is to convert the given binary number into a decimal number.Decimal number in computers is represented with base 10 and binary number is represented with base 2 as it has only two binary digits 0 and 1 whereas decimal numbers can be any numeric digit starting from 0 – 9.To convert a binary number into a decimal number we will extract digits starting from right to left through a remainder and then multiply it with the power of 2 starting from 0 and will be increased by 1 till the ... Read More

Program to convert IP address to hexadecimal in C++

Sunidhi Bansal
Updated on 18-Oct-2019 08:04:25

782 Views

Given with the input as an IP address value and the task is to represent the given IP address as its hexadecimal equivalent.What is IP addressIP address or Internet protocol is a unique number to that uniquely describes your hardware connected to a network. Internet means over the network and protocol defines the set of rules and regulations that must be followed for connection. Because of IP address only it is possible for a system to communicate with another system over a network. There are two versions of IP that are −IPv4(Internet Protocol Version 4)IPv6(Internet Protocol Version 6)IP address is ... Read More

Program to convert KiloBytes to Bytes and Bits in C++

Sunidhi Bansal
Updated on 18-Oct-2019 07:59:50

1K+ Views

Given with the input as KiloBytes and the task is to convert the given input into number of bytes and bits.Bit − In computers, bit is the smallest unit represented with two integer value 0 and 1 and all the information in computer is processed as a sequence of these two digits.N-bits = 2 ^ N patterns, where N can be any integer value starting from 1.Byte − In computers, byte is represented with 8 bits. Byte is capable of holding one character to numbers ranging between 0 and 255.1 byte = 8 bitsWhich means 2 ^ 8 patterns which ... Read More

Program to convert given Matrix to a Diagonal Matrix in C++

Sunidhi Bansal
Updated on 18-Oct-2019 07:47:09

607 Views

Given with the matrix of size nxn the task it to convert any type of given matrix to a diagonal matrix.What is a diagonal MatrixDiagonal matrix is the nxn matrix whose all the non-diagonal elements are zero and diagonal elements can be any value.Given below is the diagram of converting non-diagonal elements to 0.$$\begin{bmatrix}1 & 2 & 3 \4 & 5 & 6 \7 & 8 & 9 \end{bmatrix}\:\rightarrow\:\begin{bmatrix}1 & 0 & 3 \0 & 5 & 0 \7 & 0 & 9 \end{bmatrix}$$The approach is to start one loop for all non-diagonal elements and another loop for diagonal elements ... Read More

Maximum GCD from Given Product of Unknowns in C++

Arnab Chakraborty
Updated on 21-Oct-2019 06:59:29

153 Views

Suppose we two integers N and P. The P is the product of N unknown integers. We have to find the GCD of those integers. There can be different groups of integers possible, that will give the same result. Here we will produce GCD, which is maximum among all possible groups. Suppose N = 3, and P = 24, then different groups will be like {1, 1, 24}, {1, 2, 12}, {1, 3, 8}, {1, 4, 6}, {2, 2, 6}, {2, 3, 4}. The GCDs are: 1, 1, 1, 1, 2, 1. So answer is 2 here.The technique us like, ... Read More

Maximum Consecutive Zeroes in Concatenated Binary String in C++

Arnab Chakraborty
Updated on 17-Oct-2019 13:40:16

454 Views

Suppose we have a binary string of length n, another value say k is given. We have to concatenate the binary string k times. Then we have to find the maximum number of consecutive 0s in the concatenated string. Suppose binary string is “0010010”, and k = 2, then after concatenating the string k times, it will be “00100100010010”. So the maximum number of consecutive 0s is 3.The approach is simple. If the number has all 0s, then the answer will be n * k. If the string contains ones, then the result will be either the max length of ... Read More

Split the array into equal sum parts according to given conditions in C++

Arnab Chakraborty
Updated on 17-Oct-2019 13:36:42

827 Views

Here we will see one problem. Suppose one array arr is given. We have to check whether the array can be split into two parts, such that −Sub of both sub-arrays will be the sameAll elements, which are multiple of 5, will be in the same groupAll elements which are multiple of 3, but not multiple of 5, will be in the same groupAll other elements will be in other groups.Suppose the array elements are {1, 4, 3}, then this can be split, because the sum of {1, 3} is the same as the sum of {4}, and the groups ... Read More

Area of a polygon with given n ordered vertices in C++

Aman Kumar
Updated on 29-Jul-2025 14:39:53

1K+ Views

A polygon is a closed two-dimensional shape formed by connecting three or more straight lines end-to-end. These lines form sides, and their connection points are called vertices. When the vertices of the polygon are given in a specific order either clockwise or counter-clockwise, we can calculate the area of the polygon using a mathematical formula known as the Shoelace Formula or Surveyor’s Formula. You are given the coordinates of a polygon with n vertices. The vertices are provided in an ordered manner, meaning they are listed either in clockwise or anticlockwise order starting from the first vertex to the last. ... Read More

Area of a n-sided regular polygon with given side length in C++

Aman Kumar
Updated on 29-Jul-2025 14:47:49

1K+ Views

A regular polygon with n sides is a two-dimensional shape with n equal-length sides and n equal interior angles. It is both equilateral and equiangular (all sides are equal and all angles are equal). In the above diagram, we see that the polygon can be divided into n equal triangles. For one of the triangles, the complete angle at the center can be divided into = 2pi/n So, angle t = pi/n Now, tan(t) = a/2*h So, h = a/(2*tan(t)) Area of ... Read More

Advertisements