C++ Program to convert a decimal number into binary by inline function
Write a program in C++ to convert a decimal number into binary without using an array and with inline function.
The inline function inline T4Tutorials_Decimal_Number(int n)
helps to increase the execution time of a program. The programmer can make a request to the compiler to make the inline function as inline T4Tutorials_Decimal_Number(int n)
.
Making inline means that compiler can replace the function definitions of inline T4Tutorials_Decimal_Number(int n)
with the place where this function is called T4Tutorials_Decimal_Number (n);.
The compiler replaces the definition of inline functions at compile time instead of referring function definition at runtime.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> using namespace std; inline T4Tutorials_Decimal_Number(int n) { int i=1,j=n , binaryn=0; for(j=n;j>0;j=j/2) { binaryn=binaryn+(n%2)*i; i=
i*10; n=n/2; } cout<<"binary number ="<<binaryn<<endl; }; int main() { int n; cout<<" Please enter Decimal number :"; cin>>n; T4Tutorials_Decimal_Number (n); } |
Output
Please enter Decimal number :
9
binary number =
1001