Thursday, March 15, 2012

Program to real roots of quadratic equation with output

// d = b^2 - 4ac

#include <iostream>
#include <cmath>          //when we used c library in c++
using namespace std;

int main()
{
    float a, b, c, d, root1, root2;


    cout << "\n Enter the value of a, b, c: " ;
    cin >> a >> b >> c;
 

    d = b*b - 4*a*c;
    cout << "\n discriminant is: " << d ;
   
    if ( d<0 )
    {
        cout << "\n Roots are imagenary.";
    }
    else
    {
        root1 = (-b + sqrt(d)) / (2*a);
        root2 = (-b - sqrt(d)) / (2*a);
        cout << "\n root1 is: " << root1 << "\n root2 is: " << root2 << endl;
    }
    return 0;
}


OUTPUT:

Tuesday, March 13, 2012

Program to print star pattern in C++

#include <iostream>
using namespace std;

int main()
{
    int i,j;
   
    for(i=1; i<=5; i++)
    {
        cout << "\n";
       
        for(j=1; j<=i; j++)
        {
            cout << "*";
        }
    }
   
    return 0;
}


OUTPUT:
 

Thursday, March 8, 2012

A simple program to find factorial of a number

 // A simple program to find factorial of a number
#include<iostream>
using namespace std;

int main()
{
    int  a;
    cout << "\n Enter the no. to find factorial: ";
    cin >> a;


        int fact=1;
        for(int i=1;i<=a;i++)
        {
            fact=i*fact;
        }
   
    cout << "\n\n FACTORIAL= " << fact;
}

____________________________________________________________________________________
// program to find factorial of a number using functions
#include<iostream>
using namespace std;

int factorial(int);                //declaration of a function named factorial

int main()
{
    int fact, a;
    cout << "Enter the no.  to find factorial: ";
    cin >> a;

    fact = factorial(a);
    cout << "\n\n FACTORIAL= " << fact;
   
}

int factorial(int x)               //definition above declared function is given here
    {
        int n=1;
        for(int i=1;i<=x;i++)
        {
            n=i*n;
        }
    return (n);
}

___________________________________________________________________________________
// factorial of 1 to 10 (first 10) natural number
#include<iostream>
using namespace std;

int main()
{
    int  b;
   
    for(b=1; b<=10; b++)
    {
        long int fact = 1;
        for(int i=1;  i<=b;  i++)
        {
            fact=i*fact;
        }
        cout<< "\n\nFACTORIAL of: " << b << " is " << fact;
    }   
}

Friday, March 2, 2012

Program to find out the sum of an integer array

#include <iostream>
using namespace std;

int main()
{
    int a[]={4, 6, 25, 8, 5};
    int sum=0;


    for (int i=0; i<5; i++)
    {
        sum = sum + a[i];
    }
 

    cout << sum;
}