Saturday, 8 December 2012

Merge Sort using Recursion in C


#include<stdio.h>
int merge(int *,int,int,int);
int merge_sort(int *,int,int);
int main()
{
int a[1000],n,i,p,r;
clrscr();
printf("Enter the length of array:");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
merge_sort(a,0,n-1);
for(i=0;i<n;i++)
printf(" %d ",a[i]);
getch();
return 0;
}
int merge_sort(int *a,int p,int r)
{
int q;
if(p<r)
{
q=(p+r)/2;
merge_sort(a,p,q);
merge_sort(a,q+1,r);
merge(a,p,q,r);
}
return 0;
}
int merge(int *a,int p,int q,int r)
{
int n1,n2,i,j,k,L[500],R[500];
n1=q-p+1;
n2=r-q;
for(i=0;i<n1;i++)
L[i]=a[p+i];
for(j=0;j<n2;j++)
R[j]=a[q+j+1];
L[n1]=32765;
R[n2]=32765;
i=0;j=0;
for(k=p;k<=r;k++)
{
if(L[i]<=R[j])
{
a[k]=L[i];
i=i+1;
}
else
{
a[k]=R[j];
j=j+1;
}
}
return 0;
}

Quick sort using recursion in C


#include<stdio.h>
quicksort(int *,int,int);
partition(int *,int,int);
int main()
{
int a[100],p,r,i=0,n;
clrscr();
printf("Enter the length of array: ");
scanf("%d",&n);
printf("Enter the elements of array: \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
printf("The sorted sequence is: \n");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
getch();
return 0;
}

quicksort(int *a,int p,int r)
{
int q;
if(p<r)
{
q=partition(a,p,r);
quicksort(a,p,q-1);
quicksort(a,q+1,r);
}
return 0;
}


int partition(int *a,int p,int r)
{
int x,i,j,temp;
x=a[r];
i=p-1;
for(j=p;j<r;j++)
{
if(a[j]<=x)
{
i=i+1;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[i+1];
a[i+1]=a[r];
a[r]=temp;
return i+1;
}

Wednesday, 7 November 2012

History behind C language






How C language evolved?

Multics project started in 1964 cooperated with MIT ,  General Electric and Bell Labs but it was not very successful one. Multics had numerous features intended to result in high availability so that it would support a Computing utility similar to Telephone and Electricity services. It was not failing to deliver the promised fast and convenient on-line system,it was failing to deliver anything usable at all. They were trying to create an operating system that was much too big and to do it on hardware that was much too small. 
Bell Labs disenchanted in 1969 Multics and looked around for other tasks. When Bell Labs withdrew from the Multics research consortium,One researcher,Ken Thompson, was left with some Multics-inspired ideas about to build a file system. He was also left without a machine on which to play a game he had written called "Space Travel", a science-fiction simulation that involved navigating a rocket through the solar system. Ken Thompson was keen to work on another operating system and made several proposal to Bell Management but all declined. He didn't wait for official approval, He and his co-worker Dennis M. Ritchie amused themselves porting Thompson's "Space Travel" software to a little used PDP-7. Thompson worked intensively on providing the PDP-7 with the rudiments of a new operating system,much simpler and lighter than Multics. Everything was written in assembler language. Brian Kernighan coined the name "UNIX".
After a brief and unsuccessful filtration with Fortran,Thompson created a language B by simplifying the research language BCPL(Basic Combined Programming Language) so its interpreter would fit in the PDP-7's 8K word memory. B was never really successful; the hardware limits only provided room for an interpreter, not a compiler. The resulting slow performance prevented B from being used for system programming of UNIX itself. B simplified BCPL by omitting some features. Thompson conceived the ++ and -- operators(unary operator) and added them to the B compiler  on PDP-7. Typeless language proved to be unworkable when development switched in 1970 the newly introduced PDP-11. While at that time they were facing the problem for multiple datatypes and performance led Thompson to re-implement the OS in PDP-11 assembler rather than B. Dennis Ritchie capitalized on the more powerful PDP-11 to create "New B" which solved the both problems, multiple datatype and performance. "New B" in 1971- the name quickly evolved to "C" in 1972- was compiled rather than interpreted. 

Program to find strong number.

Strong Number: A strong number is a number in which the sum of factorial of all the digits of the given number is equal to the given number. E.g. 145=1!+4!+5!

#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,n;
long double f=1;
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
for(i=n;i>0;i=i/10)
{
r=i%10;
for(i=r;i>0;i--)
f=f*i;
s=s+f;
}
if(s==n)
printf("The number is strong.");
else
printf("The number is not strong number.");
getch();
}

Saturday, 3 November 2012

Factorial using recursion



#include<stdio.h>
long int fact(long int);
int main()
{
long int n,k=1;
clrscr();
printf("Enter the number:");
scanf("%ld",&n);
k=fact(n);
printf("Factorial is: %ld",k);

getch();

return 0;
}
long int fact(long int n)
{
long int f=1;
if(n==0)
return 1;
else
f=n*fact(n-1);
return f;
}

Output: 

Enter the number: 10
Factorial is: 3628800

Program for Binary search in array.


#include<stdio.h>
int main()
{
int a[100],i,n,s,k;
clrscr();
printf("\nEnter the length of array:");
scanf("%d",&n);
printf("\n Enter distinct elements of array: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the searching element: ");
scanf("%d",&s);
k=binary(a,s,n);
if(k!=-1)
printf("\n\nThe searching element is at %d",k+1);
else
printf("\n\nThe element is not found in the array");
return 0;
}
int binary(int a[],int p,int q)
{
int hi,low=0,mid;
hi=q-1;
while(low<=hi)
{
mid=(low+hi)/2;
if(p==a[mid])
return(mid);
if(p<a[mid])
hi=mid-1;
else
low=mid+1;
}
return(-1);
}

Output:
Enter the length of array: 20

Enter distinct element of array: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Enter the searching element: 20
The searching element is at 20

Total number of Trailing zeros in the factorial of an integer


#include<stdio.h>
int main()

{

long int i,n;
int zero,j;
clrscr();
        printf("Enter the number:");
scanf("%ld",&n);
     zero=0;
for(j=5;j<n;j*=5)
{
zero+=n/j;
}
printf("\n%d",zero);

getch();

return 0;
}


Output:
Eneter the number : 25

5

Friday, 2 November 2012

Transpose of a square matrix


#include<stdio.h>
#define max 100
void main()
{
int a[max][max],i,j,n;
clrscr();
printf("Enter the size of matrix:");
scanf("%d",&n)
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n\nOriginal matrix: \n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(" %d ",a[i][j]);
}
printf("\n");
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i<j)
{

a[i][j]=a[i][j]+a[j][i];

a[j][i]=a[i][j]-a[j][i];
a[i][j]=a[i][j]-a[j][i];
}

}

}
printf("\nTransposed matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(" %d ",a[i][j]);
}
printf("\n");
}
getch();
}

Thursday, 19 April 2012

Program to check whether string is palindrome or not


#include<stdio.h>
#include<string.h>
void main()
{
char *str,*p;
clrscr();
printf("Enter the string: ");
gets(str);
strcpy(p,str);
if(strcmp(p,strrev(str))==0)
printf("Entered String is palindrome.");
else
printf("Entered string is not palindrome");
getch();
}

Sunday, 1 April 2012

Insertion Sort


#include<stdio.h>
#include<conio.h>
#define max 20
void main()
{
int a[max],i,j,k,n;
clrscr();
printf("Enter the number of elements:  ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element %d :", i+1);
scanf("%d",&a[i]);
}
printf("Unsorted Array is : \n");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
printf("\n\n");




/* Insertion Sort  */
for(j=1;j<n;j++)
{
k=a[j];
for(i=j-1;i>=0 && k<a[i];i--)
a[i+1]=a[i];
a[i+1]=k;
printf("Pass %d, Element inserted in proper place:  %d\n",j,k);
for(i=0;i<n;i++)
printf(" %d ",a[i]);
printf("\n");
}
printf("Sorted list is : ");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
printf("\n");
getch();
}

Selection Sort


#include<stdio.h>
#include<conio.h>
#define max 20
void main()
{
 int n[max],i,j,k,m,temp,smallest;
 clrscr();
 printf("Enter the number of elements in the array:  ");
 scanf("%d",&m);
 for(i=0;i<m;i++)
 {
printf("Enter the element %d: ",i+1);
scanf("%d",&n[i]);
 }
 printf("Unsorted array is : \n");
 for(i=0;i<m;i++)
 printf(" %d ",n[i]);


 /* Selection sort  */
 printf("\n\n");
 for(i=0;i<m-1;i++)
 {
smallest=i;
for(k=i+1;k<m;k++)
{
if(n[smallest]>n[k])
smallest=k;
}
if(i!=smallest)
{
temp=n[i];
n[i]=n[smallest];
n[smallest]=temp;
}
printf("After pass %d elements are :  ",i+1);
for(j=0;j<m;j++)
printf(" %d ",n[j]);
printf("\n\n");
 }
printf("Sorted array is:  ");
for(i=0;i<m;i++)
printf(" %d ",n[i]);
printf("\n\n");
 getch();
}

Sunday, 26 February 2012

Pascal triangle program in C language


#include<stdio.h>
#include<conio.h>
void main()
{
int p[10][10],i,j,k;
clrscr();
printf("\n Pascal Triangle: ");
for(i=0;i<10;i++)
{
j=1;
p[i][0]=1;
p[i][i]=1;
while(j<i)
{
p[i][j]=p[i-1][j-1]+p[i-1][j];
j++;
}
}
for(i=0;i<10;i++)
{
j=10;
while(j>i)
{
printf(" ");
j--;
}
for(k=0;k<=i;k++)
{
printf("  %d ",p[i][k]);
}
printf("\n");
}
getch();
}



Saturday, 25 February 2012

No Argument No Returning type function program


#include<stdio.h>
#include<conio.h>
void fact();
void main()
{
clrscr();
fact();
getch();
}


void fact()
{
int i=0,f=1,n;
printf("Enter the number: ");
scanf("%d",&n);
for(i=n;i>0;i--)
f=f*i;
printf("\nThe factorial of the number is: %d",f);
}

Thursday, 16 February 2012

Program to change the string from upper case to lower case


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char n[100];
int i=0,p=0;
clrscr();
printf("Enter the string  ");
gets(n);
while(n[i]!='\0')
{
i++;
}
printf("Length of string is:  %d",i);


p=i;
while(p>=0)
{
if(n[p]>=65 && n[p]<=90)
n[p]=n[p]+32;
p--;
}
printf("The lower case string is: ");
puts(n);
getch();
}

Program to check whether two strings are same or not


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i=0,k=0;
char n[100],m[100];
clrscr();
printf("Input the string:");
gets(n);
gets(m);
while(n[i]!='\0')
{
i++;
}
k=i;
i=0;
while(n[i]!='\0')
{
if(n[i]==m[i])
{
i++;
if(k==i)
printf("The two strings are same.");
continue;
}
else
{
printf("String are not same.");
goto p;
}
}
p:
getch();
}

Sum of two integers using bit wise operators


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum,carry;
clrscr();
printf("Enter the two number: ");
scanf("%d  %d",&a,&b);
sum=a^b;
carry=a&b;
while(carry!=0)
{
carry<<=1;
a=sum;
b=carry;
sum=a^b;
carry=a&b;
}
printf("\n Sum=  %d",sum);
getch();
}

Addition of two integers using bitwise operators


/* Bit wise operator use less space in the memory */




#include<stdio.h>
#include<conio.h>
int add(int,int);
void main()
{
int m,n,sum;
clrscr();
printf("Enter the values of a and b:");
scanf("%d %d",&m,&n);
sum=add(m,n);
printf("The sum of the numbers: %d",sum);
getch();
}
int add(int m,int n)
{
int a,k;
do
{
a=m^n;
k=m&n;
m=a;
n=k<<1;
}while(k!=0);
return(a);
}