Thursday, 16 October 2014

Compute nCr using C++ programming

nCr is an important function for mathematical operations. It’s mainly used in Binomial Distribution or Permutation and Combinations or Statistic Binomial Distribution. Now let’s see the mathematical expression for nCr

For example if we write 5C2 then this expression becomes

So, in our program we are going to calculate nCr. Now let’s write the code
#include<iostream>
using namespace std;
int fact(int );
main()
{
    int n,r;
    float ncr,a,b;
    cout<<"Insert the value of n";
    cin>>n;
    cout<<"Insert the value of r";
    cin>>r;
    if(r<n)
    {
    a=fact(n);
    b=fact(n-r)*fact(r);
    ncr=a/b;
    cout<<"Answer is: "<<ncr;
    }
    else
    {
        cout<<"r must be less then n";
    }
}
int fact(int i)
{   int m=1,j;
    for(j=1;j<=i;j++)
    {
        m=m*j;
    }
    return(m);
}
Now let’s see the output of this program
Insert the value of n 5
Insert the value of r 2
Answer is: 10

Saturday, 11 October 2014

Differences between C and C++


C and C++ are very important programming languages. As C is older than C++ there some difference between C and C++ language. They are:
1. C is Procedural Language but C++ is  multi-paradigm language i.e. procedural as well as object oriented.

2. The concept of virtual Functions are used in C++ but No virtual Functions are present in C.

3. In C Top down approach is used in Program Design where in C++ Bottom up approach adopted in Program Design.

4. No namespace Feature is present in C Language as it is not needed but Namespace Feature is present in C++ for avoiding Name collision example "using namespace std;".

5. Multiple Declaration of global variables are alloed in C but Multiple Declaration of global varioables are not allowed in C++.

6. In C scanf() Function used for Input and printf() Function used for output but in C++ Cin>> Function used for Input and Cout<< Function used for output.

7. Polymorphism is not possible in C but in C++ the concept of polymorphism is used.

8. Operator overloading is not possible in C where Operator overloading is a Feature of C++.

9. Mapping between Data and Function is difficult and complicated in C programming Mapping between Data and Function can be used using "Objects" in C++ programming.

10. In C, we can call main() Function through other Functions in that programming but in C++, we cannot call main() Function through other functions.

11. No inheritance is possible in C where Inheritance is possible in C++.

12. In C, malloc() and calloc() Functions are used for Memory Allocation and free() function for memory Deallocating where In C++,  new and delete operators are used for Memory Allocating and Deallocating.

13. In C, Exception Handling is not present where in C++, Exception Handling is done with Try and Catch block.

14. C requires all the variables to be defined at the starting of a scope C++ allows the declaration of variable anywhere in the scope i.e at time of its First use.

15. We can use functions inside structures in C++ programming but not in C programming.

16. We can say C is low level language but C++ is middle-level language.

17. C++ supports Exception Handling while C does not.

18. In case of C, the data is not secured but for C++, the data is secured.

Friday, 10 October 2014

Create a simple calculator using java programming

Creating basic calculator using java is the best way to know some basic arithmetic operations like addition, subtraction, multiplication, division etc.
Let’s write the code
class Calc{
int c;
void add(int a,int b)
{ c=a+b;
System.out.println("Add = "+c);
}
void sub(int a,int b)
{ c=a-b;
System.out.println("Sub = "+c);
}
void div(int a,int b)
{ c=a/b;
System.out.println("Div = "+c);
}
void mul(int a,int b)
{ c=a*b;
System.out.println("Mul = "+c);
}
}
class Cal{
public static void main(String args[])
{
int a=10;
int b=2;
Calc ob=new Calc();
ob.add(a,b);
ob.sub(a,b);
ob.div(a,b);
ob.mul(a,b);
}
}
In above program we are we use two classes. In main class we take two integer “a” and “b” then complete addition, subtraction, division, multiplication operation on it.
Now let’s see output
Add = 12
Sub = 8
Div = 5
Mul = 20


Friday, 26 September 2014

Find out largest and smallest element in an array using C++ programming

An array can store more than one element in it. In programming you may need find  out the largest and the smallest integer or float element in it. We are going to do that using C++ programming language.
Let’s write the program
#include<iostream>
using namespace std;
main()
{
    int a[10],i,n,big,small;
    cout<<"how many element you want to enter : ";
    cin>>n;
    cout<<"Enter Your Value in Array\n";
    for(i=0;i<n;i++)
    {
        cout<<"Enter Your Value ";
        cin>>a[i];
    }
    big=a[0];
    for(i=0;i<n;i++)
    {
        if(a[i]>=big)
        {
            big=a[i];
        }
    }
    small=a[0];
    for(i=0;i<n;i++)
    {
        if(a[i]<=small)
        {
            small=a[i];
        }
    }
    cout<<"Largest One is "<<big<<"\n";
    cout<<"Smallest One is "<<small;
}
In above program first we set a value that considered as first biggest or first smallest integer value, then we check if there any other element that is bigger or smaller than stored value and if found then just replace it and continue the test. When control check that all element in array is compared then it shows the final result.
The output of above program
how many element you want to enter : 5
Enter Your Value in Array
Enter Your Value 22
Enter Your Value 54
Enter Your Value 31
Enter Your Value 91
Enter Your Value 77
Largest One is 91
Smallest One is 22

Wednesday, 24 September 2014

Search an element in array

As we know an array can store more than one element.  In programming you may have to search that element in array. and then do some important operation.
Let’s write the program
#include<stdio.h>
#include<conio.h>
int arr[100],i,p=1,length;
int search(int);
int display(int);
void main()
{

    printf("enter the length of the arry:(<=100)");
    scanf("%d",&length);
    if(length<=100)
    {
    printf("length successfully entered.And the legth is:%d \n",length);
    }
    else
    {
    printf("length is invalid.\n");
    main();
    }
    length--;
    printf("Enter the elments of the array:\n");
    for(i=0;i<=length;i++)
    {
        printf("\n%d position's:",p++);
        scanf("%d",&arr[i]);
    }
    display(length);
search(length);

   getch();
}
int display(int length)
{
    printf("\nNow the array is:\n");
    for(i=0;i<=length;i++)
    {
        printf("%d\t",arr[i]);
    }
}
int search(int length)
{
   int c, first, last, middle, n, search;

    n=length;
    printf("\nEnter value to find\n");
    scanf("%d",&search);

    first = 0;
    last = n;
    middle = (first+last)/2;

    while( first <= last )
    {
        if ( arr[middle] < search )
        first = middle + 1;
        else if ( arr[middle] == search )
        {
            printf("%d found at location %d.\n", search, middle+1);
            break;
        }
        else
            last = middle - 1;

            middle = (first + last)/2;
    }
    if ( first > last )
    printf("Not found! %d is not present in the list.\n", search);

   return 0;
}
In above program we take an array and put element in it. Then we search for an element which is already in that array. If given element does not match with any element in array then it will show not found.
Now let’s see output
enter the length of the arry:(<=100)5
length successfully entered.And the legth is:5
Enter the elments of the array:
1 position's: 65
2 position's: 55
3 position's: 41
4 position's: 33
5 position's: 87
Now the array is:
65 55 41 33 87
Enter value to find 41
41 found at location 3

Tuesday, 23 September 2014

Addition and Subtraction operation of two matrix in C++ programming

In programming we can create matrix, and do operations on them. In this program we are going to do some Addition and Subtraction Operations. Before we start writing we need to know how to do that operations. Now for example we have two matrix. A and B. Now see how to do that operation.

Now as we can see in above picture that we take a third matrix C in which we are going to put our values. Same rule applied for Subtraction also. Now let’s write the program
#include<iostream>
using namespace std;
main()
{
    int i,j,a[40][40],b[40][40],c[40][40],d[40][40],lim;
    cout<<"Enter the order of the matrix";
    cin>>lim;
    cout<<"Enter the value in matrix A \n";
    for(i=1;i<=lim;i++)
    {
        for(j=1;j<=lim;j++)
        {
            cout<<"Enter the value in A["<<i<<"],["<<j<<"] ";
            cin>>a[i][j];
        }
    }
    cout<<"Enter the value in matrix B \n";
    for(i=1;i<=lim;i++)
    {
        for(j=1;j<=lim;j++)
        {
            cout<<"Enter the value in B["<<i<<"],["<<j<<"] ";
            cin>>b[i][j];
        }
    }
    for(i=1;i<=lim;i++)
    {
        for(j=1;j<=lim;j++)
        {
            c[i][j]=a[i][j]+b[i][j];
        }
    }
    cout<<"After adding two matrix \n";
    for(i=1;i<=lim;i++)
    {
        for(j=1;j<=lim;j++)
        {
            cout<<c[i][j]<<"\t";
        }
        cout<<"\n\n";
    }
        for(i=1;i<=lim;i++)
    {
        for(j=1;j<=lim;j++)
        {
            d[i][j]=a[i][j]-b[i][j];
        }
    }
    cout<<"After subtracting two matrix \n";
    for(i=1;i<=lim;i++)
    {
        for(j=1;j<=lim;j++)
        {
            cout<<d[i][j]<<"\t";
        }
        cout<<"\n\n";
    }

}
In above program we calculate addition using array C and subtraction using array D. Now let’s see the output
Enter the order of the matrix2
Enter the value in matrix A
Enter the value in A[1],[1] 2
Enter the value in A[1],[2] 3
Enter the value in A[2],[1] 5
Enter the value in A[2],[2] 2
Enter the value in matrix B
Enter the value in B[1],[1] 1
Enter the value in B[1],[2] 4
Enter the value in B[2],[1] 3
Enter the value in B[2],[2] 6
After adding two matrix
3 7
8 8
After subtracting two matrix
1 -1
2 -4



Saturday, 20 September 2014

Swap value between two Arrays in C++ programming

In our previous programming we know how swap variables value. Now we will try same program but using Array. As we know that an array is set of data. So when we are talking about swap value between arrays, so in this case we need to swap more than 1 value. For example there are two array A[] and B[],
A[]=1,2,3 and B[]=4,5,6
So after swap we get
A[]=4,5,6 and B[]=1,2,3
Now let’s write the program
#include<iostream>
using namespace std;
main()
{
    int a[10],b[10],i;
    cout<<"Enter the Element in Array 1\n";
    for(i=0;i<4;i++)
    {
        cout<<"Enter your value: ";
        cin>>a[i];
    }
    cout<<"Enter the Element in Array 2\n";
    for(i=0;i<4;i++)
    {
        cout<<"Enter your value: ";
        cin>>b[i];
    }
    cout<<"Before swap\n";
    for(i=0;i<4;i++)
    {
        cout<<"a"<<i<<"= "<<a[i]<<"\n";
    }
    cout<<"and\n";
    for(i=0;i<4;i++)
    {
        cout<<"b"<<i<<"= "<<b[i]<<"\n";
    }
    int temp;
    for(i=0;i<4;i++)
    {
        temp=a[i];
        a[i]=b[i];
        b[i]=temp;
    }
    cout<<"After swap\n";
    for(i=0;i<4;i++)
    {
        cout<<"a"<<i<<"= "<<a[i]<<"\n";
    }
    cout<<"and\n";
    for(i=0;i<4;i++)
    {
        cout<<"b"<<i<<"= "<<b[i]<<"\n";
    }
}
In above program we set the array size to 4 for both of them. Now it is important to have same size for both of them. If size not match then this program should return error. Now let’s see the output
Enter the Element in Array 1
Enter your value: 1
Enter your value: 2
Enter your value: 3
Enter your value: 4
Enter the Element in Array 2
Enter your value: 5
Enter your value: 6
Enter your value: 7
Enter your value: 8
Before swap
a0= 1
a1= 2
a2= 3
a3= 4
and
b0= 5
b1= 6
b2= 7
b3= 8
After swap
a0= 5
a1= 6
a2= 7
a3= 8
and
b0= 1
b1= 2
b2= 3
b3= 4