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



Friday, 19 September 2014

Array insertion and deletion operation in C programming

In array we hold elements. Now in programming some time you may need to add new value in array and delete old value from array. We can do that by some simple coding. But first we need to know insertion and deletion of new value in an array.
When we want to insert new element in array first we need to clear the position of array where we want to add new element. Then shift other element at the right side of target position by one to the right. Then add new element to that target position. For example
25 35 45 87 99
We want to insert new element at position 3, So first we need to shift other element at the right side of target position by 1 to the right.
25 35 _ 45 87 99
Now we put new element at target position, 79 is our new element
25 35 79 45 87 99
Now, for deletion of old element first we find the position of that old element. Then we need to shift other element at the right side of target position by 1 to the left. The element will deleted automatically. For example
25 35 45 87 99
Suppose we want to delete 45 then just move other element to the left by 1, for this case 87 and 99 at the right side of target position. So then move to the left of their position by 1. Then it become
25 35 87 99
Now let’s write the program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int arr[100],val,pos,i,p=0,length,ch,count=0,j;
int insert(int);
int deletion(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();
    }
    printf("Enter the elments of the array:\n");
    for(i=0;i<length;i++)
    {
        printf("\n%d position's:",p++);
        scanf("%d",&arr[i]);
    }
    length--;
    display(length);
    while(1)
    {
        printf("\nChose your option:1:insertion 2:deletion 3:display 4:Exit\n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:
            printf("enter the position you want to insert a value(position should be counted from 0):");
            scanf("%d",&pos);
            insert(pos);
            break;
            case 2:
            printf("Enter the value,which you want to delete:");
            scanf("%d",&val);
            deletion(val);
            break;
            case 3:
            display(length);
            break;
            case 4:
            exit(0);
            default:
            printf("Wrong choice....Pls Enter A valid entity...");
            switch(ch);
        }
    }getch();
}
int display(int length)
{
    printf("\nNow the array is:\n");
    for(i=0;i<=length;i++)
    {
        printf("%d\t",arr[i]);
    }
}
int insert(int pos)
{
    if(pos<=length+1)
    {
        if(pos<=100 && length<=100)
        {
            printf("Enter the value you want to insert:");
            scanf("%d",&val);
            length++;
            for(i=length;i>pos;i--)
            {
                arr[i]=arr[i-1];
            }
            arr[pos]=val;
        }
        display(length);
    }
    else
    {
        printf("Invalid position or length...");
        switch(ch);
    }
}
int deletion(int val)
{
    for(i=0;i<=length;i++)
    {
        if(arr[i]==val)
        {
            j=i;
            while(j<=length)
            {
                arr[j]=arr[j+1];
                j++;
            }
            length--;
            count++;
            i--;
        }
    }
    if(count!=0)
    {
        printf("\n%d value(s) have deleted",count);
    }
    else
    {
        printf("you have entered a wrong value");
    }
    count=0;
    display(length);
    switch(ch);
}
In above program we use switch-case for insertion and deletion operation. We can do those operation many times. Now let’s see the output
enter the length of the arry:(<=100)5
length successfully entered.And the legth is:5
Enter the elments of the array:

0 position's: 55
1 position's: 12
2 position's: 89
3 position's: 36
4 position's: 41
Now the array is:
55 12 89 36 41
Chose your option:1:insertion 2:deletion 3:display 4:Exit
1
enter the position you want to insert a value(position should be counted from 0):2
Enter the value,which you want to delete:75
Now the array is:
55 12 75 89 36 41
Chose your option:1:insertion 2:deletion 3:display 4:Exit
2
Enter the value,which you want to delete: 36
1 value(s) have deleted
Now the array is:
55 12 75 89 41
Chose your option:1:insertion 2:deletion 3:display 4:Exit
3
Now the array is:
55 12 75 89 41
Chose your option:1:insertion 2:deletion 3:display 4:Exit
4



Thursday, 18 September 2014

Reverse a number in C++ programming

In programming we can reverse a number, reversing a number means if we enter a number for example 123, after reversing that number become 321. We going to do that using C++ programming. Now we need to know some points before we write a program. The reverse of a single digit is same as the number itself. For example we can say that reverse of 5 is also 5. Also reverse of a palindrome number is gives output same as original number. For example reverse of 121 is also 121.
Now let’s write the program
#include<iostream>
using namespace std;
main()
{   int i,j,n;
    cout<<"Enter Your Number \n";
    cin>>i;
    n=i;
    j=0;
    while(n!=0)
    {   j=(j*10)+(n%10);
        n=n/10;
    }
    cout<<"After Reverse your number we get "<<j; ;
}
In above program we reverse any number. First we take every digit and reverse its location. Now let’s see the output
Enter Your Number
123
After Reverse your number we get 321
Enter Your Number
121
After Reverse your number we get 121

Monday, 15 September 2014

Use break and continue in C programming

“break” and “continue” are the keyword in C language. They are used to control loop structure. In programming somewhere in loop where you want to stop loop if some condition is become true or false then we will use break; to stop that loop. Same way if you want to skip some part of your loop if your condition is become true or false, then we will use continue; to skip those part.
Now let’s write the program for break;
#include<stdio.h>
void main()
{
    int i;
    for(i=1;i<=5;i++)
    {
        if(i==3)
        {
            break;
        }
        printf("%d\n",i);
    }
}
Now here we can see that we use break if any time in loop “i==3” the loop will stop executing. Now let’s see the output of above program.
1
2
Now let’s write the same program for continue;
#include<stdio.h>
void main()
{
    int i;
    for(i=1;i<=5;i++)
    {
        if(i==3)
        {
            continue;
        }
        printf("%d\n",i);
    }
}
Now here we can see that we use continue if any time in loop “i==3” the loop will skip that part. Now let’s see the output of above program.
1
2
4
5


Saturday, 13 September 2014

Check leap year using C++ programming

A leap year is a year where we add an extra day with our year, normally a year is end with 365 days, but a leap year is end with 366 day. We add that extra day with February. To know more about leap years check Wikipedia.
Now, we can find out that a year is leap year or not using programming, let’s see the code
#include<iostream>
using namespace std;
main()
{
  int year;

  cout<<"Enter a year to check\n";
  cin>>year;
  if ( year%400 == 0)
    cout<<year<<" is a leap year";
  else if ( year%100 == 0)
    cout<<year<<" is not a leap year";
  else if ( year%4 == 0 )
    cout<<year<<" is a leap year";
  else
    cout<<year<<" is not a leap year";
}
The above program is stands on simple logic of leap year. Now let’s see the output
Enter a year to check
2012
2012 is a leap year

Wednesday, 3 September 2014

Check whether a number is Armstrong Number or not using C++ programming

A number is Armstrong number only if the sum of cubes of every individual digits of that number is equal to the number itself. For example 153 is an Armstrong number as 13 + 53 + 33 = 153. Some other Armstrong numbers are: 0, 1, 153, 370, 407. But 123 is not a Armstrong number as 13 + 23 + 33 = 36.
Let’s write the program
#include<iostream>
using namespace std;
main()
{   int i=0,n,m,k;
    cout<<"Enter Your Number";
    cin>>n;
m=n;
while(n!=0)
    {   k=(n%10);
        i=i+k*k*k;
        n=n/10;
    }
    if(i==m)
    {   cout<<"This number is Armstrong";
    }
    else
    {   cout<<"This number is Not Armstrong";
    }
}
The output of above program is

Enter Your Number 153
This number is Armstrong

Enter Your Number 123
This number is Not Armstrong


Find out LCM and GCD using C++ programming

LCM-The least common multiple of two integers a and b, usually denoted by LCM(a, b), is the smallest positive integer that is divisible by both a and b.
Let’s write the program
#include<iostream>
using namespace std;
int fun(int ,int );
main()
{
    int a,b,k;
    cout<<"Enter your numbers : \n";
    cin>>a;
    cin>>b;
    k=fun(a,b);
    cout<<"Lowest common multiple is "<<k;

}
int fun(int a,int b)
  {
    int n;
    for(n=1;;n++)
    {
  if(n%a == 0 && n%b == 0)
   return n;
    }
  }
The output of above program
Enter your numbers :
4
5
Lowest common multiple is 20
GCD- The greatest common divisor of two positive integers a and b, usually denoted by GCD(a, b),is the largest divisor common to a and b.
Let’s write the program
#include<iostream>
using namespace std;
int fun(int ,int );
main()
{
    int a,b,k;
    cout<<"Enter your numbers : \n";
    cin>>a;
    cin>>b;
    k=fun(a,b);
    cout<<"Greatest Common Divisor is "<<k;

}
  int fun(int a,int b)
  {
    int c;
    while(1)
    {
  c = a%b;
  if(c==0)
   return b;
  a = b;
  b = c;
    }
  }
The output of above program
Enter your numbers :
40
35
Greatest Common Divisor is 5