Showing posts with label Data Structure Using C. Show all posts
Showing posts with label Data Structure Using C. Show all posts

C Program to Search a Given Number Using Binary Search



PROGRAM:

#include<stdio.h>
main()
{
 int A[20],n,i,j,temp;
 clrscr();
 printf("Enter the size of array:\n");
 scanf("%d",&n);
 printf("Enter the array elements:\n");
 for(i=0;i<n;i++)
 scanf("%d",&A[i]);
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
   {
          if(A[j]>A[j+1])
           {
            temp=A[j];
            A[j]=A[j+1];
            A[j+1]=temp;
           }
   }
 }
 printf("The sorted arrays are as follows:\n");
 for(i=0;i<n;i++)
 printf("%d\t",A[i]);
 printf("enter the key element to search")
 scanf("%d",&key);
   min=0;
   max=n-1;
   while(min<=max)
          {
           mid=(int)((min+max)/2);
           if(key==A[mid])
          {
           printf("key is found in location %d",mid+1);
             break;
            }
           else if(key>A[mid])
           min=mid+1;
           else
           max=mid-1;
          }
   if(min>max)
   printf("Key not found");
  getch();
}

OUTPUT:

Enter the size of array:
6
Enter the array elements:
22
11
66
99
88
77
The sorted arrays are as follows:
11      22      66      77      88      99
 ENTER THE KEY ELEMENTS TO SEARCH 88
key is found in location 5

Enter the size of array:
6
Enter the array elements:
22
55
66
33
69
99
The sorted arrays are as follows:
22      33      55      69      99      66
 ENTER THE KEY ELEMENTS TO SEARCH 10
Key not found


C Program to Search a Given Number Using Linear Search



PROGRAM:

#include<stdio.h>
main()
{
   int nums[40];
   int key,i,n,location=0;
   clrscr();
   printf("Enter the size of an array:\n");
   scanf("%d",&n);
   printf("Enter the array element:\n");
   for(i=0;i<n;i++)
   scanf("%d",&nums[i]);
   printf("Enter the key element:");
   scanf("%d",&key);
   for(i=0;i<n;i++)
   if(nums[i]==key)
     {
        location=i+1;
        break;
     }
   if(location==0)
     {
        printf("The given key element doesn't exist and is not found");
     }
   else
   printf("The given key element %d exists and is found %d position",key,location);
   getch();

}

OUTPUT:

Enter the size of an array:
6
Enter the array element:
22
55
99
87
66
01
Enter the key element: 99
The given key element 99 exists and is found 3 position.


Enter the size of an array:
6                                                  
Enter the array element:                                                                               
101                                                
45                                                 
23                                                  
30                                                 
69                                                 
72                                                 
Enter the key element: 10                           
The given key element doesn't exist and is not found


C program to sort an array using selection sort



Program:

#include<stdio.h>
main()
{
           int i,j,a[10],n,small,index,temp;
           clrscr();
           printf("\nEnter the number of array\n");
           scanf("%d",&n);
           printf("\nEnter the array elemnts\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
    {
     small=a[i];
     index=i;
      for(j=i+1;j<n;j++)
         {
            if(a[j]<small)
            {
             small=a[j];
             index=j;
             }
          }
       temp=a[index];
        a[index]=a[i];
       a[i]=temp;
      }
printf("sorted list is\n");
          for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}


Output:

Enter the size of array
5

Enter the array elemnts
6
9
-6
-3
-2
sorted list is

-6      -3      -2      6       9


C Program to Sort an Array using Bubble Sort



PROGRAM:

#include<stdio.h>
main()
{
 int A[20],n,i,j,temp;
 clrscr();
 printf("Enter the size of array:\n");
 scanf("%d",&n);
 printf("Enter the array elements:\n");
 for(i=0;i<n;i++)
 scanf("%d",&A[i]);
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
   {
          if(A[j]>A[j+1])
           {
            temp=A[j];
            A[j]=A[j+1];
            A[j+1]=temp;
           }
   }
 }
 printf("The sorted arrays are as follows:\n");
 for(i=0;i<n;i++)
 printf("%d\t",A[i]);
 getch();
}


OUTPUT:

Enter the size of array:
6
Enter the array elements:
11
45
69
72
36
45
The sorted arrays are as follows:
0       11      36      45      69      72


Enter the size of array:
6
Enter the array elements:
-8
9
69
36
89
10
The sorted arrays are as follows:
-8      0       9       10      36      69


C program to find the GCD of two numbers using recursion



PROGRAM:

#include<stdio.h>
main()
{
int a,b;
 clrscr();
 printf("Enter the two numbers\n");
 scanf("%d%d",&a,&b);
 printf("THE GCD OF %d AND %d IS %d\n",a,b,gcd(a,b));
 getch();
}
int gcd(int a,int b)
{
if(b!=0)
 {
  return (gcd(b,b%a));
 }
else
return(a);
}


OUTPUT:

Enter the two numbers
30
45
THE GCD OF 30 AND 45 IS 15

Enter the two numbers
66
99
THE GCD OF 66 AND 99 IS 33

C program to count the number of characters in a given file




















Program:

#include<stdio.h>
  main()
   {
           FILE *fp;
int count=0;
clrscr ();
fp=fopen("vpt.txt","r");
            while( getc(fp) != EOF)
      {
                   count++;
                 }
         printf("count=%d",count);
        fclose(fp);
         getch();
}

Output:
count=16

Txt File:

C program to illustrate the use of function pointer



Program:

#include<stdio.h>
void main()
{
int sum(int, int);
int (*p)(int,int);
int a,b,r;
clrscr();
printf("input the two integers:\n");
scanf("%d%d",&a,&b);
p=sum;
r=(*p)(a,b);
printf("the sum of %4d and %4d is :%5d",a,b,r);
getch();
}
int sum(int m, int n)
{
return(m+n);
}

Output:

input the two integers:
100
150

the sum of  100 and  150 is :  250

C program to find the smallest element in an array of 10 elements using pointers



Program:

include<stdio.h>
main()
{
 int i,A[10],small,index=1;
 int *p;
  p=A;
clrscr();
  printf("Enter the array elements\n");
  for(i=0;i<10;i++)
  scanf("%d",&A[i]);
  small=*p;
  for(i=1;i<10;i++)
  {
  p++;
  if(small>*p)
  {
  index=i+1;
  small=*p;
  }
  }
  printf("Smallest element in an array %d is found at %d position\n",small,index);
  getch();
  }


Output:

Enter the array elements
66
99
-9
-8
-6
-3
55
66
-99
89
Smallest element in an array -99 is found at 9 position


Enter the array elements
88
99
-8
-6
0
23
12
45
69
88
Smallest element in an array -8 is found at 3 position

C program to swap two numbers using Call by value and Call by reference parameter passing techniques



Call by value

Program :

#include<stdio.h>
void swp(int a,int b);
main()
{
int a,b,temp;
clrscr();
printf("enter the values of a and b\n");
scanf("%d%d",&a,&b);
printf("\nvalues before calling swap function\n a:%d, b:%d\n",a,b);
swp(a,b);
getch();
}
void swp( int num1,int num2)
{
int temp;
printf("\ninside the function swap before swaping\n");
printf("num1:%d, num2:%d\n",num1,num2);
temp=num1;
num1=num2;
num2=temp;
printf("\ninside the function swap after swaping\nnum1:%d, num2:%d\n",num1,num2);
printf("\nvalues after calling swap function\n a:%d, b:%d\n",num1,num2);
}

Output:

Enter the values of a and b
55
66
values before calling swap function
 a:55, b:66
inside the function swap before swaping
num1:55, num2:66
inside the function swap after swaping
num1:66, num2:55
values after calling swap function
 a:66, b:55


Enter the values of a and b
23
32
values before calling swap function
 a:23, b:32
inside the function swap before swaping
num1:23, num2:32
inside the function swap after swaping
num1:32, num2:23
values after calling swap function
 a:32, b:23


Call by reference:

Program: 

#include<stdio.h>
void swap (int *a,int *b);
int main()
{
int a,b;
clrscr();
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
printf("Values before calling swap function\n");
printf("a:%d\t b:%d\n",a,b);
swap (&a,&b);
printf("Values after calling swap\n");
printf("a:%d\t b:%d\n",a,b);
getch();
}
void swap(int *pa,int *pb)
{
int temp,a,b;
printf("Inside the function swap  before swapping\n");
printf("a:%d\t b:%d\n",*pa,*pb);
temp=*pa;
*pa=*pb;
*pb=temp;
printf("Inside the function swap  after swapping\n");
printf("a:%d\t b:%d\n",*pa,*pb);
}


Output:

Enter two numbers
55
66
Values before calling swap function
a:55     b:66
Inside the function swap before swapping
a:55     b:66
Inside the function swap after swapping
a:66     b:55
Values after calling swap 
a:66     b:55


Enter two numbers
22
33
Values before calling swap function
a:22     b:33
Inside the function swap before swapping
a:22     b:33
Inside the function swap after swapping
a:33     b:22
Values after calling swap
a:33     b:22

Subscribe By Email

Join Our Newsletter

Get All The Latest Updates Delivered Straight Into Your Inbox For Free!

We Respect Your Privacy

Popular Posts