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...

c program to swap two numbers using call by value


This program demonstrates how the two number are swapped. This swapping of variables are done in function 'swap' which we have defined. It also clears the concept, that how "call by value" is implemented. Lets see how this is implemented :

#include<stdio.h> #include<conio.h> int swap(int , int);                       // Declaration of function main( ) { int a = 10, b = 20 ;                               // call by value swap(a,b);                                            // a and b are actual parameters printf ( "\na = %d b = %d", a, b ) ; getch(); } int swap( int x, int y )                            // x and y are formal parameters { int t ; t = x ; x = y ; y = t ; printf ( "\nx = %d y = %d", x, y ) ; }
Output :


C program to swap numbers, call by value




Note that values of a and b remain unchanged even after
exchanging the values of x and y.

Lets have a look on second method of swapping the number using call by reference :
here is the link provided for :
Call by refrence

Read more...

c program to swap two numbers using call by reference


This is another program to swap two number using call by reference method. In call by reference the addresses of actual
arguments in the calling function are copied into formal arguments of the called function. This means that using these addresses we
would have an access to the actual arguments and hence we would be able to manipulate them.


#include<stdio.h> #include<conio.h> void swap( int *x, int *y )                     // call by refrence { int t ; t = *x ; *x = *y ; *y = t ; printf( "\nx = %d y = %d", *x,*y); } int main( ) { int a = 10, b = 20 ;           swap ( &a, &b ) ;                              // passing the address of values to be swapped printf ( "\na = %d b = %d", a, b ) ; getch(); }

output :

 C program to swap two numbers using call by reference output




Note that this program manages to exchange the values of a and b
using their addresses stored in x and y.
Lets have a look on how call by value is done. Here is the program to swap two numbers using call by value :
Call by value



Read more...

c program to swap two numbers without using third variable

This is another important program to swap two numbers without using third variable. The bold letters indicated in this program
describe the concept of how swapping is made withou using any other variable. Below is the program which is implementing swaping of two numbers.


#include<stdio.h> #include<conio.h> int main() { int a,b,temp; printf("enter two values\n"); printf("\na = "); scanf("%d",&a); printf("\tb = "); scanf("%d",&b); a=a+b; // swapping without using third variable b=a-b; a=a-b; printf("\na = %d\tb = %d ", a, b); getch(); }

Output :

C program to swap without using third variable

Read more...

c program to swap two numbers using third variable

>> Monday, October 31, 2011

Hello! This program is very important as it is mostly taught and asked in external, vivas and some time in
campus recruitment exams also. We are Swapping the value of variable 'a' and 'b' by using a third variable 'temp'. Lets look on this program that how it is done.



#include<stdio.h> #include<conio.h>
int main() {     int a,b,temp;    printf("enter two values\n");    printf("\na = ");    scanf("%d",&a);    printf("\nb = ");    scanf("%d",&b);    temp=a;    a=b;    b=temp;    printf("\na = %d\tb = %d ", a, b);    getch(); }

Output :



 C program to swap two numbers using third variable
C program to swap two number using third varaible 

Read more...

c program to find whether the given number is prime or not using Recursion ?

>> Friday, October 28, 2011


This C program uses recursion for finding whether the entered number is prime or not. A function is called recursive if the statement in a body calls the same functions. It uses the modulus operator ('%') to find the prime number.Lets have a look on this program. Function name is prime in this program and it calls prime() again till the condition is satisfied and till the parser is not return to the main function.




#include<stdio.h> #include<conio.h> int prime(int n) {     static int i=2;                                              if(n%i==0&&n!=2)            {            return(1);            }            if(i<n)            {            i++;            prime(n);                                    // recursion call            } } int main() {     int p;     printf("enter the number to check for prime number : ");     scanf("%d",&p);     int l=prime(p);                                      //function returns the value and puts in 'l' variable         if(l==1)                                            //conditions         printf("%d is not a prime number ", p);         else         printf("%d is a prime number ", p);     getch(); }

Here is output for the above program :

C program to find whether the entered number is prime or not using recursion
output : Find prime number using recursion


Read more...

c program to find and count the prime number upto given number ?


This program find the Prime number upto the number you desire and will also count the number of prime numbers. This program uses goto Statement which is very infrequently used but sometime desired at some places and so as here. So in this program will we also learn to use goto statement. Below is the program with its output, with prime number upto 100.
Have a look on it :


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

int main()
{
     int n=2, count=0;                  //count variable store the number of prime number
     int i, m;
     printf("enter the number upto which you want to find prime numbers : ");      
     scanf("%d",&m);
     printf("prime numbers are : ");
     while(n<m)
     {
             for(i=2; i<n; i++)
                      {
                      if(n%i==0)
                      goto t;                             // goto is referring to line or code 't'
                      }
             printf("\t%d ", n);
             count++;
             t : n++;                                    //when goto is executed it will directly jump to this statement
     }
     printf("\n\nNumber of prime numbers are %d",count);
     getch();
}
       
   

Here is the output :

c program to find the prime number upto a given number and count total number of prime numbers


Read more...

c program to find prime number efficiently in less time ?


This C program will find the given number is prime with less time, by testing whether the given number is a multiple of any integer between 2 and square root of that number. Lets take an example to understand this. Suppose the number is 21, then rather to check its divisibility from 2 to 20, just test it divisibility only by 2, 3, and 4.As square of 4 is 16 and that of 5 is 25 which is greater than 21. The below program will let you know how this is done.


#include<stdio.h> #include<conio.h> int main() {      int n;      int i,s=1,p=1;      printf("enter the number :");      scanf("%d",&n);      while(p<n)      {      s++;      p=s*s;      }      printf("\nDivisibility is checked till number %d \n\n",s-1);      for(i=2; i<s; i++)      {               if(n%i==0)                {                 printf("%d is not a prime number", n);                getch();                return (1);                }       } printf("%d is a prime number", n); getch(); }            

Here is the output :

C program to find prime number effeciently in less time and fast
 find prime number efficiently in less time


Read more...

c program to find whether the given number is prime or not ?


In this C program, we will input the number when program runs and find whether that entered number is prime or not. This program uses "%" or "modulus" operator which gives the remainder of two numbers when it is divided. For example : 7 % 2 = 1, as 1 is remainder when 7 is divided by 2. So if the number is not prime then its % operation will give output '0' otherwise it is prime. Let us see how this program is implemented.
#include<stdio.h>                     // Including library
#include<conio.h>
int main()                              
{
     int n;
     int i;
     printf(" enter the number : ");   
     scanf("%d",&n);                  
     for(i=2; i<n; i++)
     {
              if(n%i==0)              
               {
                printf("%d is not a prime number.", n);
               getch();                                                  // hold  the  screen to display the above message
               return (1);                                              // return statement  will return the value and terimate
               }
     }
printf("%d is a prime number.", n);
getch();
}
         
Lets see the output of this program
c program to find prime number, output for prime number
output : Program to find prime number 



Here’s the another  program to find whether  the given number is prime or not by using recursion function.


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