implement c program for A bubblesort routine


//program for bubblesort routine

# include <stdio.h>
# include <stdlib.h>
void bubblesort(int array[],int size);

void main()
{
int values[10],j;
for(j=0;j<10;j++)
values[j] = rand()%100;
/*unsorted*/
printf("\nUnsorted values.\n");
for(j=0;j<10;j++)
printf("%d ",values[j]);
/*sorted*/
printf("\nSorted values.\n");
bubblesort(values,10);
for(j=0;j<10;j++)
printf("%d ",values[j]);

}

void bubblesort(int array[],int size)
{
int tmp ,i,j;
for(i = 0;i <size;i++)
for(j=0;j < size;j++)
if(array[i] < array[j])
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}

0 comments :

implement c program to check palindrome string


//program to check whether a string is a palindrome

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(void)
{
char a[50],b[50];
clrscr();
printf("Enter the string:");
gets(a);
strcpy(b,a);
strrev(a);
if(strcmp(b,a) == 0)
{
printf("The given string is a palindrome!!!");
}
else
{
printf("The given string is not a palindrome!!!");
}
getch();
}

0 comments :

implement c program to count the no words in file


//Program to count the number of words in an input text file

#define NULL 0
FILE *fpt;
void main()
{
 char name[20],c;
 int nw=0;
 clrscr();
 printf("Enter the name of file to be checked:-  ");
 gets(name);
 fpt=fopen(name,"r");
 if (fpt==NULL)
 {
  printf("ERROR - can/'t open file %s",name);
  getch();
  exit(0);
 }
 else
 {
 while ((c=getc(fpt))!=EOF)
 {
  switch(1)
  {
   case 1:
 if (c==' ')
 {
   point: // do
 //    nw=nw+1-1;
  while((c=getc(fpt))==' ');

  if (c!=' ')
nw=nw+1;
  if(c=='
')nw--;
 }


 //  case 3:
 if(c=='
'){
   goto point;}

  }  } }
 printf("The no. of words in %s is %d.  ",name,nw);
 getch();
}

0 comments :

implement c program to Count Blanks,Tabs and Newlines


//Program to Count Blanks,Tabs and Newlines

#include<stdio.h>
int main(void)
{
        int nb,nt,nl,c;
        nb=nt=nl=0;

while((c=getchar())!=EOF)
{
if(c==' ')
++nb;
if(c==' ')
++nt;
if(c=='
')
++nl;
}
printf("No. of Blanks is %d,No. of Tabs is %d and No. of Newlines is %d",nb,nt,nl);
return 0;
}

0 comments :

implement c program for Merge sort


//implement program for Merge sort

#include<stdio.h>

void getdata(int arr[],int n)
{    int i;
      printf("enter the data:");
  for(i=0;i<n;i++)
    {
     scanf("%d",&arr[i]);
    }
}

void display(int arr[],int n)
{
 int i;
 printf(" ");
 for(i=0;i<n;i++)
    {
     printf("%d ",arr[i]);
    }
 getchar();
}

void sort(int arr[],int low,int mid,int high)
{
 int i,j,k,l,b[20];
 l=low;
 i=low;
 j=mid+1;
 while((l<=mid)&&(j<=high))
   {
    if(arr[l]<=arr[j])
      {
       b[i]=arr[l];
       l++;
      }
    else
      {
       b[i]=arr[j];
       j++;
      }
    i++;
   }
 if(l>mid)
   {
    for(k=j;k<=high;k++)
       {
        b[i]=arr[k];
        i++;
       }
   }
 else
   {
    for(k=l;k<=mid;k++)
       {
        b[i]=arr[k];
        i++;
       }
   }
 for(k=low;k<=high;k++)
    {
     arr[k]=b[k];
    }
}

void partition(int arr[],int low,int high)
{
 int mid;
 if(low<high)
   {
    mid=(low+high)/2;
    partition(arr,low,mid);
    partition(arr,mid+1,high);
    sort(arr,low,mid,high);
   }
}
void main()
{
 int arr[20];
 int n;
 printf("Enter number of data:");
 scanf("%d",&n);
 getdata(arr,n);
 partition(arr,0,n-1);
 display(arr,n);
 getchar();
}

0 comments :

implement c program for Fibonacci series


//program to prints the Fibonacci series

#include<stdio.h>
#include<conio.h>
void main(void)
{
int i,j,k,n;
clrscr();
i=0;
j=1;
        printf("Fibonacci series is :--");
printf("%d %d ",i,j);
for(n=0;n<=5;n++)
{
k=i+j;
i=j;
j=k;
printf("%d ",k);
}
getch();
}

0 comments :