Pages

Tuesday, 8 December 2015

Celsius to Fahrenheit and Kelvin conversion in C++



Celsius to Fahrenheit :


Formula:
                   

                               $ F = (C \times {\frac{9}{5}}) + 32 $



  • $F$ : Temperature in Fahrenheit scale
  • $C$ : Temperature in Celsius scale 


#include "iostream"

using namespace std;

#define pi 3.14159

int main()
{
    float farenheit, celsius;

    cout << "Enter the temperature in celsius :" ;
    cin >> celsius ;

    // celsius to fareheit

    farenheit = (celsius * 9.0) / 5.0 + 32;

    cout << "Temperature in celsius : " << celsius << endl;
    cout << "Temperature in fahrenheit : " << farenheit ;

    return (0);

}

INPUT:      Enter the temperature in celsius : 35
OUTPUT : Temperature in Celsius : 35
                  Temperature in  Fahrenheit: 95


Celsius to Kelvin 


Formula: 

                               $K = C + 273.15$


  • $K$ : Temperature in Kelvin scale
  • $C$ : Temperature in Celsius scale 


#include "iostream"

using namespace std;

#define pi 3.14159

int main()
{
    float Kelvin, celsius;

    cout << "Enter the temperature in celsius :" ;
    cin >> celsius ;

    // celsius to kelvin

    Kelvin = celsius + 273.15;

    cout << "Temperature in celsius : " << celsius << endl;
    cout << "Temperature in kelvin : " << Kelvin ;

    return (0);

}


INPUT:       Enter the temperature in celsius : 35
OUTPUT : Temperature in Celsius : 35 
                  Temperature in  kelvin: 308.15


Finding a Perfect number using C++



Perfect Number: Perfect number is a positive real number which is the sum of its all positive divisors without itself.

Example: $6$ is a perfect number. The divisor of $6$ are $1, 2, 3$.
                  The sum of its divisors are : $1 + 2 + 3 = 6$

                 $28$ is a perfect number. The divisors of $28$ are $1, 2, 4, 7, 14$.
                 The sum of its divisors are : $1 + 2 + 4 + 7 + 14 = 28$

                 Some other perfect numbers are: $496, 8128$, etc  

Algorithm:

1. Find the number of divisors of the number
2. Add all the divisors 
3. Check the addition of the numbers is equivalent to that number or not
4. If equivalent then perfect number
5. If not equivalent not a perfect number 


#include "iostream"
#include "conio.h"
#include "math.h"

using namespace std;


int main()                 //Start of main
{

 int n, i=1, u=1, sum=0;
 cout << "Enter a range to find the perfect numbers :" ;
 cin >> n;
 while(i<=n)
 {                                  // start of first loop.

   while(u<=n)
   {                               //start of second loop.
     if(u<i)
     {
      if (i%u == 0)
        sum = sum + u;
      }                          // End of the if statement
     u++;
    }                            // End of second loop
   if (sum == j)
    {
     cout << i << "is a perfect number" << "\n";
    }
    i++;
    u = 1 ;  sum = 0;
   }                           // End of the First loop
     return(0);
}                             // End of main

Input: Range := 500
Output: Perfect number := 6, 28, 496

Input: Range := 10000
Output: Perfect number := 6, 28, 496, 8128

Area and Circumference of a circle using C++



Algorithm:



Radius of Circle : $r$
Diameter of a circle : $d = 2 * r$
The area of a circle is : $(\pi*{r^2})$
Circumference of a circle is : $(2*\pi * r)$  or  $(\pi * d)$
#include "iostream"
 
using namespace std;
#define pi 3.14
 
int main ()
{
    float r,a;
    float c;
    cout << "Enter radius :" ;
    cin >> r;
    // Area of the circle
    a = pi*r*r;
 
    // Circumference of circle
    c = 2*pi*r;
 
    cout << "Area of the circle : " << a << " " <<"square meter" << endl;
    cout << "Circumference of circle : " << c << " " <<"meter" ;
 
    return (0);
}
Input: Enter radius : 4
Output: Area of the circle : 50.24 square meter
              Circumference of the circle : 25.12 meter

Area of a Triangle on C++



Area of a Triangle: It is simply calculated by using the base and height of the triangle.




Formula:

                          $Area  = \frac{1}{2} \times Height \times Base $

Algorithm:

Steps:

  1. Enter the base and height
  2. Calculate the area using the above formula
  3. If height is not given find the corresponding angle between the two given sides
  4. Calculate the area by multiplying $\frac{1}{2}$ with two given sides of the triangle and $sine$ of the angle between the two sides.

#include "iostream"
#include "math.h"

using namespace std;

int main ()
{
    float base, height, area;

    cout << "Enter the base :";
    cin >> base;

    cout << "Enter the height :" ;
    cin >> height;

    // Find the area of triangle

    area = 0.5 * (base * height);
    cout << "Area of the triangle :" << area;

    return (0);
}

INPUT:       Enter the base: 12
                    Enter the height: 12
OUTPUT:   Area of the triangle: 72


If  height is not given but two side $AB$ and $BC$ is given and angle  ( angle $b$) between them  is given then the area is calculated as:

          $ Area = \frac{1}{2} \times base(BC) \times {({AB} \times Sin(b))} $

#include "iostream"
#include "math.h"

using namespace std;
#define PI 3.14159265

int main ()
{
    float b, c, area;
    double angle;

    cout << "Enter the 1st side of triangle :";
    cin >> b;

    cout << "Enter the 2nd side of triangle :" ;
    cin >> c;

     cout << "Enter the angle between two sides (degree) :" ;
    cin >> angle;


    // Find the area of triangle

    area = 0.5 * (b * c * sin(PI * angle/180));
    cout << "Area of the triangle :" << area;

    return (0);
}
INPUT:        Enter the 1st side of the triangle : 12
                     Enter the 2nd side of the triangle : 12
                     Enter the angle between two sides (degree) : 30
OUTPUT:    Area of the triangle: 36


Compound Interest Calculation using C++



Compound Interest:


Compound interest is a interest that is added to the principal amount of the deposit or loan to gain interest on the accumulated  (principal + interest) amount.  The addition of interest to the principal amount is called as compounding.

Formula:


                    $A=P \times {(1+ \frac{r}{n})}^{nt}$
                    $ci = A - P $


Where:

  • $A =$ Accumulated Amount (principal + interest)
  • $P =$ Principal Amount
  • $I =$ Interest Amount
  • $R =$ Annual Nominal Interest Rate in percent
  • $r =$ Annual Nominal Interest Rate as a decimal
  • $r = R/100$
  • $t =$ Time Involved in years, 0.5 years is calculated as 6 months, etc.
  • $n =$ number of compounding periods per unit t; at the END of each period
  • $ci =$ Compound interst

Algorithm:

Steps:
  1. Input the principal amount, rate of interest and time (in years)
  2. Find the annual interest rate as decimal
  3. Compute the accumulated amount using the above formula
  4. Find the compound interest by subtracting the principal from accumulated amount. 
  5. Determine the compound interest
#include "iostream"
#include "conio.h"
#include "math.h"
using namespace std;

int main()
{
float p, r, n, ci;
int t;

    cout<<"Enter Principle amount :";
    cin >> p  ;
    cout << "Rate in %:" ;
    cin >> r  ;
    cout << "Time in years:" ;
    cin>> t;
    // Accumulated amount calculation 
    n = p*pow((1+r/100),t);
    // Compounded amount
    ci = n-p;

    cout << "\n" << "Compound Interest = " << ci << endl;
return (0);
}
INPUT:      Enter Principal Amount: 1200
                   Rate in % : 10
                   Time in years : 2
OUTPUT:  Compound interest : 252

Decimal to Hexadecimal using C++



Decimal Number System: It is also called base $10$ system or Denary system. It refer to any number written in decimal notation format.

Hexadecimal Number System: It is also called base $16$ system or Hex system. It refer to any number written in hexadecimal notation format. It uses sixteen different symbols $0-9$ and $A, B, C, D, E, F$ represent $10-15$.


Algorithm:

  1. Divide the decimal number by $16$. Consider the division operation as an integer division.
  2. Note down the remainder (in hexadecimal).
  3. Divide the result again by $16$ and again consider the division as an integer division.
  4. Repeat step $2$ and $3$ until result is $0$.
  5. The hex value is the digit sequence of the remainders from the last to first







#include "iostream"
#include "conio.h"
#include "math.h"

using namespace std;

void dtoh(int d);
int main()
{
    int d;

    cout<< "Enter a decimal number system: ";
    cin >> d ;
    dtoh(d);
    return (0);
}

void dtoh(int d)
{
    int b,c=0,a[5],i=0;
    b=d;
    while (b>15)
    {
        a[i]=b%16;
        b=b/16;
        i++;
        c++;
    }
    a[i]=b;
    cout << "Its hexadecimal equivalent is : ";
    for (i=c;i>=0;--i)
    {
        if (a[i]==10)
        cout << "A";
        else if (a[i]==11)
        cout << "B";
        else if (a[i]==12)
        cout << "C";
        else if (a[i]==13)
        cout << "D";
        else if (a[i]==14)
        cout << "E";
        else if (a[i]==15)
        cout << "F";
        else
        cout << a[i];
    }
}
Input:    Decimal number : $12$
Output: Hexadecimal number: $C$

Input:    Decimal number : $4023$
Output: Hexadecimal number: $FB7$

Implementation of $(a+b)^2$ and $(a-b)^2$ operation using C++


Algorithm:


The basic mathematical operation $(a+b)^2$ = ${a^2} + {b^2} + {2ab}$ and the operation $(a-b)^2$ = ${a^2} + {b^2} - {2ab}$

#include "iostream"

using namespace std;

class Mathematics
{
    int a,b;
public:
    void input()
    {
        cout<< "input a:\n";
        cin >> a;
        cout << "input b:\n";
        cin >> b;
    }

    void add()
    {
        cout << "(a+b)^2=" << ((a*a)+(2*a*b)+(b*b));
    }
    
    void sub()
    {
        cout << "(a-b)^2=" << ((a*a)-(2*a*b)+(b*b));
    }
};

int main()
{
    Mathematics m;

    m.input();
    m.add();
    m.sub();

    return (0);
}