Monday, 21 December 2015

Functions, Overloading program

This program contains code for calculator to find:
Sum , Subtract, Multiplication, Division, Fibonacci

these can be find for 2 and 3 inputs, also in fractions.
It includes function and overloading concepts. 

#include<iostream>
#include<conio.h>
using namespace std;
 // Here all functions are being defined 
int add(int, int);
int add(int, int, int);
float add(float, float);

int sub(int, int);
int sub(int, int, int);
float sub(float, float);

int mul(int, int);
int mul(int, int, int);
float mul(float, float);

int divd(int, int);
int divd(int, int, int);
float divd(float, float);

int fib(int);

// here is manu/ list describing input methods
void manu()
{
cout << "for two integer opperations \n \n";
cout << "press 1 for Addition" << endl;
cout << " press 2 for Subtraction" << endl;
cout << " press 3  for Multiplication" << endl;
cout << " press 4 for Division" << endl;
cout << " press 5 for fibonacci \n \n" << endl;

cout << "for three integer opperations" << endl;
cout << "press 6 for Addition" << endl;
cout << " press 7 for Subtraction" << endl;
cout << " press 8  for Multiplication" << endl;
cout << " press 9 for Division \n \n" << endl;

cout << "for two integer fraction opperations \n \n";
cout << "press 10 for Addition" << endl;
cout << " press 11 for Subtraction" << endl;
cout << " press 12  for Multiplication" << endl;
cout << " press 13 for Division" << endl;

return;
}
// here is main function in which we will call all made functions
int main()                           
{
manu();
int n,i,x,y,z,f;
float a, b;
cin >> n;
switch (n)
{
case 1:
cout << "enter 1st num \n = ";
cin >> x;
cout << "enter 2nd num \n = ";
cin>> y;
cout << add(x, y);
break;
case 2:
cout << "enter 1st num \n = ";
cin >> x;
cout << "enter 2nd num \n = ";
cin >> y;
cout << sub(x, y);
break;
case 3:
cout << "enter 1st num \n = ";
cin >> x;
cout << "enter 2nd num \n = ";
cin >> y;
cout << mul(x, y);
break;
case 4:
cout << "enter 1st num \n = ";
cin >> x;
cout << "enter 2nd num \n = ";
cin >> y;
cout << divd(x, y);
break;
case 5:
cout << "put a number to find its fibonacci \n = ";
cin >> f;
cout << "fibonacci of " << f << " is = " << fib(f);
break;
case 6:
cout << "enter 1st num \n = ";
cin >> x;
cout << "enter 2nd num \n = ";
cin >> y;
cout << "enter 3rd num \n = ";
cin >> z;
cout << add(x, y, z);
break;

case 7:
cout << "enter 1st num \n = ";
cin >> x;
cout << "enter 2nd num \n = ";
cin >> y;
cout << "enter 3rd num \n = ";
cin >> z;
cout << sub(x, y, z);
break;

case 8:
cout << "enter 1st num \n = ";
cin >> x;
cout << "enter 2nd num \n = ";
cin >> y;
cout << "enter 3rd num \n = ";
cin >> z;
cout << mul(x, y, z);
break;

case 9:
cout << "enter 1st num \n = ";
cin >> x;
cout << "enter 2nd num \n = ";
cin >> y;
cout << "enter 3rd num \n = ";
cin >> z;
cout << divd(x, y, z);
break;

case 10:
cout << "enter 1st num \n = ";
cin >> a;
cout << "enter 2nd num \n = ";
cin >> b;
cout << add(a, b);
break;
case 11:
cout << "enter 1st num \n = ";
cin >> a;
cout << "enter 2nd num \n = ";
cin >> b;
cout << sub(a, b);
break;
case 12:
cout << "enter 1st num \n = ";
cin >> a;
cout << "enter 2nd num \n = ";
cin >> b;
cout << mul(a, b);
break;
case 13:
cout << "enter 1st num \n = ";
cin >> a;
cout << "enter 2nd num \n = ";
cin >> b;
cout << divd(a, b);
break;
}

_getch();
}
// here all functions' are explained
int add(int a, int b)
{
int ans = a + b;
return ans;
}
int add(int a, int b, int c)
{
int ans = a + b + c;
return ans;
}
float add(float a, float b)
{
float ans = a + b;
return ans;
}

int sub(int a, int b)
{
int ans = a - b;
return ans;
}
int sub(int a, int b, int c)
{
int ans = a - b - c;
return ans;
}
float sub(float a, float b)
{
float ans = a - b;
return ans;
}

int mul(int a, int b)
{
int ans = a*b;
return ans;
}
int mul(int a, int b, int c)
{
int ans = a * b * c;
return ans;
}
float mul(float a, float b)
{
float ans = a * b;
return ans;
}
int divd(int a, int b)
{
int ans = a / b;
return ans;
}
int divd(int a, int b, int c)
{
int ans = a / b / c;
return ans;
}
float divd(float a, float b)
{
float ans = a / b;
return ans;
}
int fib(int n)
{

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

}

Tuesday, 15 December 2015

Program of Fibonacci Series

This program shows fibonacci numbers series less than 1000
Also add all even number of series.



# include <iostream>
# include <conio.h>
using namespace std;
void main()

{
int  x = 0;
int y = 1;
int n = 0;
int fib = 0;
int sum = 0;
cout << "The fibnashi series Prepared by HAFIZ AHMAD BILAL \n";

cout << "The Fibnashi Series is \n" << "\n";
do {
fib = x + y;

if (fib <= 1000){
cout << "                       " << fib << "  \n";
};

y = x;
x = fib;


if (x % 2 != 1)
{

sum = x + sum;

};
} while(fib<=1000);
cout << "\n" << "The Sum of Even Fibnashi Sereis is = "<< sum << "\n" << endl;
system("pause");

}

Program of Calculator using Switch-Case logic

It can Calculate by four operations (+ - * /)
Also tells which input number is greater!

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int n, a, b, c, d,e,f;
cout << "Enter a two digit number : ";
cin >> n;
if (n >= 10 && n <= 99)
{
cout << "Its a two digit number";
a = n % 10;
b = n / 10;
c = a + b;
if (c >= 10)
{
d = c / 10;
e = c % 10;
f = d + e;
cout << "The sum is : " << f << endl;
}
else
cout << "The sum is : " << c << endl;
}
return 0;
}

Conversion of Unit

This program convert a value of some unit (g, kg, mg) into other unit.


#include<iostream>
#include<conio.h>
using namespace std;
float n, a, b, c, d, e,g=1 ,mg, kg;
int i, j;
int main()
{
cout << " This a programe to convert the unit of some value \n \n put some value = ";
cin >> n;
cout << "Now put it's present unit \n \n 1 for 'g' \n 2 for 'kg' \n 3 for 'mg' \n \n ";
cin >> i;
//a = n * i;
// cout << a<<" g";


switch (i)
{
case 1:
cout <<"       ="<< n << " g";
a = n;
break;

case 2:
cout <<"       ="<< n << " kg";
a = n * 1000;
break;

case 3:
cout <<"       ="<< n << " mg";
a = n / 1000;
break;
}

cout << "\n \n Now put the unit in which you want to convert your value \n \n 1 for 'g' \n 2 for 'kg' \n 3 for 'mg' \n \n ";
cin >> j;

switch (j){
case 1:
cout << "       =" << a << " g";
break;

case 2:
cout << "       =" << a / 1000 << " kg";
break;

case 3:
cout << "       =" << a * 1000 << " mg";
break;
}

_getch();

}

Program to get single digit number from double

Program to find single digit number from double. 
Example: input -->    89
                output->     8 + 9 = 17
                output->     1 + 7 = 8  (single digit)

(It was given in Mid-term Exam in my First term)

#include<iostream>
#include<conio.h>
using namespace std;
int n, a, b, c, d, e;
int main()
{
cout << "Put any two digit number = ";
cin >> n;

while (n > 10){
a = n % 10;
b = n / 10;
c = a + b;
cout << b << " + " << a << " = " << c << "\n";
n = c;

}
_getch();
}

Program to get code of any key in C++


// This is the program which give u the c++ code of any Key/Character like A, B, C, Space, Enter etc


# include<iostream>
# include<conio.h>

using namespace std;

void main()
{
int code, code1;
do{
char key = _getch();
code = key;
cout << key << " = " << code;
} while (code1 = 27);
}

Program of Pathagorian Triples in C++

               Pathagorian    triples

// This is the program which shows all combinations, less than 500 making answer, satisfying Pathagorian Theorum.


# include<iostream>
# include<conio.h>

using namespace std;

void main()
{
       int p, q, r;        //Here we need three variables.

       for (p = 1; p*p <= 500; ++p){

              for (q = 1; q*q <= 500; ++q){

                     for (r = 1; r*r <= 500; ++r){

                           if ((p*p) + (q*q) == (r*r))

                                  cout << p*p << "  +  " << q*q << "   =  " << r*r << endl;
                     }
              }
       }
       _getch();
}