c program bubble sort

>> Friday, November 18, 2011


c program to implement bubble sort. Sorting refers to the operation of rearranging the element. Bubble sort algorithm requires n-1 passes, where n is the number of input items. Here is the c program for bubble sort. The bubble sort gets its name because elements tend to move up into the correct order like bubbles rising to the surface.


#include<stdio.h> #include<conio.h> int main( ) { int a[100]; int i, j, temp, n ; printf("how many numbers you want to sort : \n"); scanf("%d",&n); printf("Enter %d number values you want to sort\n", n); for(j=0; j<n; j++) scanf("%d",&a[j]); for(j=1;j<n;j++) {             for(i=0; i<n; i++)             {                       if(a[i]>a[i+1])                       {                                 temp=a[i];                                 a[i]=a[i+1];                                 a[i+1]=temp;                       }             }      }      printf ( "\n\nArray after sorting:\n") ;      for ( i = 0 ; i <n ; i++ )      printf ( "%d\t", a[i] ) ;      getch(); }
output:

c program bubble sort output
c program to implement bubble sort



Use of Bubble sort :Bubble sort is simple and so it is often used to introduce the concept of sorting algorithm, to introductory computer science students.

Read more...

c program to implement dfa


C program of dfa implementation. Let us first know what is DFA or let us again revise the concept of DFA? Automation are basically language acceptor or language
recognizer. A finite automata is a collection of 5-tuple(Q,∑,∂,q0,F). Where
Q=finite set of states
∑=input symbol
∂=transition function
q0=initial state
F=set of final state


Here is the c program to implement dfa

#include<stdio.h> #include<conio.h> int ninputs; int check(char,int ); //function declaration int dfa[10][10]; char c[10], string[10]; int main() {     int nstates, nfinals;     int f[10];     int i,j,s=0,final=0;     printf("enter the number of states that your dfa consist of \n");     scanf("%d",&nstates);     printf("enter the number of input symbol that dfa have \n");     scanf("%d",&ninputs);     printf("\nenter input symbols\t");     for(i=0; i<ninputs; i++)     {     printf("\n\n %d input\t", i+1);     printf("%c",c[i]=getch());     }     printf("\n\nenter number of final states\t");     scanf("%d",&nfinals);       for(i=0;i<nfinals;i++)     {       printf("\n\nFinal state %d : q",i+1);       scanf("%d",&f[i]);     }              printf("-----------------------------------------------------------------------");      printf("\n\ndefine transition rule as (initial state, input symbol ) = final state\n");      for(i=0; i<ninputs; i++)      {               for(j=0; j<nstates; j++)               {                        printf("\n(q%d , %c ) = q",j,c[i]);                        scanf("%d",&dfa[i][j]);               }      }          do      {      i=0;      printf("\n\nEnter Input String.. ");      scanf("%s",string);     while(string[i]!='\0')       if((s=check(string[i++],s))<0)       break;       for(i=0 ;i<nfinals ;i++)       if(f[i] ==s )       final=1;            if(final==1)       printf("\n valid string");       else       printf("invalid string");       getch();     printf("\nDo you want to continue.?  \n(y/n) ");      }      while(getch()=='y');           getch(); } int check(char b,int d) {     int j;     for(j=0; j<ninputs; j++)     if(b==c[j])     return(dfa[d][j]);     return -1;   }
       
 output :

c program to implement dfa
c program to implement dfa





 The above output is for the dfa to check whether the given binary number is even.
Its DFA is made as follows :

c program to implement dfa
DFA: To check whether the given number is even
and its table is made as :



States-Input
0
1
q0
q2
q1
q1
q2
q1
q2
q2
q1



Uses of DFA:  The very good example of finite state system is a control mechanism of elevator. It is very good tool for the programs such as text editors and lexical analyzer.
Thanks.

Read more...

C program to find factorial of a number using recursion

>> Wednesday, November 2, 2011


Factorial is mostly used in calculting permutation and combination. Factorial sign is '!' i.e exclamatory mark.
For eg 4! can be calculated as : 4!=4*3*2*1=24. Here the c program to calculate factorial using recursion function.



#include<stdio.h> #include<conio.h> int recfact(int); main( ) { int a, fact, rec ; printf ( "\nEnter any number " ) ; scanf ( "%d", &a ) ; rec = recfact ( a ) ; printf ( "Factorial value = %d", fact ) ; getch(); } int recfact( int x ) { int f ; if ( x == 1 ) return ( 1 ) ; else f = x * recfact ( x - 1 ) ; return ( f ) ; }


Output :

C program to find factorial using recursion
c program to find factorial using recursion

Read more...

C program to find factorial of a number



C program to calculate factorial of a number : For an integer n greater than or equal to 1, the factorial is the product of all integers less than or equal to n but greater than or equal to 1. The factorial value of 0 is defined as equal to 1. The factorial values for negative integers are not defined. For eg : factorial of 5 i.e 5!=5*4*3*2*1=120. #include<stdio.h> #include<conio.h> int fact(int ); int main( ) { int a, f ; printf ( "\nEnter any number " ) ; scanf ( "%d", &a ) ; f = fact ( a ) ; printf ( "Factorial value = %d", f ) ; getch(); } int fact ( int x ) { int f = 1, i ; for ( i = x ; i >= 1 ; i-- ) f = f * i ; return ( f ) ; }


Output :

C program to find factorial. 


Read more...

c program return statement returns more than one value

This program makes a contrasting concept that how a return statement
can return more than one value. Usually in C programming we make a call by value.
This means that in general you cannot alter the actual arguments. But if
desired, it can always be achieved through a call by reference.
Using a call by reference intelligently we can make a function
return more than one value at a time, which is not possible
ordinarily. This is shown in the program given below.


#include<stdio.h> #include<conio.h> int areaperi ( int r, float *a, float *p ) { *a = 3.14 * r * r ; *p = 2 * 3.14 * r ; return (0); } int main( ) { int radius ; float area, perimeter ; printf ( "\nEnter radius of a circle " ) ; scanf ( "%d", &radius ) ; areaperi ( radius, &area, &perimeter ) ; printf ( "Area = %f", area ) ; printf ( "\nPerimeter = %f", perimeter ) ; getch(); }

Output :

C program to return more than one value
C porgram to return more than one value.

Read more...

c program to implement Heap sort Algorithm

>> Tuesday, November 1, 2011



Before going through this program. Read this for better understanding of program.“Consider the complete tree H. Observe that H is a heap. H is represented in array TREE. That is, TREE[1] is the root of the tree H, and the left and right children of node TREE[k] are, respectively, TREE[2k] and TREE[2k+1]. This means that in particular, that the parent of any non root node TREE[j] is the node TREE[j/2](where j/2 means integer division). Observe that the nodes of H on the same level appear one after the other in the array TREE”.

Write a program to apply heap sort algorithm ?


#include<stdio.h> #include<conio.h> int main() {       int TREE[10],N,i,j,K,p,c,temp;       printf("\n\n Enter no of elements..");       scanf("%d",&N);       printf("\n\n Enter the nos..");       for(i=1;i<=N;i++)       scanf("%d",&TREE[i]);       for(i=2;i<=N;i++)       {   K=i;   do   {        if(TREE[K]>TREE[K/2])                        // Values are inserted in the heap                 {                       temp=TREE[K];      TREE[K]=TREE[K/2];      TREE[K/2]=temp;        }             p=K/2;             K=p;          }       while(K!=0);       }       printf("\n\n\n On inserting values are arranged as \n");       for(i=1;i<=N;i++)                                // Displaying values in heap       printf("%d\t",TREE[i]);       for(j=N;j>0;j--)       {    temp=TREE[1];    TREE[1]=TREE[j];    TREE[j]=temp;    p=0;    do             {                                            // Heap sorting is applied  c=2*p+2;  if((TREE[c]<TREE[c+1]) && c<j-1)                   c++;  if(TREE[p]<TREE[c] && c<j)                   {        temp=TREE[p];        TREE[p]=TREE[c];              TREE[c]=temp;  }  p=c;    }while(c<(j+1));       }       printf("\n\n\n The sorted nos are..");       for(i=1;i<=N;i++)                         // printing the heap in sorted order       printf("%d\t",TREE[i]);       getch(); }
Output :

C program to implement heap sort, heap sorting
Output : C program for Heap sort 

Read more...

c program to print n numbers without using loop


This program will print the n numbers without using any loop. Here we uses the concept of recursion. Lets See the program
to make clear view that how recursion works.


#include <stdio.h> #include <conio.h> void print(int p) {   if(p>0)   {     print(p-1);                    // recursive call;     printf("\n\n %d \t", p);   }   return; } int main() {   int n;   printf("enter the number\n\n");   scanf("%d",&n);                  // number to which you wnat to print   print(n);   getch();   return 0; }

Output:

C program to print n numbers, withou loop



Read more...

About This Blog

This blog will contain c programs related to interview preparation, basic programs, operating system, graphics, data structure, algorithms implementation, compiler and porjects. C is one of the most widely used programming languages of all time. This blog is intended for engineer students and for the people who are preparing for their interview in IT sector.

Share and Save