Loading

Archive for 27 Aug 2012

C Program for Selection Sort


C - Program to Implement Selection Sort


#include<stdio.h>

main()
{
   int array[100], 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;
}

Posted in | Leave a comment

C Program to Shudown Computer

C Program to Shudown Computer 

( works in Windows Xp)


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

main()
{
   char ch;

   printf("Do you want to shutdown your computer now (y/n)\n");
   scanf("%c",&ch);

   if (ch == 'y' || ch == 'Y')
      system("C:\\WINDOWS\\System32\\shutdown -s");

   return 0;
}

Posted in | Leave a comment

C Program to Check Anagram

C - Program to Check Anagram


#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}, 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;
}

Posted in | Leave a comment

C Program to Generate Random Numbers

C - Program to Generate Random Numbers


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

int main() {
  int c, 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;
}

Posted in | Leave a comment

C Program for Pascals Triangle

 C - Program for Pascals Triangle


#include<stdio.h>

long factorial(int);

main()
{
   int i, n, c;

   printf("Enter the number of rows you wish to see in pascal triangle\n");
   scanf("%d",&n);

   for ( i = 0 ; i < n ; i++ )
   {
      for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
         printf(" ");

      for( c = 0 ; c <= i ; c++ )
         printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));

      printf("\n");
   }

   return 0;
}

long factorial(int n)
{
   int c;
   long result = 1;

   for( c = 1 ; c <= n ; c++ )
         result = result*c;

   return ( result );
}

Posted in | Leave a comment

C Program for Odd/Even Number

 C Program to Check whether a Number is Odd/Even


#include<stdio.h>

main()
{
   int n;

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

   if ( n%2 == 0 )
      printf("\n Even");
   else
      printf("\n Odd");

   return 0;
}

Posted in | Leave a comment

C Program for Fibonacci Series



 C Program for Fibonacci Series


#include<stdio.h>

main()
{
   int n, first = 0, second = 1, next, c;
   printf("Enter the number of terms\n");
   scanf("%d",&n);
   printf("First %d terms of Fibonacci series are :-\n",n);

   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      printf("%d\n",next);
   }
   return 0;
}

Posted in | Leave a comment

C Program for Armstrong Number

 C - Program for Check if a Number is Armstrong Number



#include <stdio.h>

main()
{
   int number, sum = 0, temp, remainder;

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

   temp = number;

   while( temp != 0 )
   {
      remainder = temp%10;
      sum = sum + remainder*remainder*remainder;
      temp = temp/10;
   }

   if ( number == sum )
      printf("\n Entered number is an Armstrong number.");
   else
      printf("\n Entered number is not an Armstrong number.");        

   return 0;
}

Posted in | Leave a comment

C Program to Reverse a Number


 C Program to Reverse a Number



#include <stdio.h>

main()
{
   int n, reverse = 0;

   printf("Enter a number to reverse\n");
   scanf("%d",&n);

   while (n != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n = n/10;
   }

   printf("Reverse of entered number is = %d\n", reverse);

   return 0;
}

Posted in | Leave a comment

C Program to Display prime numbers

C - Program to Display Prime Numbers


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

void main()
{
   int n, i = 3, count, c;

   printf("Enter the number of prime numbers required\n");
   scanf("%d",&n);

   if ( n >= 1 )
   {
      printf("First %d prime numbers are :\n",n);
      printf("2\n");
   }

   for ( count = 2 ; count <= n ;  )
   {
      for ( c = 2 ; c <= i - 1 ; c++ )
      {
         if ( i%c == 0 )
            break;
      }
      if ( c == i )
      {
         printf("%d\n",i);
         count++;
      }
      i++;
   }   
  getch();     
  }

Posted in | Leave a comment

C Program for Counting Sort

C - Program for Counting Sort


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

void main()
{
   int a[20],b[20],c[1000],n,i,j,k=1000;
   clrscr();
   printf("\nHow many Numbers Do u want to sort :: ");
   scanf("%d",&n);

   printf("\nEnter the Numbers :: ");
   for(i=1;i<=n;i++)
   {
    scanf("%d",&a[i]);
   }
  
 /* counting sort starts here */
   for(i=0;i<k;i++)
    c[i]=0;
   for(j=1;j<=n;j++)
    c[a[j]]=c[a[j]]+1;
   for(i=1;i<k;i++)
    c[i]=c[i]+c[i-1];
   for(j=n;j>=1;j--)
   {
    b[c[a[j]]]=a[j];
    c[a[j]]=c[a[j]]-1;
   }

   printf("\nThe Sorted Numbers :: ");
   for(i=1;i<=n;i++)
    printf(" %d ",b[i]);
   getch();
}

Posted in | Leave a comment

C Program to Swap Two Numbers without Temporary Variable

C -Program to Swap Two Numbers without Temporary Variable



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

void main()
{
   int x,y;
   clrscr();
   printf("\n Enter Value of x :: ");
   scanf("%d",&x);
   printf("\n Enter Value of y :: ");
   scanf("%d",&y);
   printf("\n Before Swapping\n");
   printf("\n x :: %d\t\ty :: %d",x,y);
   x=x+y;
   y=x-y;
   x=x-y;
   printf("\n\n After Swapping\n");
   printf("\n x :: %d\t\ty :: %d",x,y);
   getch();
}

Posted in | Leave a comment

C Program to Implement Insertion Sort

 

 C - Program to Implement Insertion Sort

  
 
 
#include<stdio.h>
void main()
{
 int A[20], N, Temp, i, j;
 clrscr();
 printf("\n\n\t ENTER THE NUMBER OF TERMS...: ");
 scanf("%d", &N);
 printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:");
 for(i=0; i<N; i++)
 {
  gotoxy(25,11+i);
  scanf("\n\t\t%d", &A[i]);
 }
 for(i=1; i<N; i++)
 {
  Temp = A[i];
  j = i-1;
  while(Temp<A[j] && j>=0)
  {
   A[j+1] = A[j];
   j = j-1;
  }
  A[j+1] = Temp;
 }
 printf("\n\tTHE ASCENDING ORDER LIST IS...:\n");
 for(i=0; i<N; i++)
  printf("\n\t\t\t%d", A[i]);
 getch();
}

Posted in | Leave a comment