Pages

ATM Programing in C

Sunday 30 June 2013
‎                                          /*Note Pin code is 1234*/
#include<stdio.h>
#include<conio.h>

void main(void)
{
    unsigned long amount=2000,deposit,withdr​aw;
    int choice,pin=0,k=0;
    char another='y';

    while(pin!=1234)
    {
        clrscr();
        gotoxy(30,25);
        printf("Enter pin:");
        scanf("%d",&pin);
    }
    clrscr();
    do
    {

    printf("********Welcome to ATM Service**************\n");
    printf("1. Check Balance\n");
    printf("2. Withdraw Cash\n");
    printf("3. Deposit Cash\n");
    printf("4. Quit\n");
    printf("******************​**************************​*\n\n");
    printf("Enter your choice: ");
    scanf("%d",&choice);
    switch(choice)
    {
    case 1:
        printf("\nYour Balance is Rs : %lu ",amount);
        break;
    case 2:
        printf("\nEnter the amount to withdraw: ");
        scanf("%lu",&withdraw);
        if(withdraw%100!=0)
        {
            printf("\nPlease enter amount in multiples of 100");
        }
        else if(withdraw>(amount-500))
        {
            printf("\nInsufficient Funds");
        }
        else
        {
            amount=amount-withdraw;
            printf("\n\nPlease collect cash");
            printf("\nYour balance is %lu",amount);
        }
            break;
    case 3:
        printf("\nEnter amount to deposit");
        scanf("%lu",&deposit);
        amount=amount+deposit;
        printf("Your balance is %lu",amount);
        break;
    case 4:
        printf("\nThank you for using ATM");
        break;
    default:
        printf("\nInvalid Choice");
    }

    printf("\n\n\nDo you want another transaction?(y/n): ");
    fflush(stdin);
    scanf("%c",&another);

    if(another=='n'||another==​'N')
    k=1;
    }
    while(!k);
    printf("\n\nHave a nice day");
    getch();
}
Read more ...

To Calculate Sum of Natural Numbers

Saturday 29 June 2013
#include <stdio.h>
int main()
{
    int n, count;
    int sum=0;
 
    printf("Enter an integer: ");
    scanf("%d",&n);
    count=1;
  
   while(count<=n)       /* while loop terminates if count>n */
   {
       sum+=count;       /* sum=sum+count */
       ++count;
   }
   printf("Sum = %d",sum);
   return 0;
}
Read more ...

To Convert Hexadecimal to Octal and Vice Versa

Saturday 29 June 2013
#include <stdio.h>

int main()
{
int n;

printf("Enter an octal number: ");
scanf("%o",&n); /*Takes number in octal format. */

/* %o and %x will display the number is octal format and hexadecimal form respectively. */

printf("%o in octal = %x in hexadecimal", n, n);

printf("\nEnter an hexadecimal number: ");
scanf("%x",&n);               /* Takes number in hexadecimal format.*/

printf("%x in hexadecimal = %o in octal", n, n);
return 0;
}
Read more ...

To Convert Decimal to Hexadecimal Number and Hexadecimal to Decimal Number

Saturday 29 June 2013
#include <stdio.h>
#include <math.h>
#include <string.h>

void decimal_hex(int n, char hex[]);
int hex_decimal(char hex[]);
int main()
{
    char hex[20],c;
    int n;

    printf("Instructions:\n");
    printf("Enter h to convert decimal to hexadecimal:\n");
    printf("Enter d to hexadecimal number to decimal:\n");
    printf("Enter a character: ");
    scanf("%c",&c);

    if (c=='h' || c=='H')
    {
        printf("Enter decimal number: ");
        scanf("%d",&n);
        decimal_hex(n,hex);
        printf("Hexadecimal number: %s",hex);
    }
    if (c=='d' || c=='D')
    {
        printf("Enter hexadecimal number: ");
        scanf("%s",hex);
        printf("Decimal number: %d",hex_decimal(hex));
    }
    return 0;
}

/* Function to convert decimal to hexadecimal. */

void decimal_hex(int n, char hex[])
{
    int i=0,rem;
    while (n!=0)
    {
        rem=n%16;
        switch(rem)
        {
            case 10:
              hex[i]='A';
              break;
            case 11:
              hex[i]='B';
              break;
            case 12:
              hex[i]='C';
              break;
            case 13:
              hex[i]='D';
              break;
            case 14:
              hex[i]='E';
              break;
            case 15:
              hex[i]='F';
              break;
            default:
              hex[i]=rem+'0';
              break;
        }
        ++i;
        n/=16;
    }
    hex[i]='\0';
    strrev(hex);            /* Reverse string */
}

/* Function to convert hexadecimal to decimal. */

int hex_decimal(char hex[])  
{
    int i, length, sum=0;
    for(length=0; hex[length]!='\0'; ++length);
    for(i=0; hex[i]!='\0'; ++i, --length)
    {
        if(hex[i]>='0' && hex[i]<='9')
            sum+=(hex[i]-'0')*pow(16,length-1);
        if(hex[i]>='A' && hex[i]<='F')
            sum+=(hex[i]-55)*pow(16,length-1);
        if(hex[i]>='a' && hex[i]<='f')
            sum+=(hex[i]-87)*pow(16,length-1);
    }
    return sum;
}
Read more ...

To Find Number of Consonants, Digits, Vowels and White Space Character

Saturday 29 June 2013
#include<stdio.h>

int main()
{
    char line[150];
    int i,v,c,ch,d,s,o;
    o=v=c=ch=d=s=0;
   
    printf("Enter a line of string:\n");
    gets(line);
   
    for(i=0;line[i]!='\0';++i)
    {
        if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
            ++v;
        else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
            ++c;
        else if(line[i]>='0'&&c<='9')
            ++d;
        else if (line[i]==' ')
            ++s;
    }

    printf("Vowels: %d",v);
    printf("\n Consonants: %d",c);
    printf("\n Digits: %d",d);
    printf("\n White spaces: %d",s);
    return 0;
}
Read more ...

To Find Largest Element Using Dynamic Memory Allocation

Saturday 29 June 2013
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,n;
    float *data;

    printf("Enter total number of elements(1 to 100): ");
    scanf("%d",&n);

    data=(float*)calloc(n,sizeof(float));                 /* Allocates the memory for 'n' elements */

    if(data==NULL)
    {
        printf("Error!!! memory not allocated.");
        exit(0);
    }
    printf("\n");

    for(i=0;i<n;++i)                                             /* Stores number entered by user. */
    {
       printf("Enter Number %d: ",i+1);
       scanf("%f",data+i);
    }

    for(i=1;i<n;++i)                                      /* Loop to store largest number at address data */
    {
       if(*data<*(data+i))                              /* Change < to > if you want to find smallest number */
           *data=*(data+i);
    }

    printf("Largest element = %.2f",*data);
    return 0;
}
Read more ...

To Swap Elements Using Call by Reference

Saturday 29 June 2013
#include<stdio.h>

void cycle(int *x,int *y,int *z);
int main()
{
    int x,y,z;
    printf("Enter value of x, y and z respectively: ");
    scanf("%d%d%d",&x,&y,&z);

    printf("Value before swapping:\n");
    printf("x=%d\ny=%d\nz=%d\n",x,y,z);

    cycle(&x,&y,&z);

    printf("Value after swapping numbers in cycle:\n");
    printf("x=%d\ny=%d\nz=%d\n",x,y,z);
    return 0;
}
void cycle(int *x,int *y,int *z)
{
    int temp;

    temp=*y;
    *y=*x;
    *x=*z;
    *z=temp;
}
Read more ...

To Calculate Standard Deviation by Passing it to Function

Saturday 29 June 2013
#include <stdio.h>
#include <math.h>

float standard_deviation(float data[], int n);
int main()
{
    int n, i;
    float data[100];

    printf("Enter number of datas( should be less than 100): ");
    scanf("%d",&n);
    printf("Enter elements: ");

    for(i=0; i<n; ++i)
        scanf("%f",&data[i]);
    printf("\n");
    printf("Standard Deviation = %.2f", standard_deviation(data,n));
    return 0;
}
float standard_deviation(float data[], int n)
{
    float mean=0.0, sum_deviation=0.0;
    int i;

    for(i=0; i<n;++i)
    {
        mean+=data[i];
    }
    mean=mean/n;

    for(i=0; i<n;++i)
    sum_deviation+=(data[i]-mean)*(data[i]-mean);
    return sqrt(sum_deviation/n);          
}
Read more ...

To Convert Binary to Octal and Vice Versa

Friday 28 June 2013
#include <stdio.h>
#include <math.h>

int binary_octal(int n);
int octal_binary(int n);
int main()
{
    int n;
    char c;

    printf("Instructions:\n");
    printf("Enter alphabet 'o' to convert binary to octal.\n");
    printf("2. Enter alphabet 'b' to convert octal to binary.\n");
    scanf("%c",&c);

    if ( c=='o' || c=='O')
    {
        printf("Enter a binary number: ");
        scanf("%d",&n);
        printf("%d in binary = %d in octal", n, binary_octal(n));
    }
    if ( c=='b' || c=='B')
    {
        printf("Enter a octal number: ");
        scanf("%d",&n);
        printf("%d in octal = %d in binary",n, octal_binary(n));
    }
    return 0;
}

/* Function to convert binary to octal. */

int binary_octal(int n) 
{
    int octal=0, decimal=0, i=0;
    while(n!=0)
    {
        decimal+=(n%10)*pow(2,i);
        ++i;
        n/=10;
    }

/*At this time, the decimal variable contains corresponding decimal value of binary number. */

    i=1;
    while (decimal!=0)
    {
        octal+=(decimal%8)*i;
        decimal/=8;
        i*=10;
    }
    return octal;
}

/* Function to convert octal to binary.*/

int octal_binary(int n) 
{
    int decimal=0, binary=0, i=0;
    while (n!=0)
    {
        decimal+=(n%10)*pow(8,i);
        ++i;
        n/=10;
    }

/* At this time, the decimal variable contains corresponding decimal value of that octal number. */

    i=1;
    while(decimal!=0)
    {
        binary+=(decimal%2)*i;
        decimal/=2;
        i*=10;
    }
    return binary;
}
Read more ...

To Convert Octal Number to Decimal and Vice Versa

Friday 28 June 2013
#include <stdio.h>
#include <math.h>

int decimal_octal(int n);
int octal_deciaml(int n);
int main()
{
   int n;
   char c;

   printf("Instructions:\n");
   printf("1. Enter alphabet 'o' to convert decimal to octal.\n");
   printf("2. Enter alphabet 'd' to convert octal to decimal.\n");
   scanf("%c",&c);

   if (c =='d' || c == 'D')
   {
       printf("Enter an octal number: ");
       scanf("%d", &n);
       printf("%d in octal = %d in decimal", n, octal_decimal(n));
   }
   if (c =='o' || c == 'O')
   {
       printf("Enter a decimal number: ");
       scanf("%d", &n);
       printf("%d in decimal = %d in octal", n, decimal_octal(n));
   }
   return 0;
}

/* Function to convert decimal to octal */

int decimal_octal(int n)
{
    int rem, i=1, octal=0;
    while (n!=0)
    {
        rem=n%8;
        n/=8;
        octal+=rem*i;
        i*=10;
    }
    return octal;
}

/* Function to convert octal to decimal */

int octal_decimal(int n)
{
    int decimal=0, i=0, rem;
    while (n!=0)
    {
        rem = n%10;
        n/=10;
        decimal += rem*pow(8,i);
        ++i;
    }
    return decimal;
}
Read more ...

To Convert Binary Number to Decimal or Decimal Number to Binary

Friday 28 June 2013
#include <stdio.h>
#include <math.h>

int binary_decimal(int n);
int decimal_binary(int n);
int main()
{
   int n;
   char c;

   printf("Instructions:\n");
   printf("1. Enter alphabet 'd' to convert binary to decimal.\n");
   printf("2. Enter alphabet 'b' to convert decimal to binary.\n");
   scanf("%c",&c);

   if (c =='d' || c == 'D')
   {
       printf("Enter a binary number: ");
       scanf("%d", &n);
       printf("%d in binary = %d in decimal", n, binary_decimal(n));
   }
   if (c =='b' || c == 'B')
   {
       printf("Enter a decimal number: ");
       scanf("%d", &n);
       printf("%d in decimal = %d in binary", n, decimal_binary(n));
   }
   return 0;
}

/* Function to convert decimal to binary.*/

int decimal_binary(int n) 
{
    int rem, i=1, binary=0;
    while (n!=0)
    {
        rem=n%2;
        n/=2;
        binary+=rem*i;
        i*=10;
    }
    return binary;
}

/* Function to convert binary to decimal.*/

int binary_decimal(int n)
{
    int decimal=0, i=0, rem;
    while (n!=0)
    {
        rem = n%10;
        n/=10;
        decimal += rem*pow(2,i);
        ++i;
    }
    return decimal;
}
Read more ...

To Calculate Factorial Using Recursion

Friday 28 June 2013
#include<stdio.h>

int factorial(int n);
int main()
{
    int n;

    printf("Enter an positive integer: ");
    scanf("%d",&n);

    printf("Factorial of %d = %ld", n, factorial(n));
    return 0;
}
int factorial(int n)
{
    if(n!=1)
     return n*factorial(n-1);
}
Read more ...

To Calculate Sum using Recursion

Friday 28 June 2013
#include<stdio.h>

int add(int n);
int main()
{
    int n;

    printf("Enter an positive integer: ");
    scanf("%d",&n);
    printf("Sum = %d",add(n));
    return 0;
}

int add(int n)
{
    if(n!=0)
    return n+add(n-1);  /* recursive call */
}
Read more ...

To Calculate Power

Friday 28 June 2013
/* C program to calculate the power of an integer*/

#include <stdio.h>
int main()
{
  int base, exp;
  long long int value=1;

  printf("Enter base number and exponent respectively: ");
  scanf("%d%d", &base, &exp);

  while (exp!=0)
  {
      value*=base;       /* value = value*base; */
      --exp;
  }
  printf("Answer = %d", value);
}
Read more ...

Write a C Program to Find Roots of Quadratic Equation

Friday 28 June 2013
/* Library function sqrt() computes the square root. */

#include <stdio.h>
#include <math.h>       /* This is needed to use sqrt() function.*/

int main()
{
  float a, b, c, determinant, r1,r2;
  float real, imag;

  printf("Enter coefficients a, b and c: ");
  scanf("%f%f%f",&a,&b,&c);

  determinant=b*b-4*a*c;

  if (determinant>0)
  {
      r1= (-b+sqrt(determinant))/(2*a);
      r2= (-b-sqrt(determinant))/(2*a);
      printf("Roots are: %.2f and %.2f",r1 , r2);
  }
  else if (determinant==0)
  {
    r1 = r2 = -b/(2*a);
    printf("Roots are: %.2f and %.2f", r1, r2);
  }
  else
  {
    real= -b/(2*a);
    imag = sqrt(-determinant)/(2*a);
    printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
  }
  return 0;
}
Read more ...

To Find ASCII value of a character entered by user

Friday 28 June 2013
#include <stdio.h>

int main()
{
   char c;

   printf("Enter a character: ");
   scanf("%c",&c);                 /* Takes a character from user */

   printf("ASCII value of %c = %d",c,c);
 
   return 0;
}
Read more ...

Computes the size of variable using sizeof operator

Friday 28 June 2013
#include <stdio.h>

int main()
{
  int a;
  float b;
  double c;
  char d;

  printf("Size of int: %d bytes\n",sizeof(a));
  printf("Size of float: %d bytes\n",sizeof(b));
  printf("Size of double: %d bytes\n",sizeof(c));
  printf("Size of char: %d byte\n",sizeof(d));

  return 0;
}
Read more ...

To Add Two Numbers Using Pointers

Friday 28 June 2013
#include <stdio.h>

int main()
{
   int first, second, *p, *q, sum;

   printf("Enter two integers to add\n");
   scanf("%d%d", &first, &second);

   p = &first;
   q = &second;
   sum = *p + *q;

   printf("Sum of entered numbers = %d\n",sum);
   return 0;
}
Read more ...

To Find Maximum Element in array using Pointers

Friday 28 June 2013
#include <stdio.h>

int main()
{
  long array[100], *maximum, size, c, location = 1;

  printf("Enter the number of elements in array\n");
  scanf("%ld", &size);

  printf("Enter %ld integers\n", size);

  for ( c = 0 ; c < size ; c++ )

    scanf("%ld", &array[c]);

  maximum  = array;
  *maximum = *array;

  for (c = 1; c < size; c++)
  {
    if (*(array+c) > *maximum)
    {
       *maximum = *(array+c);
       location = c+1;
    }
  }
  printf("Maximum element found at location %ld and it's value is %ld.\n", location, *maximum);
  return 0;
}
Read more ...

To Find Maximum Element in array

Friday 28 June 2013
#include <stdio.h>

int main()
{
  int array[100];
  int maximum, size, c;
  int location = 1;

  printf("Enter the number of elements in array\n");
  scanf("%d", &size);

  printf("Enter %d integers\n", size);

  for (c = 0; c < size; c++)
    scanf("%d", &array[c]);

  maximum = array[0];

  for (c = 1; c < size; c++)
  {
    if (array[c] > maximum)
    {
       maximum  = array[c];
       location = c+1;
    }
  }

  printf("Maximum element is present at location %d and it's value is %d.\n", location, maximum);
  return 0;
}
Read more ...

To Find minimum element in array

Friday 28 June 2013
#include <stdio.h>

int main()
{
    int array[100];
    int minimum, size, c;
    int location = 1;

    printf("Enter the number of elements in array\n");
    scanf("%d",&size);

    printf("Enter %d integers\n", size);

    for ( c = 0 ; c < size ; c++ )
       scanf("%d", &array[c]);

    minimum = array[0];

    for ( c = 1 ; c < size ; c++ )
    {
        if ( array[c] < minimum )
        {
           minimum = array[c];
           location = c+1;
        }
    }
    printf("Minimum element is present at location %d and it's value is %d.\n", location, minimum);
    return 0;
}
Read more ...

Linear search C program

Friday 28 June 2013
#include <stdio.h>

int main()
{
   int array[100];
   int search, c, number;

   printf("Enter the number of elements in array\n");
   scanf("%d",&number);

   printf("Enter %d numbers\n", number);

   for ( c = 0 ; c < number ; c++ )
      scanf("%d",&array[c]);

   printf("Enter the number to search\n");
   scanf("%d",&search);

   for ( c = 0 ; c < number ; c++ )
   {
      if ( array[c] == search )        /* if required element found */
      {
         printf("%d is present at location %d.\n", search, c+1);
         break;
      }
   }
   if ( c == number )
      printf("%d is not present in array.\n", search);  
   return 0;
}
Read more ...

Binary Search Implementation in C

Friday 28 June 2013
#include <stdio.h>

int main()
{
   int c, first, last, middle, n, search;
   int array[100];

   printf("Enter number of elements\n");
   scanf("%d",&n);

   printf("Enter %d integers\n", n);

   for ( c = 0 ; c < n ; c++ )
      scanf("%d",&array[c]);

   printf("Enter value to find\n");
   scanf("%d",&search);

   first = 0;
   last = n - 1;
   middle = (first+last)/2;

   while( first <= last )
   {
      if ( array[middle] < search )
         first = middle + 1; 
      else if ( array[middle] == search )
      {
         printf("%d found at location %d.\n", search, middle+1);
         break;
      }
      else
         last = middle - 1;
         middle = (first + last)/2;
   }
   if ( first > last )
      printf("Not found! %d is not present in the list.\n", search);
   return 0;
}
Read more ...

To Reverse an array in C

Friday 28 June 2013
#include <stdio.h>

int main()
{
   int n, c, d;
   int a[100], b[100];

   printf("Enter the number of elements in array\n");
   scanf("%d", &n);

   printf("Enter the array elements\n");

   for (c = 0; c < n ; c++)
      scanf("%d", &a[c]);

   /*
    * Copying elements into array b starting from end of array a
    */

   for (c = n - 1, d = 0; c >= 0; c--, d++)
      b[d] = a[c];

   /*
    * Copying reversed array into original.
    * Here we are modifying original array, this is optional.
    */

   for (c = 0; c < n; c++)
      a[c] = b[c];

   printf("Reverse array is\n");

   for (c = 0; c < n; c++)
      printf("%d\n", a[c]);
   return 0;
}
Read more ...

To Insert an element in an array

Friday 28 June 2013
#include <stdio.h>

int main()
{
   int array[100];
   int position, c, n, value;

   printf("Enter number of elements in array\n");
   scanf("%d", &n);

   printf("Enter %d elements\n", n);

   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);

   printf("Enter the location where you wish to insert an element\n");
   scanf("%d", &position);

   printf("Enter the value to insert\n");
   scanf("%d", &value);

   for (c = n - 1; c >= position - 1; c--)
      array[c+1] = array[c];
   array[position-1] = value;

   printf("Resultant array is\n");

   for (c = 0; c <= n; c++)
      printf("%d\n", array[c]);
   return 0;
}
Read more ...

To Delete an element from an array

Friday 28 June 2013
#include <stdio.h>

int main()
{
   int array[100];
   int position, c, n;

   printf("Enter number of elements in array\n");
   scanf("%d", &n);

   printf("Enter %d elements\n", n);

   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);

   printf("Enter the location where you wish to delete element\n");
   scanf("%d", &position);

   if ( position >= n+1 )
      printf("Deletion not possible.\n");
   else
   {
      for ( c = position - 1 ; c < n - 1 ; c++ )
         array[c] = array[c+1];
      printf("Resultant array is\n");

      for( c = 0 ; c < n - 1 ; c++ )
         printf("%d\n", array[c]);
   }
    return 0;
}
Read more ...

Bubble sort algorithm in c

Friday 28 June 2013
#include <stdio.h>

int main()
{
  int array[100];
  int n, c, d, swap;

  printf("Enter number of elements\n");
  scanf("%d", &n);

  printf("Enter %d integers\n", n);

  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);

  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1])          /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }
  printf("Sorted list in ascending order:\n");

  for ( c = 0 ; c < n ; c++ )
     printf("%d\n", array[c]);
  return 0;
}
Read more ...

To Merge two sorted arrays

Friday 28 June 2013
#include <stdio.h>

void merge(int [], int, int [], int, int []);
int main()
{
    int a[100], b[100], sorted[200];
    int m, n, c;

    printf("Input number of elements in first array\n");
    scanf("%d", &m);

    printf("Input %d integers\n", m);

    for (c = 0; c < m; c++)
    {
       scanf("%d", &a[c]);
    }
    printf("Input number of elements in second array\n");
    scanf("%d", &n);

    printf("Input %d integers\n", n);

    for (c = 0; c < n; c++)
    {
       scanf("%d", &b[c]);
    }
    merge(a, m, b, n, sorted);

    printf("Sorted array:\n");

    for (c = 0; c < m + n; c++)
    {
       printf("%d\n", sorted[c]);
    }
    return 0;
}
void merge(int a[], int m, int b[], int n, int sorted[])
{
  int i, j, k;
  j = k = 0;

  for (i = 0; i < m + n;)
  {
     if (j < m && k < n)
     {
       if (a[j] < b[k])
       {
           sorted[i] = a[j];
           j++;
       }
       else
       {
           sorted[i] = b[k];
           k++;
        }
         i++;
     }
     else if (j == m)
     {
      for (; i < m + n;)
      {
        sorted[i] = b[k];
        k++;
        i++;
      }
     }
     else
     {
      for (; i < m + n;)
      {
        sorted[i] = a[j];
        j++;
        i++;
      }
     }
  }
}
Read more ...

Insertion sort algorithm implementation in C

Friday 28 June 2013
                                                 /* insertion sort ascending order */
#include <stdio.h>

int main()
{
  int n, c, d, t;
  int array[1000];

  printf("Enter number of elements\n");
  scanf("%d", &n);

  printf("Enter %d integers\n", n);

  for (c = 0; c < n; c++)
  {
    scanf("%d", &array[c]);
  }
  for (c = 1 ; c <= n - 1; c++)
  {
    d = c;
    while ( d > 0 && array[d] < array[d-1])
    {
      t = array[d];
      array[d]   = array[d-1];
      array[d-1] = t;
      d--;
    }
  }
  printf("Sorted list in ascending order:\n");

  for (c = 0; c <= n - 1; c++)
  {
    printf("%d\n", array[c]);
  }
  return 0;
}
Read more ...

Selection Sort algorithm implementation in C

Friday 28 June 2013
#include <stdio.h>

int main()
{
   int array[100];
   int n, c, d, position, swap;

   printf("Enter number of elements\n");
   scanf("%d", &n);

   printf("Enter %d integers\n", n);

   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);

   for ( c = 0 ; c < ( n - 1 ) ; c++ )
   {
      position = c;

      for ( d = c + 1 ; d < n ; d++ )
      {
         if ( array[position] > array[d] )
            position = d;
      }
      if ( position != c )
      {
         swap = array[c];
         array[c] = array[position];
         array[position] = swap;
      }
  }
   printf("Sorted list in ascending order:\n");

   for ( c = 0 ; c < n ; c++ )
      printf("%d\n", array[c]);
   return 0;
}
Read more ...

To Add Two matrix

Friday 28 June 2013
#include <stdio.h>

int main()
{
   int m, n, c, d;
   int first[10][10], second[10][10], sum[10][10];

   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);
   printf("Enter the elements of first matrix\n");

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         scanf("%d", &first[c][d]);

   printf("Enter the elements of second matrix\n");

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
            scanf("%d", &second[c][d]);

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         sum[c][d] = first[c][d] + second[c][d];

   printf("Sum of entered matrices:-\n");

   for ( c = 0 ; c < m ; c++ )
   {
      for ( d = 0 ; d < n ; d++ )
         printf("%d\t", sum[c][d]);
      printf("\n");
   }
   return 0;
}
Read more ...

C program to transpose a matrix

Friday 28 June 2013
#include <stdio.h>

int main()
{
   int m, n, c, d;
   int matrix[10][10], transpose[10][10];

   printf("Enter the number of rows and columns of matrix ");
   scanf("%d%d",&m,&n);
   printf("Enter the elements of matrix \n");

   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         scanf("%d",&matrix[c][d]);
      }
   }
   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         transpose[d][c] = matrix[c][d];
      }
   }
   printf("Transpose of entered matrix :-\n");
   for( c = 0 ; c < n ; c++ )
   {
      for( d = 0 ; d < m ; d++ )
      {
         printf("%d\t",transpose[c][d]);
      }
      printf("\n");
   }
   return 0;
}
Read more ...

Matrix multiplication in c

Friday 28 June 2013
#include <stdio.h>

int main()
{
  int m, n, p, q, c, d, k;
  int sum = 0;
  int first[10][10], second[10][10], multiply[10][10];

  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");

  for (  c = 0 ; c < m ; c++ )
    for ( d = 0 ; d < n ; d++ )
      scanf("%d", &first[c][d]);

  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d", &p, &q);

  if ( n != p )
    printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
  {
    printf("Enter the elements of second matrix\n");
    for ( c = 0 ; c < p ; c++ )
      for ( d = 0 ; d < q ; d++ )
        scanf("%d", &second[c][d]);
    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
      {
        for ( k = 0 ; k < p ; k++ )
        {
          sum = sum + first[c][k]*second[k][d];
        }
        multiply[c][d] = sum;
        sum = 0;
      }
    }
    printf("Product of entered matrices:-\n");
    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
        printf("%d\t", multiply[c][d]);
      printf("\n");
    }
  }
  return 0;
}
Read more ...

Reverse String in C

Friday 28 June 2013
#include <stdio.h>
#include <string.h>

int main()
{
   char arr[100];

   printf("Enter a string to reverse\n");
   gets(arr);

   strrev(arr);
   printf("Reverse of entered string is \n%s\n",arr);
   return 0;
}
Read more ...

To Find String Length

Friday 28 June 2013
#include <stdio.h>
#include <string.h>

int main()
{
   char a[100];
   int length;

   printf("Enter a string to calculate it's length\n");
   gets(a);

   length = strlen(a);
   printf("Length of entered string is = %d\n",length);
   return 0;
}
Read more ...

Remove Vowels From a String in C using Pointers

Friday 28 June 2013
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define TRUE 1
#define FALSE 0

int check_vowel(char);
main()
{
   char string[100], *temp, *pointer;
   char ch, *start;

   printf("Enter a string\n");
   gets(string);

   temp = string;
   pointer = (char*)malloc(100);
   if( pointer == NULL )
   {
      printf("Unable to allocate memory.\n");
      exit(EXIT_FAILURE);
   }
   start = pointer;
   while(*temp)
   {
      ch = *temp;
      if ( !check_vowel(ch) )
      {
         *pointer = ch;
          pointer++;
      }
      temp++;
   }
   *pointer = '\0';
   pointer = start;
   strcpy(string, pointer);           /* If you wish to convert original string */
   free(pointer);
   printf("String after removing vowel is \"%s\"\n", string);
   return 0;
}
int check_vowel(char a)
{
   if ( a >= 'A' && a <= 'Z' )
      a = a + 'a' - 'A';
   if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
      return TRUE;
  return FALSE;
}
Read more ...

Remove Vowels From a String in C

Friday 28 June 2013
Remove vowels string  C : C program to remove vowels from a string, or to remove, if the input string is "C programming language" so the output will be "C Programming". The program will establish us a new string and processing input string character by character, and if a vowel is something new Finder Series confirms otherwise added character in the new series, ends after the string we copy new string convoys string. A string without vowels we finally.

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

int check_vowel(char);

int main()
{
  char s[100], t[100];
  int i, j = 0;

  printf("Enter a string to delete vowels\n");
  gets(s);

  for(i = 0; s[i] != '\0'; i++)
  {
    if(check_vowel(s[i]) == 0)
    {    
      t[j] = s[i];
      j++;
    }
  }

  t[j] = '\0';

  strcpy(s, t);    //We are changing initial string

  printf("String after deleting vowels: %s\n", s);

  return 0;
}

int check_vowel(char c)
{
  switch(c)
  {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
      return 1;
    default:
      return 0;
  }
}
Read more ...

Sort a String in alphabetic order

Friday 28 June 2013
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void sort_string(char*);
int main()
{
   char string[100];

   printf("Enter some text\n");
   gets(string);

   sort_string(string);
   printf("%s\n", string);

   return 0;
}
void sort_string(char *s)
{
   int c, d = 0;
   int length;
   char *pointer, *result, ch;

   length = strlen(s);
   result = (char*)malloc(length+1);
   pointer = s;

   for ( ch = 'a' ; ch <= 'z' ; ch++ )
   {
      for ( c = 0 ; c < length ; c++ )
      {
         if ( *pointer == ch )
         {
            *(result+d) = *pointer;
            d++;
         }
         pointer++;
      }
      pointer = s;
   }
   *(result+d) = '\0';
   strcpy(s, result);
   free(result);
}
Read more ...

Remove spaces, blanks from a string in C

Friday 28 June 2013
#include <stdio.h>

int main()
{
   char text[100], blank[100];
   int c = 0, d = 0;

   printf("Enter some text\n");
   gets(text);

   while (text[c] != '\0')
   {
      if (!(text[c] == ' ' && text[c+1] == ' '))
     {
        blank[d] = text[c];
        d++;
     }
      c++;
   }
   blank[d] = '\0';
   printf("Text after removing blanks\n%s\n", blank);
   return 0;
}
Read more ...

Swap Two Strings

Friday 28 June 2013

 Write a C Program to Swap Two Strings.

#include <stdio.h>
#include <string.h>
#include <malloc.h>

int main()
{
   char first[100], second[100];
   char *temp;

   printf("Enter the first string\n");
   gets(first);

   printf("Enter the second string\n");
   gets(second);

   printf("\nBefore Swapping\n");
   printf("First string: %s\n",first);
   printf("Second string: %s\n\n",second);

   temp = (char*)malloc(100);

   strcpy(temp,first);
   strcpy(first,second);
   strcpy(second,temp);

   printf("After Swapping\n");
   printf("First string: %s\n",first);
   printf("Second string: %s\n",second);

   return 0;
}
Read more ...

To Find Frequency of characters in a string in C

Friday 28 June 2013
This program calculates the frequency of characters in a string, that is to say, the characters are present, how many times in a row. For example, the string "code" each of the character 'c', 'o',  'd' and 'e' appeared once. Considering all lowercase, ignoring other characters (special characters). You can easily program this large and special characters to deal with.

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

int main()
{
   char string[100], ch;
   int c = 0, count[26] = {0};

   printf("Enter a string\n");
   gets(string);

   while ( string[c] != '\0' )
   {
      /* Considering characters from 'a' to 'z' only */
       if ( string[c] >= 'a' && string[c] <= 'z' )
         count[string[c]-'a']++;
         c++;
   }
   for ( c = 0 ; c < 26 ; c++ )
   {
      if( count[c] != 0 )
         printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);
   }
   return 0;
}
Read more ...

Anagram in C

Friday 28 June 2013
Anagram c: C program to check whether two strings are anagrams or not, it is assumed that only the string alphabets. Two words are anagrams of each other, is removed when the letters of a word, re-form. 'S another word above definition shows that two strings anagrams that all the characters of the two strings as often. For example, "abc" and "cab" an anagram strings, here each character 'a', 'b' and 'c' only once in the two strands occur. Our algorithm tries to find out how many characters appear in the string and then compare their corresponding counts.

#include <stdio.h>

int check_anagram(char [], char []);
int main()
{
   char a[100], b[100];
   int flag;

   printf("Enter first string\n");
   gets(a);

   printf("Enter second string\n");
   gets(b);

   flag = check_anagram(a, b);
   if (flag == 1)
      printf("\"%s\" and \"%s\" are anagrams.\n", a, b);
   else
      printf("\"%s\" and \"%s\" are not anagrams.\n", a, b);
   return 0;
}
int check_anagram(char a[], char b[])
{
   int first[26] = {0}, second[26] = {0};
   int c = 0;
   while (a[c] != '\0')
   {
      first[a[c]-'a']++;
      c++;
   }
   c = 0;
   while (b[c] != '\0')
   {
     second[b[c]-'a']++;
      c++;
   }
    for (c = 0; c < 26; c++)

  {
      if (first[c] != second[c])
         return 0;
  }

 return 1;
}
Read more ...

To Read a File in C

Friday 28 June 2013
C program to read a file: This program reads a file entered by the user, and displays the contents of the screen, a file structure again fopen function to open a file pointer. FILE is a predefined structure stdio.h. If the file has been opened fopen as a pointer back to the file and if it is not able to open a file so that it returns NULL. fgetc function read a character from the file and close the file fclose function. Opening a file means that we can bring run from the hard disk into memory the file. The operations file must exist in the directory where the executable of this code available basis.

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

int main()
{
   char ch;
   char file_name[25];
   FILE *fp;

   printf("Enter the name of file you wish to see\n");
   gets(file_name);

   fp = fopen(file_name,"r");                                    // read mode
   if( fp == NULL )
   {
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
   }
   printf("The contents of %s file are :\n", file_name);
   while( ( ch = fgetc(fp) ) != EOF )
      printf("%c",ch);
   fclose(fp);
   return 0;
}
Read more ...

To Copy Files in C

Friday 28 June 2013

 Write a Program Copy Files in C.

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

int main()
{
   char ch, source_file[20], target_file[20];
   FILE *source, *target;

   printf("Enter name of file to copy\n");
   gets(source_file);

   source = fopen(source_file, "r");

   if( source == NULL )
   {
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }
   printf("Enter name of target file\n");
   gets(target_file);

   target = fopen(target_file, "w");

   if( target == NULL )
   {
      fclose(source);
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }
   while( ( ch = fgetc(source) ) != EOF )
      fputc(ch, target);
   printf("File copied successfully.\n");
   fclose(source);
   fclose(target);
   return 0;
}
Read more ...

To Merge Two Files in C

Friday 28 June 2013
This C program adds two files and stores the contents of another file. The files are merged and are for reading the file that contains the contents of the two files, open and opened in a write mode. To merge two files first, to open a file and read character by character and content store to read it in another file, and then we read the content of a file and stores it in a file, you can read until EOF (two files the end of the file), is achieved.

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

int main()
{
   FILE *fs1, *fs2, *ft;
   char ch, file1[20], file2[20], file3[20];

   printf("Enter name of first file\n");
   gets(file1);

   printf("Enter name of second file\n");
   gets(file2);

   printf("Enter name of file which will store contents of two files\n");
   gets(file3);

   fs1 = fopen(file1,"r");
   fs2 = fopen(file2,"r");
   if( fs1 == NULL || fs2 == NULL )
   {
      perror("Error ");
      printf("Press any key to exit...\n");
      getch();
      exit(EXIT_FAILURE);
   }
   ft = fopen(file3,"w");
   if( ft == NULL )
   {
      perror("Error ");
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }
   while( ( ch = fgetc(fs1) ) != EOF )
   fputc(ch,ft);
   while( ( ch = fgetc(fs2) ) != EOF )

   fputc(ch,ft);
   printf("Two files were merged into %s file successfully.\n",file3);
   fclose(fs1);
   fclose(fs2);
   fclose(ft);
   return 0;
}
Read more ...

To list files in directory in C

Friday 28 June 2013
This program lists all the files in a library / folder where the executable file is present. For example, if the executable file is present in C: \ \ TC \ \ BIN then displays all the files in C: \ \ TC \ \ BIN.

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

int main()
{
      int done;
      struct ffblk a;

      printf("Press any key to view the files in the current directory\n");
      getch();

      done = findfirst("*.*",&a,0);

      while(!done)
     {
         printf("%s\n",a.ff_name);
         done = findnext(&a);
     }
     getch();
     return 0;
}
Read more ...

To Delete a File in C

Friday 28 June 2013
The C program to delete a file that is entered by the user, the file will be deleted, be present in the directory where the executable file of the program is available. The file extension must contain, remove the macro used to delete the file. If there is an error to delete the file, an error message will be displayed with perror function.

#include<stdio.h>

main()
{
   int status;
   char file_name[25];

   printf("Enter the name of file you wish to delete\n");
   gets(file_name);

   status = remove(file_name);

   if( status == 0 )
      printf("%s file deleted successfully.\n",file_name);
   else
   {
      printf("Unable to delete the file\n");
      perror("Error");
   }
  return 0;
}
Read more ...

To Print Date in C

Friday 28 June 2013
#include <stdio.h>
#include <conio.h>
#include <dos.h>

main()
{
   struct date d;
   getdate(&d);

   printf("Current system date is %d/%d/%d",d.da_day,d.da_mon,d.da_year);
   getch();

   return 0;
}
Read more ...

To add two complex numbers in C program

Friday 28 June 2013
#include <stdio.h>

struct complex
{
   int real, img;
};

int main()
{
   struct complex a, b, c;

   printf("Enter a and b where a + ib is the first complex number.\n");
   printf("a = ");
   scanf("%d", &a.real);

   printf("b = ");
   scanf("%d", &a.img);

   printf("Enter c and d where c + id is the second complex number.\n");
   printf("c = ");
   scanf("%d", &b.real);
   printf("d = ");
   scanf("%d", &b.img);

   c.real = a.real + b.real;
   c.img = a.img + b.img;

   if ( c.img >= 0 )
      printf("Sum of two complex numbers = %d + %di\n",c.real,c.img);
   else
      printf("Sum of two complex numbers = %d %di\n",c.real,c.img);
   return 0;
}
Read more ...

To generate random numbers in c program

Friday 28 June 2013
#include <stdio.h>
#include <stdlib.h>

int main()
{
  int c;
  int n;

  printf("Ten random numbers in [1,100]\n");
  for (c = 1; c <= 10; c++)
  {
    n = rand()%100 + 1;
    printf("%d\n", n);
  }
   return 0;
}

Read more ...