5/01/2009

C Program Tower Of Hanoi


/*C Program Tower Of Hanoi*/
#include<stdio.h>
#include<conio.h>
void TOH(int,char,char,char);
void main()
{
    int n;

    printf("Enter number of disks:\n");
    scanf("%d",&n);
    printf("Tower of Hanoi for %d disk:\n",n);
    TOH(n,'a','b','c');

}
void TOH(int n,char a,char b,char c)
{
    if(n<=0)
        printf(" wrong i/p \n");
    else if(n==1)
        printf(" move disk from tower %c to %c \n",a,c);
    else
    {
        TOH(n-1,a,c,b);
        TOH(1,a,b,c);
        TOH(n-1,b,a,c);
    }
}
Out Put:
Enter number of disks:
3
Tower of Hanoi for 3 disk:
 move disk from tower a to c
 move disk from tower a to b
 move disk from tower c to b
 move disk from tower a to c
 move disk from tower b to a
 move disk from tower b to c
 move disk from tower a to c

C Program Stack Operation


/*C Program Stack Operation*/
#include<stdio.h>
#include<conio.h>
#define SIZE 5
int top=-1,S[SIZE];
void push()
{
    int item;
    if(top==SIZE-1)
        printf("stack overflow \n");
    else
    {
        printf(" enter the element \n");
        scanf("%d",&item);
        top=top++;
        S[top]=item;
    }
}
void pop()
{
    int x;
    if(top==-1)
    {
        printf("stack is underflow!\n");
        return;
    }
    x=S[top];
    printf("poped element=%d \n",x);
    top--;
}
void display()
{
    int i;
    if(top==-1)
    {
        printf(" stack is underflow!\n");
        return;
    }
    printf(" stack contents are:\n");
    for(i=0; i<=top; i++)
    {
        printf("%d \n",S[i]);
    }
}
void main()
{
    int ch;

    for(;;)
    {
        printf("1.PUSH\t2.POP\t3.DISPLAY\t4.EXIT\n");
        printf("enter the choice: \n");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:
            push();
            break;
        case 2:
            pop();
            break;
        case 3:
            display();
            break;
        default:
            exit(0);
        }

    }
}
Out Put:
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
1
 enter the element
1
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
1
 enter the element
2
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
1
 enter the element
3
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
1
 enter the element
4
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
1
 enter the element
5
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
1
stack overflow
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
3
 stack contents are:
1
2
3
4
5
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
2
poped element=5
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
2
poped element=4
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
2
poped element=3
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
2
poped element=2
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
2
poped element=1
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
2
stack is underflow!
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
4

C Program Selection Sort


/*C Program Selection Sort*/
#include<stdio.h>
#include<conio.h>
void main()
{
    int a[20],i,j,n,small,position;

    printf("Enter the number of elements:\n");
    scanf("%d",&n);
    printf("Enter the elements: \n");
    for(i=0; i<n; i++)
        scanf("%d",&a[i]);
    for(i=0; i<n-1; i++)
    {
        small=a[i];
        position=i;
        for(j=i+1; j<n; j++)
            if(small>a[j])
            {
                small=a[j];
                position=j;
            }
        a[position]=a[i];
        a[i]=small;
    }
    printf("Soretd list is: \n");
    for(i=0; i<n; i++)
        printf("%d \n",a[i]);

}
Out Put:
Enter the number of elements:
9
Enter the elements:
54
78
95
15
32
25
14
68
123
Soretd list is:
14
15
25
32
54
68
78
95
123

C Program Quick Sort


/*C Program Quick Sort*/
#include<stdio.h>
#include<conio.h>
int partition(int a[],int low,int high)
{
    int i,j,temp,key;
    key=a[low];
    i=low+1;
    j=high;
    while(1)
    {
        while(i<high&&key>=a[i])
            i++;
        while(key<a[j])
            j--;
        if(i<j)
        {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
        else
        {
            temp=a[low];
            a[low]=a[j];
            a[j]=temp;
            return j;
        }
    }
}
void quick_sort(int a[],int low,int high)
{
    int j;
    if(low<high)
    {
        j=partition(a,low,high);
        quick_sort(a,low,j-1);
        quick_sort(a,j+1,high);
    }
}
void main()
{
    int i,n,a[20];

    printf(" enter no. of ele \n");
    scanf("%d",&n);
    printf("enter the ele \n");
    for(i=0; i<n; i++)
        scanf("%d",&a[i]);
    quick_sort(a,0,n-1);
    printf("sorted list is \n");
    for(i=0; i<n; i++)
        printf(" %d \n",a[i]);

}
Out Put:
Enter number of elements:
8
enter the ele
32
45
8
15
78
122
35
47
sorted list is
 8
 15
 32
 35
 45
 47
 78
 122

C Program Queue Operation


/*C Program Queue Operation*/
#include<stdio.h>
#include<conio.h>
#define max 5
int r,f,x,item,q[max];
void insert()
{
    if(r==max-1)
    {
        printf("queue is overflow! \n");
        return;
    }
    else
    {
        printf(" enter the element: \n");
        scanf("%d",&item);
        r=r+1;
        q[r]=item;
    }
}
void delete()
{
    if(f>r)
    {
        printf(" queue is underflow! \n");
        return;
    }
    x=q[f];
    printf(" deleted element = %d\n",x);
    f=f+1;
}
void display()
{
    int i;
    if(f>r)
    {
        printf(" queue is underflow \n");
    }
    printf(" contents of the queue: \n");
    for(i=f; i<=r; i++)
    {
        printf(" %d \t",q[i]);
    }
    printf("\n");
}

void main()
{
    int ch;
    f=0;
    r=-1;

    while(1)
    {
        printf("1.insert\t2.delete\t3.display\t4.exit \n");
        printf(" enter the choice: \n");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:
            insert();
            break;
        case 2:
            delete();
            break;
        case 3:
            display();
            break;
        default:
            exit(0);
        }

    }
}
Out Put:
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
 enter the element:
1
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
 enter the element:
2
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
 enter the element:
3
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
 enter the element:
4
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
 enter the element:
5
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
queue is overflow!
1.insert        2.delete        3.display       4.exit
 enter the choice:
3
 contents of the queue:
 1       2       3       4       5
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 deleted element = 1
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 deleted element = 2
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 deleted element = 3
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 deleted element = 4
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 deleted element = 5
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 queue is underflow!
1.insert        2.delete        3.display       4.exit
 enter the choice:
4

C Program Merge Sort


/*C Program Merge Sort*/
#include<stdio.h>
#include<conio.h>
void merge(int a[10],int low,int mid,int high)
{
    int i=low;
    int j=mid+1;
    int k=low;
    int c[100];
    while(i<=mid&&j<=high)
    {
        if(a[i]<a[j])
        {
            c[k]=a[i];
            i=i+1;
            k=k+1;
        }
        else
        {
            c[k]=a[j];
            j=j+1;
            k=k+1;
        }
    }
    while(i<=mid)
    {
        c[k]=a[i];
        k=k+1;
        i=i+1;
    }
    while(j<=high)
    {
        c[k]=a[j];
        k=k+1;
        j=j+1;
    }
    for(i=low; i<=high; i++)
    {
        a[i]=c[i];
    }
}
void merge_sort(int a[],int low,int high)
{
    int mid;
    if(low<high)
    {
        mid=(low+high)/2;
        merge_sort(a,low,mid);
        merge_sort(a,mid+1,high);
        merge(a,low,mid,high);
    }
}
void main()
{
    int a[100],n,i;

    printf("Enter number of elements: \n");
    scanf("%d",&n);
    printf("enter the elements:\n");
    for(i=0; i<n; i++)
        scanf("%d",&a[i]);
    merge_sort(a,0,n-1);
    printf("sorted elements are:\n");
    for(i=0; i<n; i++)
        printf("%d\n",a[i]);

}
Out Put:
Enter number of elements:
9
enter the elements:
12
56
4
8749
32
48
2
15
98
sorted elements are:
2
4
12
15
32
48
56
98
8749

C Program Linear Search


/*C Program Linear Search*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
    int i,n,ele,a[20];

    printf("enter number of elements:\n");
    scanf("%d",&n);
    printf("enter %d elements\n",n);
    for(i=0; i<=n-1; i++)
        scanf("%d",&a[i]);
    printf("enter the element to be searched:\n");
    scanf("%d",&ele);
    for(i=0; i<=n-1; i++)
    {
        if(a[i]==ele)
        {
            printf("element found at %d position",i+1);
            exit(0);
        }
    }
    printf("element %d not found",ele);

}
Out Put:
enter number of elements:
9
enter 9 elements
45
65
9
78
15
24
35
67
123
enter the element to be searched:
78
element found at 4 position

C Program Insertion Sort


/*C Program Insertion Sort*/
#include<stdio.h>
#include<conio.h>
void main()
{
    int a[20],n,temp,i,j;

    printf("Enter the no of elements:\n");
    scanf("%d",&n);
    printf("Enter %d elements:\n",n);
    for(i=0; i<n; i++)
        scanf("%d",&a[i]);
    for(i=1; i<n; i++)
    {
        temp=a[i];
        j=i-1;
        while(temp<a[j]&&j>=0)
        {
            a[j+1]=a[j];
            j=j-1;
        }
        a[j+1]=temp;
    }
    printf("Sorted elements are:\n");
    for(i=0; i<n; i++)
        printf(" %d \n",a[i]);

}
Out Put:
Enter the no of elements:
8
Enter 8 elements:
21
23
48
95
64
51
79
321
Sorted elements are:
 21
 23
 48
 51
 64
 79
 95
 321

C Program Create a Linked List


/*C Program Create a Linked List*/
#include<stdio.h>
#include<malloc.h>
struct node
{
    int info;
    struct node *link;
};
struct node *getnode()
{
    return (struct node*)malloc(sizeof(struct node));
}
struct node *insert (int item,struct node *first)
{
    struct node *temp;
    temp=getnode();
    temp->info=item;
    temp->link=first;
    return temp;
}
void display(struct node *temp)
{
    printf("contents of Linked List\n");
    while(temp!=NULL)
    {
        printf("%d\t", temp->info);
        temp=temp->link;
    }
    printf("\n");
}
void main()
{
    struct node*first=NULL;
    int ch,item;

    while(1)
    {
        printf("1.insert\t2.display\n");
        printf("enter the choice:\n");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:
            printf("enter the item to be inserted:\n");
            scanf("%d",&item);
            first=insert(item,first);
            break;
        case 2:
            display(first);
            break;
        default:
            exit(0);
        }

    }
}
Out Put:
1.insert        2.display
enter the choice:
1
enter the item to be inserted:
1
1.insert        2.display
enter the choice:
1
enter the item to be inserted:
2
1.insert        2.display
enter the choice:
1
enter the item to be inserted:
3
1.insert        2.display
enter the choice:
1
enter the item to be inserted:
4
1.insert        2.display
enter the choice:
1
enter the item to be inserted:
5
1.insert        2.display
enter the choice:
2
contents of Linked List
5       4       3       2       1
1.insert        2.display
enter the choice:
3

C Program Circular Queue


/*C Program Circular Queue*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define size 3
int item,x,f,r,connt,i,cQ[size];
void insert()
{
    if(connt==size-1)
    {
        printf("overflow\n");
        return;
    }
    printf("enter elements to be inserted:\n");
    scanf("%d",&item);
    r=r+1;
    cQ[r]=item;
    r=r%size;
    connt++;
}
void delete()
{
    if(connt==-1)
    {
        printf("empty\n");
        return;
    }
    x=cQ[f];
    printf("deleted element: %d\n",x);
    f=f+1;
    f=f%size;
    connt--;
}
void display()
{
    if(connt==-1)
    {
        printf("empty\n");
        return;
    }
    printf("elements in the queue:\n");
    for(i=0; i<=connt; i++)
    {
        printf("%d\t",cQ[f++]);
        f=f%size;
    }
    printf("\n");
}
void main()
{
    int ch;

    f=0,r=-1,connt=-1;
    while(1)
    {
        printf("1.insert\t2.delete\t3.display\n");
        printf("enter the choice:\n");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:
            insert();
            break;
        case 2:
            delete();
            break;
        case 3:
            display();
            break;
        default:
            exit (0);
        }

    }
}
Out Put:
1.insert        2.delete        3.display
enter the choice:
1
enter elements to be inserted:
1
1.insert        2.delete        3.display
enter the choice:
1
enter elements to be inserted:
2
1.insert        2.delete        3.display
enter the choice:
1
enter elements to be inserted:
3
1.insert        2.delete        3.display
enter the choice:
1
overflow
1.insert        2.delete        3.display
enter the choice:
3
elements in the queue:
1       2       3
1.insert        2.delete        3.display
enter the choice:
2
deleted element: 1
1.insert        2.delete        3.display
enter the choice:
2
deleted element: 2
1.insert        2.delete        3.display
enter the choice:
2
deleted element: 3
1.insert        2.delete        3.display
enter the choice:
2
empty
1.insert        2.delete        3.display
enter the choice:
4

C program Binary Search


/*C program Binary Search*/
#include<stdio.h>
#include<conio.h>
void main()
{
    int array[10];
    int i, j, n, temp, num;
    int low,mid,high;

    printf("Enter the value of the array\t");
    scanf("%d",&n);
    printf("Enter the elements one by one:\n");
    for(i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    }
    printf("Input array elements\n");
    for(i=0; i<n; i++)
    {
        printf("%d\n",array[i]);
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<(n-i-1); j++)
        {
            if(array[j]>array[j+1])
            {
                temp=array[j];
                array[j]=array[j+1];
                array[j+1]=temp;
            }
        }
    }
    printf("Sorted array is...\n");
    for(i=0; i<n; i++)
    {
        printf("%d\n",array[i]);
    }
    printf("Enter the element to be searched\n");
    scanf("%d",&num);
    low=1;
    high=n;
    do
    {
        mid=(low+high)/2;
        if(num<array[mid])
            high=mid-1;
        else if(num>array[mid])
            low=mid+1;
    }
    while(num!=array[mid] && low<=high);
    if(num==array[mid])
    {
        printf("\n\t%d is present at position %d",num,mid+1);
    }
    else
    {
        printf("Search is FAILED\n");
    }

}
Output:
Enter the value of the array    8
Enter the elements one by one:
4
6
1
9
7
2
3
8
Input array elements
4
6
1
9
7
2
3
8
Sorted array is...
1
2
3
4
6
7
8
9
Enter the element to be searched
5
Search is FAILED

4/01/2009

C++ Program To Perform Arithmetic Operation Using Class And Object


/* C++ Program To Perform Arithmetic Operation Using Class And Object */

#include <iostream>

using namespace std;

class arithmetic
{
    float a,b,sum,div,mul,diff;
public:
    void getdata();
    void putdata();
};
void arithmetic::getdata()
{
    cout<<"Enter The Value For Two Numbers:"<<endl;
    cin>>a>>b;
}
void arithmetic::putdata()
{
    sum=a+b;
    cout<<"Sum Of Two Numbers:"<<sum<<endl;
    diff=a-b;
    cout<<"Difference Of Two Numbers:"<<diff<<endl;
    mul=a*b;
    cout<<"Product Of Two Numbers:"<<mul<<endl;
    div=a/b;
    cout<<"Division Result:"<<div<<endl;

}

int main()
{
    arithmetic a;
    a.getdata();
    a.putdata();

    return 0;
 }
Out Put:
Enter The Value For Two Numbers:
21
7
Sum Of Two Numbers:28
Difference Of Two Numbers:14
Product Of Two Numbers:147
Division Result:3

C++ Program To Calculate The Sum Of All Even And Odd Numbers Up to N


/* C++ Program To Calculate The Sum Of All Even And Odd Numbers Up to N */

#include <iostream>

using namespace std;

int main()
{
    int i=1,number,evensum=0,oddsum=0;
      cout<<"Enter The Number:"<<endl;
        cin>>number;
      int temp=number;
        while(i<=number)
        {
            if(i%2==0)
            {
                cout<<i;
                cout<<"\t";
                evensum=evensum+i;
                i++;
            }
            else
            {
                cout<<i;
                cout<<"\t";
                oddsum=oddsum+i;
                i++;
            }
        }

            cout<<"\nSum Of Even Numbers: "<<evensum<<endl;
            cout<<"Sum Of Odd Numbers: "<<oddsum<<endl;


    return 0;
 }
Out Put:
Enter The Number:
6
1       2       3       4       5       6
Sum Of Even Numbers: 12
Sum Of Odd Numbers: 9

C++ Program To Check Whether The Given Number Is Palindrome Or Not


/* C++ Program To Check Whether The Given Number Is Palindrome Or Not */

#include <iostream>

using namespace std;

int main()
{
    int number,rev=0;
      cout<<"Enter The Number:"<<endl;
        cin>>number;
      int temp=number;
        while(number>0)
        {
            int rem=number%10;
            number=number/10;
            rev=(rev*10)+rem;
        }
        if(rev==temp)
            cout<<"The Given Number "<<temp<<" Is A Palindrome Number."<<endl;
        else
            cout<<"The Given Number "<<temp<<" Is Not A Palindrome Number."<<endl;


    return 0;
 }
Out Put:
Enter The Number:
121
The Given Number 121 Is A Palindrome Number.

C++ Program To Find Minimum And Maximum Of Two Numbers Using Function


/* C++ Program To Find Minimum And Maximum Of Two Numbers Using Function */

#include <iostream>

using namespace std;

void min(int,int);
void max(int,int);
void max(int a,int b)
{
    float sum=0;
    if(a>b)
    {
        cout<<"Max of numbers is A:"<<a<<endl;
        cout<<"Min of numbers is B:"<<b<<endl;
    }
    else
        min(a,b);

}
void min(int a,int b)
{
        cout<<"Max of numbers is B:"<<b<<endl;
        cout<<"Min of numbers is A:"<<a<<endl;
}
int main()
{
    int a, b, c;
      cout<<"Enter The Values For A And B:"<<endl;
        cin>>a>>b;
    max(a,b);

    return 0;
 }
Out Put:
Enter The Values For A And B:
34
65
Max of numbers is B:65
Min of numbers is A:34

C++ Program To Find The Biggest Of 3 Given Numbers


/* C++ Program To Find The Biggest Of 3 Given Numbers */

#include <iostream>

using namespace std;

class Number
{
    int a,b,c;
public:
    void getdata();
    void showdata();
};
void Number::getdata()
{
    cout<<"Enter three Numbers:\n";
    cin>>a>>b>>c;
}
void Number::showdata()
{
    if((a>b)&&(a>c))
        cout<<"Biggest Number:"<<a<<endl;
    else if((b>a)&&(b>c))
        cout<<"Biggest Number:"<<b<<endl;
    else
        cout<<"Biggest Number:"<<c<<endl;
}

int main()
{
    Number m1;
    m1.getdata();
    m1.showdata();

    return 0;
}
Out Put:
Enter three Numbers:
54
58
98
Biggest Number:98

C++ Program To Calculate The Average Of 3 Numbers


/* C++ Program To Calculate The Average Of 3 Numbers */

#include <iostream>

using namespace std;

void total(int a,int b, int c)
{
    float sum=0;
    sum=a+b+c;
        cout<<"Sum Of Numbers:"<<sum<<endl;
}
void average(int a,int b, int c)
{
    float avg=(a+b+c)/3.0;
        cout<<"Average Of Numbers:"<<avg<<endl;
}
int main()
{
    float a, b, c;
      cout<<"Enter Three Numbers:"<<endl;
        cin>>a>>b>>c;
    total(a,b,c);
    average(a,b,c);

    return 0;
 }
Out Put:
Enter Three Numbers:
10
20
30
Sum Of Numbers:60
Average Of Numbers:20

3/19/2009

C Program To Find Mean, Median, Standard Deviation And Mode For A Given Set Of Values


/* C Program To Find Mean, Median, Standard Deviation And Mode For A Given Set Of Values */


#include<stdio.h>
#include<math.h>
#include<conio.h>

void main()
{
    int a[10],b[10],i,j,k=0,temp,sum=0,n,choice,count=1,max=0,mode;

    float mean=0,median=0,sd,x,add=0,adds=0;
    printf("Enter the size of array:\n");
        scanf("%d",&n);
    printf("Enter The elements:\n");
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
      printf("Elements before sorting:\n");
         for(i=0;i<n;i++){
            printf("%d\n",a[i]);
         }

      for(i=0;i<n;i++)
      {
          for(j=0;j<n-1;j++)
          {

              if(a[j]>a[j+1])
              {
                  temp=a[j];
                  a[j]=a[j+1];
                  a[j+1]=temp;
              }
          }
      }
      printf("Sorted Elements:\n");
         for(i=0;i<n;i++)
            printf("%d\n",a[i]);


      for(;;)
      {
          printf("1.Mean\t2.Median\t3.Standard Deviation\t4.Mode\t5.Exit\n");
            scanf("%d",&choice);

           switch(choice)
           {
           case 1:
            for(i=0;i<n;i++)
            {
                sum+=a[i];
                mean=(float)sum/n;
            }
            printf("Mean= %.2f\n",mean);
            break;
           case 2:
               if(n/2==0){
                median=a[n/2];
                printf("Median= %f\n",median);
                break;
               }
               else
                median=(a[n/2]+a[n/2]+1)/2;

               printf("Median= %.2f\n",median);
               break;
            case 3:
            for(i=0;i<n;i++)
               add+=a[i];
                mean=add/n;
            for(i=0;i<n;i++)
                    adds+=adds+((a[i]-mean)*(a[i]-mean));
            x=adds/n;
            sd=sqrt(x);
            printf("Standard deviation= %.2f\n",sd);
            break;

            case 4:
                for(i=0;i<n-1;i++)
                    {
                        mode=0;
                    for(j=i+1;j<n;j++)
                    {
                        if(a[i]==a[j])
                        {
                            mode++;
                        }
                    }
                if((mode>max)&&(mode!=0))
                    {
                        k=0;
                        max=mode;
                        b[k]=a[i];
                        k++;
                    }
                else if(mode==max)
                    {
                        b[k]=a[i];
                        k++;
                    }
                }
            for(i=0;i<n;i++)
                {
                    if(a[i]==b[i])
                    count++;
                }
            if(count==n)
            printf("There is no mode!\n");
            else
            {
                printf("Mode:\n");
                for(i=0;i<k;i++)
                printf("%d\n",b[i]);
            }

                break;
            case 5:
                exit(0);

            default:
                printf("Invalid Choice!\n");
                break;
           }
      }

}
Out Put:
Enter the size of array:
6
Enter The elements:
2
3
4
8
2
5
Elements before sorting:
2
3
4
8
2
5
Sorted Elements:
2
2
3
4
5
8
1.Mean  2.Median        3.Standard Deviation    4.Mode  5.Exit
1
Mean= 4.00
1.Mean  2.Median        3.Standard Deviation    4.Mode  5.Exit
2
Median= 4.00
1.Mean  2.Median        3.Standard Deviation    4.Mode  5.Exit
3
Standard deviation= 6.03
1.Mean  2.Median        3.Standard Deviation    4.Mode  5.Exit
4
Mode:
2
1.Mean  2.Median        3.Standard Deviation    4.Mode  5.Exit
5

3/15/2009

C Program To Solve Equation Using GAUSS SEIDEL METHOD


/*C Program To Solve Equation Using GAUSS SEIDEL METHOD*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.0001
#define X1(x2,x3) ((17 - 20*(x2) + 2*(x3))/20)
#define X2(x1,x3) ((-18 - 3*(x1) + (x3))/20)
#define X3(x1,x2) ((25 - 2*(x1) + 3*(x2))/20)


void main()
{
  double x1=0,x2=0,x3=0,y1,y2,y3;
  int i=0;
  clrscr();
  printf("\n__________________________________________\n");
  printf("\n   x1\t\t   x2\t\t   x3\n");
  printf("\n__________________________________________\n");
  printf("\n%f\t%f\t%f",x1,x2,x3);
  do
  {
   y1=X1(x2,x3);
   y2=X2(y1,x3);
   y3=X3(y1,y2);
   if(fabs(y1-x1)<ESP && fabs(y2-x2)<ESP && fabs(y3-x3)<ESP )
   {
     printf("\n__________________________________________\n");
     printf("\n\nx1 = %.3lf",y1);
     printf("\n\nx2 = %.3lf",y2);
     printf("\n\nx3 = %.3lf",y3);
     i = 1;
   }
   else
   {
     x1 = y1;
     x2 = y2;
     x3 = y3;
     printf("\n%f\t%f\t%f",x1,x2,x3);
   }
  }while(i != 1);
getch();
}
Out Put:
__________________________________________

   x1              x2              x3

__________________________________________

0.000000         0.000000       0.000000
0.850000        -1.027500       1.010875
1.978588        -1.146244       0.880205
2.084265        -1.168629       0.866279
2.105257        -1.172475       0.863603
2.108835        -1.173145       0.863145
2.109460        -1.173262       0.863065
2.109568        -1.173282       0.863051
__________________________________________


x1 = 2.110

x2 = -1.173

x3 = 0.863

3/10/2009

C Program To Find The Root Of The Equation x*x*x-18=0 Using Regula Falsi Method


/* C Program To Find The Root Of The Equation x*x*x-18=0 Using Regula Falsi Method */


#include<stdio.h>
#include<math.h>
#include<conio.h>

float f(float);
int main()
{
    float x0,x1,x2,y0,y1,y2,e;
    int i,n;

      printf("Enter the initial guess\n");
          scanf("%f%f",&x0,&x1);
       y0=f(x0);
       y1=f(x1);

       if((y0*y1)>=0)
       {
           printf("Invalid guess.\n");
           exit(0);
       }
      printf("Enter the allowable error:\n");
          scanf("%f",&e);
      printf("Enter the number of iterations:\n");
          scanf("%d",&n);

      printf("f(x)=x*x*x-18\n");
      printf("-------------------------------------------------------\n");
      printf("i\tx0\tx1\ty0\ty1\tx2\ty2\n");
      printf("-------------------------------------------------------\n");
      for(i=0;i<n;i++)
      {
          x2=((x0*y1)-(x1*y0))/(y1-y0);
          y2=f(x2);

          printf("%d\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n",i,x0,x1,y0,y1,x2,y2);

          if(fabs(y2)<=e)
          {
              printf("-------------------------------------------------------\n");
              printf("Converges to a root at %d iteration.\n",i);
              printf("Root is %.3f",x2);
              exit(0);
          }
          if(y0*y2>0)
          {
              x0=x2;
              y0=y2;
          }
          else{
            x1=x2;
            y1=y2;
          }
      }
      printf("-------------------------------------------------------\n");
      printf("Does not converges to root in %d iterations.\n",i);
      printf("Root is= %.3f\n",x2);

        return 0;
}
   float f(float x)
   {
       return(x*x*x-18);
   }
Out Put:
Enter the initial guess
2
3
Enter the allowable error:
0.001
Enter the number of iterations:
6
f(x)=x*x*x-18
-------------------------------------------------------
i       x0      x1      y0      y1      x2      y2
-------------------------------------------------------
0       2.000   3.000   -10.000 9.000   2.526   -1.876
1       2.526   3.000   -1.876  9.000   2.608   -0.261
2       2.608   3.000   -0.261  9.000   2.619   -0.035
3       2.619   3.000   -0.035  9.000   2.621   -0.005
4       2.621   3.000   -0.005  9.000   2.621   -0.001
-------------------------------------------------------
Converges to a root at 4 iteration.
Root is 2.621

C Program To Find The Root Of The Equation x*x-25 Using Bisection Method


/* C Program To Find The Root Of The Equation x*x-25 Using Bisection Method */


#include<stdio.h>
#include<math.h>
#include<conio.h>

float f(float);
int main()
{
    float x0,x1,x2,y0,y1,y2,e;
    int i;

      printf("Enter the initial guess\n");
          scanf("%f%f",&x0,&x1);
       y0=f(x0);
       y1=f(x1);

       if((y0*y1)>0)
       {
           printf("Invalid guess.\n");
           exit(0);
       }
      printf("Enter the allowable error:\n");
          scanf("%f",&e);


      printf("f(x)=x*x-25\n");
      printf("-------------------------------------------------------\n");
      printf("i\tx0\tx1\ty0\ty1\tx2\ty2\n");
      printf("-------------------------------------------------------\n");
          i=0;
        while((x1-x0)/x1>e)
        {
            printf("%d\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n",i,x0,x1,y0,y1,x2,y2);

            x2=(x0+x1)/2;
            y2=f(x2);
            if(y0*y0>0)
            {
                x0=x2;
                y0=y2;
            }
            else{
                x1=x2;
                y1=y2;
            }
            i++;

        }
        printf("-------------------------------------------------------\n");
        printf("The root is %.3f",x2);

        return 0;
}
   float f(float x)
   {
       return(x*x-25);
   }
Out Put:
Enter the initial guess
2
7
Enter the allowable error:
.01
f(x)=x*x-25
-------------------------------------------------------
i       x0      x1      y0      y1      x2      y2
-------------------------------------------------------
0       2.000   7.000   -21.000 24.000  0.000   0.000
1       4.500   7.000   -4.750  24.000  4.500   -4.750
2       5.750   7.000   8.063   24.000  5.750   8.063
3       6.375   7.000   15.641  24.000  6.375   15.641
4       6.688   7.000   19.723  24.000  6.688   19.723
5       6.844   7.000   21.837  24.000  6.844   21.837
6       6.922   7.000   22.912  24.000  6.922   22.912
-------------------------------------------------------
The root is 6.961

2/27/2009

C Program To Perform Array Of Structure



/*C Program To Perform Array Of Structure*/

#include <stdio.h>
#include <stdlib.h>

struct date
{
    int day;
    int month;
    int year;
};
int main()
{
    struct date d[10];
    int i,n;
    void call(struct date);
    void call1(int);
    void call2(struct date[] ,int);

    printf("How many date you want to print\n");
      scanf("%d",&n);
    printf("Enter the dates:\n");
      for(i=0;i<n;i++)
      scanf("%d%d%d",&d[i].day,&d[i].month,&d[i].year);

      call(d[0]);
      call1(d[0].year);
      call2(d,n);

    return 0;
}
void call2(struct date d[], int n)
{
    int i;
      printf("Displaying the dates\n");
        for(i=0;i<n;i++)
        {
            printf("%d/%d/%d\n",d[i].day,d[i].month,d[i].year);

        }
}
void call1(int year)
{
    printf("Year= %d\n",year);
}
void call(struct date x)
{
    printf("%d/%d/%d\n",x.day,x.month,x.year);
} 
Out Put:
How many date you want to print
1
Enter the dates:
23
1
2009
23/1/2009
Year= 2009
Displaying the dates
23/1/2009

C Program Array Of Structure



/*C Program Array Of Structure */

#include <stdio.h>
#include <stdlib.h>


struct date
{
    int day;
    int month;
    int year;
};
int main()
{
    struct date d[10];
    int i,n;
    printf("How many date you want to input\n");
      scanf("%d",&n);
    printf("Enter the dates:\n");
      for(i=0;i<n;i++)
      scanf("%d%d%d",&d[i].day,&d[i].month,&d[i].year);

      printf("Printing the dates\n");
        for(i=0;i<n;i++)
        {
            printf("%d/%d/%d\n",d[i].day,d[i].month,d[i].year);

        }
    return 0;
}
Out Put:
How many date you want to input
3
Enter the dates:
1 3 2009
3 1 2008
4 2 2007
Printing the dates
1/3/2009
3/1/2008
4/2/2007

C Structures



Structures:
One of the unique features of the C language is structures. They provide a method for packing together data of different types. A structure is a convenient tool for handling a group of logically related data items. It is a user defied data type with a template that serves to define its data properties.

Syntax:
        <tag_name>
        struct struct_name{
           ---------- //members of a structure
           ---------- //members of a structure       
           ---------- //members of a structure

         };

/*C Structures */

#include <stdio.h>
#include <stdlib.h>


int main()
{
    struct sample
{
    int x;
    float y;
    char z;
};
    struct sample v1,v2;

    v1.x=4;
    v1.y=4.65;
    v1.z='U';

    printf("%d\n%.2f\n%c\n",v1.x,v1.y,v1.z);
    printf("Enter an integer, float and a char\n");
      scanf("%d%f%s",&v2.x,&v2.y,&v2.z);
    printf("%d\n%.2f\n%c\n",v2.x,v2.y,v2.z);

    return 0;
}
Out Put:
4
4.65
U
Enter an integer, float and a char
3
1.6
l
3
1.60
l

2/25/2009

Different Methods Of Calling Functions In C Programming Language

Methods of calling funcctions in C:
===================================
 1) Call by value:
 =================
    In call by value, only the value will be passed.If any modification is done for the formal parameters within a function it does not reflect on the actual parameters.

 2) Call by reference:
 =====================
    In pass by reference both, value and adddress of the variable will be passed. If any modification is done by a formal parameters within a function, it reflects on actual parameters.

 3) Call by value_Result
 =======================
 
 4) Call by result
 =================

 5) Call by constatnt value
 ==========================
    

/* 1. Call by value*/
#include <stdio.h>
 
/* function declaration */
void swap(int x, int y);
/* function definition to swap the values */
void swap(int x, int y)
{
   int temp;

   temp = x; /* save the value of x */
   x = y;    /* put y into x */
   y = temp; /* put temp into y */
  
   return;
}
 
int main ()
{
   /* local variable definition */
   int a = 100;
   int b = 200;
 
   printf("Before swap, value of a : %d\n", a );
   printf("Before swap, value of b : %d\n", b );
 
   /* calling a function to swap the values */
   swap(a, b);
 
   printf("After swap, value of a : %d\n", a );
   printf("After swap, value of b : %d\n", b );
   printf("Did not swap. ");
 
   return 0;
}

/* 2. Call by reference*/
#include <stdio.h>
#include <conio.h>


/* function definition to swap the values */
void swap(int *a, int *b)
{
   int temp;

   temp = *a; /* save the value of x */
   *a = *b;    /* put y into x */
   *b = temp; /* put temp into y */

   return;
}

int main ()
{
   /* local variable definition */
   int a = 100;
   int b = 200;

   printf("Before swap, value of a : %d\n", a );
   printf("Before swap, value of b : %d\n", b );

   /* calling a function to swap the values */
   swap(&a, &b);

   printf("After swap, value of a : %d\n", a );
   printf("After swap, value of b : %d\n", b );

   return 0;
}

/* 3. Call by value_result*/

#include <stdio.h>
#include <conio.h>

/* function declaration */
int area(int l, int w);
 
/* function definition to calculate the area */
int area(int l, int w)
{
   return(l*w);
}
 

int main ()
{
   /* local variable definition */
   int l = 12;
   int w = 5;
 
   printf("area : %d\n", area(l,w) );
   
   return 0;
}

/* 4. Call by result*/

#include <stdio.h>
#include <conio.h>

int a=3,b=5;
/* function definition to calculate the sum */
int sum()
{
   return(a+b);
}
 

int main ()
{
   /* local variable definition */
   int result;
   
   result=sum();

   printf("Sum : %d\n", result );
  
   return 0;
}

/* 5. Call by constatnt value*/

#include <stdio.h>
#include <conio.h>

/* function definition to calculate the area */
float area(const float pi, float r)
{
   return(pi*r*r);
}
 

int main ()
{
   /* local variable definition */
   const float pi=3.14;
   float r=2.5;
 
   printf("Area : %.2f\n", area(pi,r) );
  
   return 0;
}