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