Pages

Thursday 17 December 2015

Roots of quadratic equations using C++


Quadratic Equation:
A quadratic equation is any equation in any form $ax^2 + bx + c = 0$, where $a \ne 0$. Finding the roots of the quadratic equation is nothing but solving the equation for $x$. Any quadratic equation can be solved by the formula :

$x = \frac{-b \pm {\sqrt {b^2 - 4ac}}}{2a}$

Algorithm:
  1. Input the coefficients of the quadratic equation
  2. Determine the determinant of the coefficients
  3. Check whether $a$ is $0$ or not 
  4. If $a \ne 0$ then check determinant
  5. If $determinant \gt 0$ roots are real and different
  6. If $determinant = 0$ roots are real and same
  7. Otherwise roots are complex
  8. Find the roots based on above criteria
  9. Stop the program

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

using namespace std;

int main()
{
    float a,b,c,x,y, determinant, real, imaginary;

    cout << "Enter coefficients a, b and c : " ;
    cin >> a >> b >> c ;
    determinant = b*b - 4*a*c;

    if(a==0)
    {
        cout << " 'a' can not be zero" ;
    }
    else
    {
        if(determinant > 0)
        {
            x= (-b + sqrt(determinant))/(2*a);
            y= (-b - sqrt(determinant))/(2*a);
            cout <<"Roots are real and different:" <<endl;
            cout << "x :" << x << endl;
            cout << "y :" << y << endl;
        }
        else if (determinant == 0)
        {
            cout << "Roots are real and same " << endl;
            x = (-b + sqrt(determinant))/(2*a);
            cout << "x = y =" << x << endl;
        }
        else
        {
          real = -b/(2*a);
          imaginary = sqrt(-determinant)/(2*a);
          cout << "x = " << real << "+" << imaginary << "i" << endl;
          cout << "y = " << real << "-" << imaginary << "i" << endl;
        }
        return (0);
    }
}



INPUT :     Enter coefficients a, b and c : 4 5 6
OUTPUT : x = -0.625+1.05327i
                    y = -0.625-1.05327i


Palindrome number using C++


Palindrome number:

A number is said to palindrome if the digits of the number is reversed and it still remain the same.

Example:
  • Digits from $0$ to $9$ are all palindrome
  • $11$, $22$, $33$, $44$, $121$, $383$, $12321$, etc.
Algorithm

  1. Start the program
  2. Input a number to check palindrome or not
  3. Initialize the remainder variable to $0$
  4. Divide the input number by $10$ and store its remainder on a variable
  5. Store the quotient on the palindrome variable.
  6. Reverse the number and compare with the original input number
  7. If the reversed number is equivalent to original then it is palindrome otherwise not palindrome. 

#include"iostream"
using namespace std;
int main()
{
    int palindrome, reverse=0;

    cout<<"Enter a number:  ";
    cin>>palindrome;

    int num=0,key=palindrome;

    for(int i=1; palindrome!=0; i++)
   {
    num = palindrome % 10;
    palindrome=palindrome / 10;

    reverse=num+(reverse*10);
    }

   if(reverse==key)
    {
        cout<<key<<" is a Palindrome number";
    }
  else
   {
        cout<<key<<" is not a Palindrome Number";
    }
return 0;
}



INPUT : Enter a number: $135$
OUTPUT: $135$ is not a Palindrome Number

INPUT : Enter a number: $52325$
OUTPUT: $52325$ is a Palindrome Number


Fibonacci series using C++



Fibonacci series:

The Fibonacci number series has first two numbers equal to $1$ and $0$. and next onwards each number is the addition of previous two numbers.

1st number = $0$
2nd number = $1$
3rd number = $0+1=1$
4th number = $1+1=2$
5th number = $1+2=3$
6th number = $2+3=5$
cont...

The fibonacci sequence is : $0,1,2,3,5,8,13,21, ...$





Algorithm:

  1. Start the program
  2. Read the range of sequence
  3. Initialize start to $0$ and next to $1$
  4. Get fibonacci number by adding the start with next
  5. Assign next to start
  6. Assign next to fibonacci 
  7. Stop the program when reach the desired range

#include "iostream"
    using namespace std;

    int main()
    {
     int range, start = 0, next = 1, fibonacci=0;
     cout<<"Enter the range for fibonacci sequence: ";
     cin >> range;
     cout<<"Fibonacci series upto "<<range<<"terms:"<<endl;
     for ( int c = 0 ; c < range ; c++ )
       {
          if ( c <= 1 )
             fibonacci = c;
          else
          {
             fibonacci = start + next;
             start = next;
             next = fibonacci;
          }
          cout<<fibonacci<<" ";
       }
       return 0;
    }


INPUT :      Enter the range for fibonacci sequence: 15
OUTPUT :  Fibonacci series upto 15 terms:
                    0 1 1 2 3 5 8 13 21 34 55 89 144 233 377


Thursday 10 December 2015

Area and perimeter of a rectangle using C++



Rectangle: It is a quadrilateral which has four right (90 degree) angle triangle.

Area of the rectangle:    $height \times width$
Perimeter of the rectangle :  $ 2 \times (height + width) $

Algorithm: 

  1. Start
  2. Enter the height and width of the rectangle
  3. Area = $height \times width$
  4. Circumference =  $ 2 \times (height + width) $
  5. Stop




#include "iostream"

using namespace std;

int main()

{

    int width,height,area,perimeter;

    cout<<"Enter  width of rectangle : ";

    cin >> width;

    cout<< "Enter height of rectangle : ";

    cin >> height;

    area = height * width;
    cout<<"Area of rectangle : "<<area<<endl;

    perimeter=2*(height+width);

    cout<< "Perimeter of rectangle : "<<perimeter<<endl;

return (0);

}


INPUT :       Enter the width of rectangle: 12
                     Enter the height of rectangle: 10
OUTPUT :  Area of the rectangle: 120
                    Perimeter of the rectangle: 44


Factorial of a number using C++



Factorial of a numebr $n$:

Factorial of a number $n$ is the product of the numbers from $1$ upto $n$. It is denoted by $n!$.

Example:  Factorial of  $3$ is $3! = 1 \times 2 \times 3 = 6 $
                 Factorial of $5$ is $5! = 1 \times 2 \times 3 \times 4 \times 5 = 120 $



Algorithm : 

  1. Start the program
  2. Read the number $n$
  3. Initialize $i = 1$ and $factorial =1$
  4. Multiply factorial with $i$ and store at factorial
  5. Increment the $i$
  6. Perform the step 4 and 5 until $i = n $
  7. Stop the program



#include"iostream"

using namespace std;

int main()

{

    int num, factorial=1;

    cout<<" Enter a number to find the factorial :  ";

    cin>>num;

    for(int i=1;i<=num; i++)

    {

        factorial=factorial*i;

    }

cout<<"Factorial of the number is ="<<factorial << endl;
return 0;

}


INPUT :      Enter the number to find the factorial : 8
OUTPUT :  Factorial of the number is = 40320


Decimal to binary conversion using C++



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

Binary System: It is also called base $2$ system. It uses two different symbols $0$ or $1$. 


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






#include "iostream"

using namespace std;

int main()
{
  int n,d,a, c, k;

  cout<<"Enter a decimal number :";
  cin>>d;
  n=d;
  cout<<"Binary equivalent of the given number : ";

 for( a=1;n!=0;a++)

  {
     n=n/2;

  }

a=a-2;
  for (c = a; c >= 0; c--)
  {
    k = d >> c;

    if (k & 1)
      cout<<"1";
    else
      cout<<"0";
  }



  return 0;
}
INPUT:       Enter a decimal number : 17
OUTPUT:   Binary equivalent of the given number : 10001


Tuesday 8 December 2015

Prime number detection using C++




Prime Number: It is a natural number other than $1$ which has only two divisors, $1$ and itself.

Examples:

$3$ is a prime number. It has two divisors $1$ and $3$ itself.
$13$ is a prime number. It has two divisors $1$ and $13$ itself.

Algorithm:

Steps:
  1. Take an integer to check whether it is prime or not.
  2. Divide the integer by the all the numbers from 1 to till that number
  3. Count the number of times the integer is divisible
  4. If the count is 2 then it is a prime number
  5. Otherwise it is not a prime number.

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

using namespace std;

        int main()
        {

         int n, count=0;

        cout<< "Enter a number to check prime : ";
        cin>>n;

        for(int a=1;a<=number; a++)
            {
                if(number%a==0)
                {
                    count++;
                }
            }
        if(count==2)
            {
                cout<<" PRIME NUMBER \n";
            }
        else
            {
                cout<<" NOT A PRIME NUMBER \n";
            }

       return (0);
    }


INPUT:     Enter a number to check prime : $7$
OUTPUT: PRIME NUMBER

INPUT:       Enter a number to check prime : $15$
OUTPUT:  NOT A PRIME NUMBER

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);
}