Oct 14, 2011

C Program to calculate the Combinations and Permutations.

C Program to calculate the Combination and Permutations. Combination means way of selecting a things or particular item from the group or sets. nCr=n!/r!(n-r)!. Permutations means possible way of rearranging in the group or set in the particular order. Read more about C Programming Language .



/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

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

main()
{
 int n , r, ncr( int , int);
 long npr( int , int);
 long double fact( int);
 printf(" Enter value of n & r \n");
 scanf("%d %d",&n , &r);
 if( n>= r)
 {
  printf( " %dC%d is %d\n", n,r,ncr( n , r));
  printf(" %dP%d  is %ld", n,r,npr( n, r));
 }
 else
 {
  printf("WRONG INPUT?? enter the correct input");
 }
}
long double fact( int p)
{
 long double facts = 1;
 int i;
 for( i = 1; i<= p; i++)
  facts = facts * i;
 return( facts);
}

int ncr ( int n, int r)
{
 return( fact( n) / (fact( r) * fact(n- r) ) ) ;
}

long npr( int n , int r)
{
 return( fact( n) / fact( n- r));
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


C Program to check the given number is Armstrong or not?

C Program to check the given number is Armstrong number or not?. Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. Example: 153 = 1^3 + 5^3 + 3^3. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/


#include<stdio.h>
void main()
{
 int num,num1,arms=0,rem;

 printf("Enter the number:\n");
 scanf("%d",&num);
 num1=num;
 while(num>0)
 {
  rem=num%10;
  arms=arms+rem*rem*rem;
  num=num/10;
 }
 if(num1==arms)
 {
  printf(" \n%d is an Armstrong number",num1);
 }
 else
 {
  printf("\n%d is NOT an Armstrong number",num1);
 }

} 
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!



C Program to Demonstrate #if, #else & #endif preprocessors.

C Program to demonstrate the Preprocessor directives like #if, #else, #define, #endif. C Preprocessors are not the program statements, they are executed before the actual compilation of the code. C Preprocessors substitute the code where they called, i.e they replace the code as they defined. #if, #else are the conditional directives. Here at the compile time #if value is false, so #else part is execute.
Read more about C Programming Language .



/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>

#define SWITCH 0

/* The #if can only perform 
 * INTEGER tests!!!!
 */
#if  ( SWITCH == 1 )
#define TEXT "Example to #if"
#else
#define TEXT "Example to # else"
#endif

main ()
{
 printf(TEXT);
}
Read more Similar C Programs
Learn C Programming

Number System
Preprocessor
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


C program to find the character type.

C Program to find the Character types. Here we used the isdidit(), isalpa(), and isspace() functions. isdigit() function returns true if argument is a decimal digit, otherwise false. isalpa() function returns true if argument is a defined set of characters in c, otherwise false. isspace() function returns true if argument is a white space character (one of ' ' '\f' '\n' '\r' '\t' '\v'), otherwise false. Read more about C Programming Language .



/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/



#include <stdio.h>     
#include <ctype.h>   

#define FALSE 0
#define TRUE  1

    /* function declarations   */
int char_type(char);

main()
   {
   char ch;
   /* get a character from the keyboard  */
   printf(" Please enter a charcater => ");
   ch = getc(stdin);

   char_type(ch); /* Figure out the character type  */
   }


int char_type(char ch)
   {
   /* returns non zero if A-Z or a-z  */
   if ( isalpha(ch) != FALSE)
      printf("%c is an Alpha character.\n",ch);

   /* returns non zero if 0-9    */
   if ( isdigit(ch) != FALSE)
      printf("%c is a numeric character.\n",ch);

   /* returns non zero if a space, CR, Tab, NL FF  */
   if ( isspace(ch) != FALSE)
      printf("%c is white space\n", ch);

   }
Read more Similar C Programs
Searching in C

C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


C Program to demonstrate extern variable.

C Program to demonstrate extern variable. Extern variables are very useful in c,when the source code is in the two or more files, you can refer the variables from one file to another using extern variables. Extern variables are also global variable,but in explicit manner. Read more about C Programming Language .




/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
void write_extern(void);

extern int count;

void write_extern(void) 
{
 printf("count is %i\n", count);
}

int count = 5;

main() 
{
 write_extern();
}

Read more Similar C Programs
C Strings

Simple C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


C Program to demonstrate functions.

C Program to demonstrate functions. C Functions are block of codes, performs the specific task. Default function in C is main(). Advantages of the functions are easy to understand, coding, well defined tasks, easy error handling. There are several ways to call the functions like, pass by value, pass by reference etc.. Read more about C Programming Language .



/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/


#include<stdio.h>
int add( int, int);    /* Function declaration  */

main()
{
 int i=1;
 printf("i starts out life as %d.", i);

 i = add(1, 1);   /* Function call         */

 printf(" And becomes %d after function is executed.\n", i);
}

/************************************************************************/

int add( int a, int b)    /* Function definition   */
{
 int c;
 c = a + b;
 return c;
}
Read more Similar C Programs
C Strings

Simple C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


C Program to Find out the size of the different data types

C Program to find the Size of Different data types. In This program we, find the size of the datatypes in c using sizeof() operator. sizeof() operator returns the its argument memory in bytes. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/


 


#include <stdio.h>
main()
{

 printf("    short int is %2d bytes \n", sizeof(short int));
 printf("          int is %2d bytes \n", sizeof(int));
 printf("        int * is %2d bytes \n", sizeof(int *));
 printf("     long int is %2d bytes \n", sizeof(long int));
 printf("   long int * is %2d bytes \n", sizeof(long int *));
 printf("   signed int is %2d bytes \n", sizeof(signed int));
 printf(" unsigned int is %2d bytes \n", sizeof(unsigned int));
 printf("\n");
 printf("        float is %2d bytes \n", sizeof(float));
 printf("      float * is %2d bytes \n", sizeof(float *));
 printf("       double is %2d bytes \n", sizeof(double));
 printf("     double * is %2d bytes \n", sizeof(double *));
 printf("  long double is %2d bytes \n", sizeof(long double));
 printf("\n");
 printf("  signed char is %2d bytes \n", sizeof(signed char));
 printf("         char is %2d bytes \n", sizeof(char));
 printf("       char * is %2d bytes \n", sizeof(char *));
 printf("unsigned char is %2d bytes \n", sizeof(unsigned char));
}
Read more  C Programs
Array In C

Simple C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

C Program to demonstrate the 'atof' and 'gets' functions.

C Program to demonstrate the 'atof' and 'gets' functions. atof function converts the intial nptr string to the double. atof means ASCII to float. gets function reads string from the input stream. puts function prints string to the output stream This program will multiply to floating point numbers. The program will accept invalid data, and give you crap results as a result. Read more about C Programming Language .



/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/



#include <stdio.h>   
#include <stdlib.h>  

main()
{
 char str1[80], str2[80]; /* define a couple o' strings. */
 double result;  /* Result of multiplication. */

 puts ("This program will multiply to floating point numbers.");
 puts ("The program will accept invalid data, and give you");
 puts ("crap results as a result.\n");
 puts ("Please enter the first number.");
 gets(str1);

 puts ("And the second.");
 gets(str2);

 result = atof(str1) * atof(str2);
 printf("Answer is %8.2f\n", result);
}


Read more Similar C Programs
C Strings

Simple C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


C Program to Demonstrate getting the parameters from the command line.

C Program to getting the parameters from the command line. To pass the arguments from command line, in main function we have to declare two arguments . First one is number of arguments passed to the program and other one is pointer variable which gives the list of arguments passed. Read more about C Programming Language .



/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/



#include <stdio.h>

main(int argc, char *argv[]) {
 int count;

 /*  Main takes two variables 'argc' is the number of parms on the 
  *  command line and 'argv' is a pointer to each of the parameters.
  *
  * int argc    -- integer number called 'argc'
  * char *argv[] -- Character pointer array!
  */

 printf("%i parameters entered on the command line.\n", argc);

 /*
  * progname  argc = 1
  * progname parm1 parm2 argc = 3
  */

 /* 
  * We take 1 from argc because 
  * the argv array starts at zero 
  * an ends at argc -1
  */

 for (count = 0; count <= argc - 1; count++) {
  /* printf expects a pointer 
   * to the text 
   */

  printf("parm %d is %s\n", count, argv[count]);
 }
}
Read more Similar C Programs
C Strings

Simple C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


C Program to implement the Newton- Gregory forward interpolation

C Program to implement the Newton- Gregory forward interpolation. Newtons - Gregory forward difference formula is a finite difference identity capable of giving an interpolated value between the tabulated points {fk} in terms of the first value f0 and powers of the forward difference Δ. In this program we used the multidimensional arrays and arrays in c. Read more about C Programming Language .



/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/



#include<stdio.h>
#include<conio.h>
#define MaxN 100
#define Order_of_diff 4

void main ()
{
 float arr_x[MaxN+1], arr_y[MaxN+1], numerator=1.0, denominator=1.0, x, y, p, h, diff_table[MaxN+1][Order_of_diff+1];
 int i,j,n,k;
 clrscr();

 printf("Enter the value of n \n");
 scanf("%d",&n);
 printf("Enter the values of x and y");

 for(i=0; i<=n; i++)
  scanf("%f%f", &arr_x[i], &arr_y[i]);
 printf("Enter the value of x at which value of y is to be calculated");
 scanf("%f", &x);
 h=arr_x[1]-arr_x[0];

 for(i=0; i<=n-1; i++)
  diff_table[i][1]=arr_y[i+1]-arr_y[i];/*Creating the difference table and calculating first order differences*/
 for(j=2; j<=Order_of_diff; j++)/*Calculating higher order differences*/
  for(i=0; i<=n-j; i++)
   diff_table[i][j]=diff_table[i+1][j-1] - diff_table[i][j-1];
 i=0;

 while(!(arr_x[i]>x)) /* Finding x0 */
  i++;
 i--;
 p=(x-arr_x[i])/h;
 y=arr_y[i];

 for (k=1; k<=Order_of_diff; k++)
 {
  numerator *=p-k+1;
  denominator *=k;
  y +=(numerator/denominator)*diff_table[i][k];
 }
 printf("When x=%6.1f, y=%6.2f\n",x, y);
 getch();
}

Read more Similar C Programs
Learn C Programming

Simple C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


C Program to implement the Lagrange interpolation

C Program to implement the Lagrange interpolation. Lagrange interpolation is the polynomial interpolation. It is the process of passing a polynomial of degree n-1 through n points. In this program we used the arrays in c. Read more about C Programming Language .



/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/
#include<stdio.h>
#include<conio.h>
#define MaxN 90

void main()
{
 float arr_x[MaxN+1], arr_y[MaxN+1], numerator, denominator, x, y=0;
 int i, j, n;
 clrscr();
 printf("Enter the value of n: \n");
 scanf("%d", &n);
 printf("Enter the values of x and y: \n");
 for(i=0; i<=n; i++)
  scanf("%f%f", &arr_x[i], &arr_y[i]);
 printf("Enter the value of x at which value of y is to be calculated: ");
 scanf("%f", &x);
 for (i=0; i<=n; i++)
 {
  numerator=1;
  denominator=1;
  for (j=0; j<=n; j++)
   if(j!=i)
   {
    numerator *= x-arr_x[j];
    denominator *= arr_x[i]-arr_x[j];
   }
  y+=(numerator/denominator)*arr_y[i];
 }
 printf("When x=%4.1f y=%7.1f\n",x,y);
 getch();
}



Read more Similar C Programs
Learn C Programming

Simple C Programs

You can easily select the code by double clicking on the code area above. To get regular updates on new C programs, you can You can discuss these programs on our Facebook Page. Start a discussion right now,

our page! Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button) Like to get updates right inside your feed reader? Grab our feed!



C Program to check the given number is perfect or not.

C Program to check the given number is perfect or not?.  Perfect number is a positive integer that is equal to the sum of its proper positive divisors. example 6, divisor of 6 are 1, 2,3. Sum of divisors is 1+2+3=6.
Read more about C Programming Language .





/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/


#include<stdio.h>
int main(){
 int num,i=1,sum=0;

 printf("Enter a number: ");
 scanf("%d",&num);

 while(i<num){
  if(num%i==0)
   sum=sum+i;
  i++;
 }
 if(sum==num)
  printf("%d is a perfect number",i);
 else
  printf("%d is not a perfect number",i);

 return 0;
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!



C Program to print Pascal's Triangle

Pascal Triangle is started by 1, then followed by The binomial coefficient (n,k),
where n is the non negative integer and k is the integer between 0 and n.
Pascal triangle is the triangular array.
Read more about C Programming Language.
-->
/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include"stdio.h"
int main() 
{
    int r, i, j, b, num;
    printf("How many lines do you want?");
    scanf("%d", &r);
    b = r;
    for(i=0;i<r;i++) 
    {
        num = 1;
        for(j=0;j<=i;j++) 
        {
            printf("%d", num);
            num = (num * (i-j)/(j+1));
        }
        printf("\n");
    }
}
You can easily select the code by double clicking on the code area above. To get regular updates on new C programs, you can You can discuss these programs on our Facebook Page. Start a discussion right now,

our page! Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button) Like to get updates right inside your feed reader? Grab our feed!
--> To browse more C Programs visit this link

C Program to print Floyd's triangle

Floyd's triangle is the right angled triangular array of natural numbers. Here we used the nested For loop to print the Floyd's triangle.
Read more about C Programming Language .



/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>


int main(){
 int i,j,n,k=1;

 printf("Enter the range: ");
 scanf("%d",&n);

 printf("FLOYD'S TRIANGLE\n\n");
 for(i=1;i<=n;i++){
  for(j=1;j<=i;j++,k++)
   printf(" %d",k);
  printf("\n");
 }

 return 0;
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


Oct 13, 2011

C Program to calculate the total execution time of a program.

C Program to calculate the total execution time of a program. Here we used the "time.h" preprocessor. In this program we used the clock_t variables start and end , They starts the time counter and ends the counter. Execution time of a program is useful to calculate the efficiency of the program.
Read more about C Programming Language .




/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<time.h>

int main() 
{
 int i;
 double total_time;
 clock_t start, end;

 start = clock();//time count starts 

 srand(time(NULL));
 for (i = 0; i < 25000; i++) 
 {
  printf("random_number[%d]= %d\n", i + 1, rand());
 }
 end = clock();//time count stops 
 total_time = ((double) (end - start)) / CLK_TCK;//calulate total time
 printf("\nTime taken to print 25000 random number is: %f", total_time);
 return 0;
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

C Program to sort the string, using shell sort technique.

C Program to sort the string, using shell sort technique. Shell sort is the one of the oldest sorting technique, quite well for all types of arrays in c. The shell sort is a "diminishing increment sort", better known as a "comb sort" to the unwashed programming masses. The algorithm makes multiple passes through the list, and each time sorts a number of equally sized sets using the insertion sort . The size of the set to be sorted gets larger with each pass through the list, until the set consists of the entire list. This sets the insertion sort up for an almost-best case run each iteration with a complexity that approaches O(n) . Read more about C Programming Language .


/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void shell_sort(char *chars, int c)  {

 register int i, j, space, k;
 char x, a[5];

 a[0]=9; a[1]=5; a[2]=3; a[3]=2; a[4]=1;

 for(k=0; k < 5; k++) {
  space = a[k];
  for(i=space; i < c; ++i) {
   x = chars[i];
   for(j=i-space; (x < chars[j]) && (j >= 0); j=j-space)
    chars[j+space] = chars[j];
   chars[j+space] = x;
  }
 }
}

int main() {
 char string[300];
 printf("Enter a string:");
 gets(string);
 shell_sort(string, strlen(string));
 printf("The sorted string is: %s.\n", string);
 return 0;
}
Read more Similar C Programs
Array In C

Sorting Techniques

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


C program to implement MERGE sort.

Merge sort is based on the divide conquer strategy. Array is divided in to two halves.if the array length is n, then it is divided into n/2,n/4,n/8.... and each part is sorted independently, then conquered into the sorted array. The efficiency of merge sort is O(n log n). Read more about C Programming Language .
/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/



#include <stdio.h>
#include <stdlib.h>

#define MAX_ARY 10

void merge_sort(int x[], int end, int start);

int main(void) {
 int ary[MAX_ARY];
 int j = 0;

 printf("\n\nEnter the elements to be sorted: \n");
 for(j=0;j<MAX_ARY;j++)
  scanf("%d",&ary[j]);

 /* array before mergesort */
 printf("Before    :");
 for(j = 0; j < MAX_ARY; j++)
  printf(" %d", ary[j]);

 printf("\n");

 merge_sort(ary, 0, MAX_ARY - 1);

 /* array after mergesort */
 printf("After Merge Sort :");
 for(j = 0; j < MAX_ARY; j++)
  printf(" %d", ary[j]);

 printf("\n");
 getch();
}

/* Method to implement Merge Sort*/
void merge_sort(int x[], int end, int start) {
 int j = 0;
 const int size = start - end + 1;
 int mid  = 0;
 int mrg1 = 0;
 int mrg2 = 0;
 int executing[MAX_ARY];

 if(end == start)
  return;

 mid  = (end + start) / 2;

 merge_sort(x, end, mid);
 merge_sort(x, mid + 1, start);

 for(j = 0; j < size; j++)
  executing[j] = x[end + j];

 mrg1 = 0;
 mrg2 = mid - end + 1;

 for(j = 0; j < size; j++) {
  if(mrg2 <= start - end)
   if(mrg1 <= mid - end)
    if(executing[mrg1] > executing[mrg2])
     x[j + end] = executing[mrg2++];
    else
     x[j + end] = executing[mrg1++];
   else
    x[j + end] = executing[mrg2++];
  else
   x[j + end] = executing[mrg1++];
 }
}
Read more Similar C Programs
Array In C

Sorting Techniques

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!



C Program to implement Insertion sort.

Insertion sorting technique is the elementary sorting technique. Insertion sort sorts one element at a time, It is just like manual sorting by humans. Insertion sort is better for small set of elements. Insertion sort is slower than heap sort, shell sort, quick sort,and merge sort. Read more about C Programming Language .





/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/


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

void inst_sort(int []);

void main()
{
 int num[5],count;
 clrscr();
 printf("\nEnter the Five Elements to sort:\n");

 for (count=0;count<5;count++)
  scanf("%d",&num[count]);
 inst_sort(num);

 printf("\n\nElements after sorting: \n");
 for(count=0;count<5;count++)
  printf("%d\n",num[count]);
 getch();
}

// Function for Insertion Sorting
void inst_sort(int num[])
{
 int i,j,k;
 for(j=1;j<5;j++)
 {
  k=num[j];
  for(i=j-1;i>=0 && k<num[i];i--)
   num[i+1]=num[i];
  num[i+1]=k;
 }
}
Read more Similar C Programs
Array In C

Sorting Techniques

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!



C Program to sort a linked list.

Data structures using C, Linked list is a data structure in which the objects are arranged in a linear order. In this program, we sort the list elements in ascending order. Read more about C Programming Language .
/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/




#include <stdio.h>
#include <stdlib.h>
#define NULL 0

struct linked_list
{
 int number;
 struct linked_list *next;
};
typedef struct linked_list node;

main ()
{
 int n;
 node *head = NULL;
 void print(node *p);
 node *insert_Sort(node *p, int n);

 printf("Input the list of numbers.\n");
 printf("At end, type -999.\n");
 scanf("%d",&n);

 while(n != -999)
 {
  if(head == NULL)       /* create 'base' node */
  {
   head = (node *)malloc(sizeof(node));
   head ->number = n;
   head->next = NULL;

  }

  else    /* insert next item */
  {
   head = insert_Sort(head,n);
  }
  scanf("%d", &n);
 }
 printf("\n");
 print(head);
 print("\n");
}
node *insert_Sort(node *list, int x)
{
 node *p1, *p2, *p;
 p1 = NULL;
 p2 = list; /* p2 points to first node */

 for( ; p2->number < x ; p2 = p2->next)
 {
  p1 = p2;

  if(p2->next == NULL)
  {
   p2 = p2->next;    /* p2 set to NULL */
   break;      /* insert new node at end */
  }
 }

 /* key node found */
 p = (node *)malloc(sizeof(node)); /* space for new node */
 p->number = x;     /* place value in the new node */
 p->next = p2;      /* link new node to key node */
 if (p1 == NULL)
  list = p;      /* new node becomes the first node */
 else
  p1->next = p;  /* new node inserted after 1st node */
 return (list);
}
void print(node *list)
{
 if (list == NULL)
  printf("NULL");
 else
 {
  printf("%d-->",list->number);
  print(list->next);
 }
 return;
}
Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


C Program to reverse the first n characters in a file.

C Program to reverse the first n characters in a file using the command line arguments. Here we read the file name and n are specified on the command line. If file exists, it reverse the n characters, else it gives the error message. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/


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

void main(int argc, char *argv[])
{
 char a[15];
 char s[20];
 char n;
 int k;
 int j=0;
 int i;
 int len;
 FILE *fp;

 if(argc!=3)
 {
  puts("Improper number of arguments.");
  exit(0);
 }
 fp = fopen(argv[1],"r");
 if(fp == NULL)
 {
  puts("File cannot be opened.");
  exit(0);
 }

 k=*argv[2]-48;
 n = fread(a,1,k,fp);
 a[n]='\0';
 len=strlen(a);
 for(i=len-1;i>=0;i--)
 {
  s[j]=a[i];
  printf("%c",s[j]);
  j=j+1;
 }
 s[j+1]='\0';
 getch();
}/*(Note: The file name and n are specified on the command line.)*/

Read more Similar C Programs
File operations

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!



C program which copies one file contents to another file.

C Program which copies one file contents to another file. Here we read the one file and copies the characters to another file which are existed in the disc. Here we read the file names from the command line.Read more about C Programming Language .


/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/



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

void main(int argc, char *argv[])
{
 FILE *fs,*ft;
 char ch;
 clrscr();
 if(argc!=3)
 {
  puts("Invalid number of arguments.");
  exit(0);
 }
 fs = fopen(argv[1],"r");
 if(fs==NULL)
 {
  puts("Source file cannot be opened.");
  exit(0);
 }
 ft = fopen(argv[2],"w");
 if (ft==NULL)
 {
  puts("Target file cannot be opened.");
  fclose(fs);
  exit(0);
 }
 while(1)
 {
  ch=fgetc(fs);
  if (ch==EOF)
   break;
  else
   fputc(ch,ft);
 }
 fclose(fs);
 fclose(ft);
 getch();
}

Read more Similar C Programs
File operations

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


Oct 12, 2011

C Program to generate random numbers.

C Program to generate random numbers. Random Numbers are numbers which are produced by a process and its outcome is unpredictable.The function srand() is used to seed the random sequence. The rand() function shall compute a sequence of pseudo-random integers in the range [0, {RAND_MAX}] with a period of at least 2^32.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main() {
 int i, num;
 printf("Enter how many random numbers you want?\n");
 scanf("%d", &num);
 //The function srand() is used to seed the random sequence. 
 //srand() will generate a specific "random" sequence over and over again. 
 srand(time(NULL));
 //The rand() function shall compute a sequence of pseudo-random 
 //integers in the range [0, {RAND_MAX}]  with a period of at least 2^32.
 for (i = 0; i < num; i++) {
  printf("random_number[%d]= %d\n", i + 1, rand());
 }

 printf("A number between 0 and 99: %d\n", rand() % 100);

 printf("A number between 0 and 9: %d\n", rand() % 10);
 
 return 0;
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!



C Program to show Sleep() function example.

C Program to show Sleep() function example. sleep() function stops the execution of the program, wherever its invoked or called. It takes the arguments in microseconds. In Windows we use Sleep() function i,e 'S', and for other systems it is sleep() function.
Read more about C Programming Language .
/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                                  Happy Coding
***********************************************************/

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

int main()
{
 printf("This is the message before sleep() function\n");
 Sleep(1000); //1000 microsecond= 1 second will sleep...
 printf("This is the message after 1 second");
 getch();
 return 0;
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

List of C Programs