Need a project done?

C++ Programming Developer

Search This Blog

Generic Binary to Decimal Converter in C++

#include <iostream>
#include <cmath> //For using pow operator which calculates x^y.
using namespace std;
int main()
{
int Binary;
cout<< "Binary: "; cin>>Binary;

//Seperating each digit of the Binary Number, since we have to work on each digit.
int Digit[11]; // An int cannot store more than 11 digits.
int k = 1, i = 0;
while (Binary/k != 0)
{
Digit[i] = (Binary/k) % 10;//Each digit of binary number is being stored in an array named Digit.
i++;
k *= 10; //k = k*10;
}

int Decimal = 0; i = 0;
while (Digit[i] != -858993460) //This is the garbage value which is produced Normally.
{
Decimal += Digit[i]*pow(2,i); //This is the formula which you and I use for the conversion from binary to decimal.
i++;
}
cout<< "Decimal: "<<Decimal<<endl;
return 0;
}

You might want to see: Generic Decimal to Binary Converter

Share knowledge: (Link to this post of decimal to binary program in C++ is: http://adf.ly/bjxQH)

How to make Timer in C++ without clearing whole screen i.e. without system("cls")

#include<iostream>
#include<windows.h>//for using Sleep => Pauses execution of program, where ever used.
using namespace std;

int main()
{
for (int minutes = 2; minutes>=0; minutes--)
{
for (int seconds = 59; seconds>=0; seconds--)
{
cout<<minutes<<":"<<seconds;
Sleep (1000); //System pauses for 1000 milliseconds i.e. 1 second.
cout<<"\r";//Removes the timer i.e. the line on which timer is running. You can replace this line by //system("cls");
}
}
 return 0;
}

You might not be sure about the last step. \r is carriage return. When used alone as I have used, it removes the current line. And normally it's used to bring the cursor to the starting of line. As I've used in my Decimal to Binary Converter.
This is an efficient way, you can use system("cls") command, which clears whole screen and then prints the timer again (and loop is continued like this) whereas \r is clearing only one single line. \r is efficient because there may be a scenario in which we don't want our whole of the screen to be cleared and then we'll have to reprint our whole screen.
Feel free to ask if you still have queries.

Function to draw a right angle triangle

Write a function to draw a right angle triangle.


#include<iostream>
void Draw_Asterisks();
using namespace std;

int main()
{
Draw_Asterisks();
}

void Draw_Asterisks()
{
for (int lines = 1; lines<=5; lines++)
{
for (int i = 1; i<=2*lines-1 ; i++)
cout<<"*";

cout<<endl;
}
}

Function to calculate Largest of 3 (three) Numbers

Q. Write a function to calculate largest of 3 numbers.

Answer:
#include<iostream>
using namespace std;
double largest_of_three(double,double,double);

int main()
{
double a,b,c;

cout<<"a = "; cin>>a;
cout<<"b = "; cin>>b;
cout<<"c = "; cin>>c;

cout<<"Largest: "<<largest_of_three(a,b,c)<<endl;
}

double largest_of_three(double a, double b, double c)
{

if (a > b && a > c)
return a;
else if (b>c && b>a)
return b;
else return c;
}

Generic Decimal to Binary Converter in C++

#include <iostream>
#include <iomanip>
void binary_converter (int);
using namespace std;

int main()
{
float Num;
cout<<"Number: ";
cin>>Num;

cout<<"Binary: "; binary_converter(Num);
cout<<endl;
return 0;
}

void binary_converter (int Num)
{
int a, i = 20;
while (Num != 0)
{
a = Num % 2;
Num=Num/2;
cout<<"\r"<<setw(i--)<<a;
}

}

Program to produce Fibonacci sequence (first 25 terms) | Recursive function to find any Fibonacci term

Write a function to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci sequence the sum of two successive terms gives the third term. Following are the first few terms of the Fibonacci sequence: 1 1 2 3 5 8 13 21 34 55 89...

#include<iostream>
void fibonaci_sequence();
using namespace std;

void main()
{
            fibonaci_sequence();//Prints first 25 terms of Fibonaci Sequence.
}

void fibonaci_sequence()
{
            int term[27];
            term[0] = term[1] = 1;
            cout<<term[0]<<"\t "<<term[1]<<" \t";
           
            for (int i = 2; i<25; i++)
            {
                        term[i] = term[i-2]+term[i-1];
                        cout<<term[i]<<" \t";
            }
            cout<<endl;
}

//==========================================================================

Recursive Function to find any fibanacci term.

#include <iostream>
using namespace std;

int fib(int);

int main()
{
int x;
cout << "X: ";
cin >> x;

cout << fib(x) << endl;

return 0;
}


int fib(int term)
{
if (term == 1 || term == 2)
return 1;
else
return fib(term-1) + fib(term-2);
}

Write a function that receives as input three integer arguments and then circularly shift their values to right. (The change in variables must be permanent) Example:

Question:
Write a function that receives as input three integer arguments and then circularly shift their values to right. (The change in variables must be permanent) Example:
If x=5, y=8, z=10 then after circular shift y=5, z=8, and x=10.



#include<iostream>
using namespace std;
void swap_by_ref(int &,int &,int &); //Swaps the values of x and z "permanently".
int main()
{
 int x,y,z;
 cout<<"Enter 3 numbers followed by spaces or enter: ";  cin>>x>>y>>z;
 swap_by_ref(x,y,z);
 cout<<"x = "<<x<<" \t y = "<<y<<" \t z = "<<z<<endl;
 return 0;
}
void swap_by_ref(int &x, int &y, int &z)
{
 //Logic behind Swaping

 int temporary_variable_1 = x;
 x = z;
 int  temporary_variable_2 = y;
 y = temporary_variable_1;
 z =  temporary_variable_2;
}

Question:



#include<iostream>
using namespace std;

int fact(int);
long double Series(int,int);

int N,K;

int main()

{
cout<<"Enter 2 Numbers followed by space "; cin>>N>>K;
cout<<"Series = "<<Series(N,K)<<endl;

 return 0;
}
//Factorial Calculator
int fact(int N)
{
int Num = 1;
for (int i=1; i<=N; i++)
Num *= i;
return Num;
}
//Series Calculator
long double Series(int N,int K)
{
return  ((fact (N))/((fact(K))*(fact(N-K))));

}

How to Calculate Prime Factors?


#include <iostream>

using namespace std;

int main()
{
cout << "Enter a number: ";
int num;
cin >> num;

for (int i=2; i <= num; i++)
{
 while(num % i == 0)
 {
   num /= i;
   cout << i << " ";
 }
}
cout << endl;
return 0;
}

Make a border of Asterics

//Program to Print Asterics border of size 24(rows) by 79(columns).



#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
 char array[24][79];

 //Loop for Storing '*' at all indexes of array
 for (int k=0; k<79; k++)
 {
  for (int m=0; m<24; m++)
   array[m][k] = '*';
 }

  //Loop for Printing first line
  for (int j=0; j<24; j++)
  {
 cout<<array[0][j]<< " "; //within curly brackets just for proper looking, no use.
  }

  cout<<endl;

  //Loop for Printing verticle lines of border
  for (int i=0; i<24; i++)
  {
 cout<<array[1][0]<<setw(46)<<array[23][1]<<endl; // //within curly brackets no use.
  }

  //Loop for Printing Last line of Border
  for ( j=0; j<24; j++)
  {
 cout<<array[0][j]<< " "; //within curly brackets just for proper looking, no use.
  }

  cout<<endl<<endl<<endl<<endl;

  return 0;

}





A better & Stylish way of doing the Same Work!


#include <iostream>
#include<iomanip>
#include<windows.h>
using namespace std;
int main()
{
 int Speed = 50; //Lesser the value Greater the Speed of printing Asterics
 //because it's the time in milliseconds.

 char array[24][79];

 //Loop for Storing '*' at all indexes of array
 for (int k=0; k<79; k++)
 {
  for (int m=0; m<24; m++)
   array[m][k] = '*';
 }

  //Loop for Printing first line
  for (int j=0; j<24; j++)
  {
   cout<<array[0][j]<< " ";
   Sleep(Speed);
  }
 
  cout<<endl;

  //Loop for Printing verticle lines of border
  for (int i=0; i<24; i++)
  {
   cout<<array[1][0]<<setw(46)<<array[23][1]<<endl;
   Sleep(Speed);
  }

  //Loop for Printing Last line of Border
  for ( j=0; j<24; j++)
  {
   cout<<array[0][j]<< " ";
   Sleep(Speed);
  }

  cout<<endl<<endl<<endl<<endl;

  return 0;

}





"Don't let anyone ever make you feel like you don't deserve what you want."