C++ Programming Code Examples
C++ > Conversions Code Examples
Convert Hexadecimal to Binary
/* Convert Hexadecimal to Binary
To convert hexadecimal number to binary number in C++ programming, you have to ask to the user to enter the hexadecimal number to convert it into binary number to display the equivalent value in binary format as shown here in the following program.
Following C++ program ask to the user to enter any number in hexadecimal format to convert it into binary, then display the result on the screen: */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long int i=0;
char binnum[100], hexdec[100];
cout<<"Enter any hexadecimal number : ";
cin>>hexdec;
cout<<"\nEquivalent Binary value is : ";
while(hexdec[i])
{
switch(hexdec[i])
{
case '0' : cout<<"0000";
break;
case '1' : cout<<"0001";
break;
case '2' : cout<<"0010";
break;
case '3' : cout<<"0011";
break;
case '4' : cout<<"0100";
break;
case '5' : cout<<"0101";
break;
case '6' : cout<<"0110";
break;
case '7' : cout<<"0111";
break;
case '8' : cout<<"1000";
break;
case '9' : cout<<"1001";
break;
case 'A' : cout<<"1010";
break;
case 'B' : cout<<"1011";
break;
case 'C' : cout<<"1100";
break;
case 'D' : cout<<"1101";
break;
case 'E' : cout<<"1110";
break;
case 'F' : cout<<"1111";
break;
case 'a' : cout<<"1010";
break;
case 'b' : cout<<"1011";
break;
case 'c' : cout<<"1100";
break;
case 'd' : cout<<"1101";
break;
case 'e' : cout<<"1110";
break;
case 'f' : cout<<"1111";
break;
default : cout<<"\nInvalid hexadecimal digit "<<hexdec[i];
}
i++;
}
getch();
}
In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.
Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block. • The expression can be integer expression or a character expression. • Value-1, 2, n are case labels which are used to identify each case individually. Remember that case labels should not be same as it may create a problem while executing a program. Suppose we have two cases with the same label as '1'. Then while executing the program, the case that appears first will be executed even though you want the program to execute a second case. This creates problems in the program and
It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.
The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.
Displaying the "Topological Sort Method" of finding whether a given graph contains cycle or not using Kosaraju's Algorithm. Enter the source and destination. Cycles exist in graph.
To store Student details like student 'name', student 'roll num', 'student age'. You have 2 ways to do it, one way is to create different variables for each data, but the downfall of
Program Declares the array of five element & the elements of that array are accessed using pointer. The five elements are entered by the user and stored in the integer array data. And
See in this case the output is Woof, which is what we expect. What happens in this case? Since we marked the function animalSound() as virtual, the call to the function is resolved
"Switch statement" is multi-way decision that tests whether an expression 'matches' one of a number of "constant integer", and branches accordingly. 'Switch statement' that allows us