Auto AdSense

Monday, 27 October 2014

C Program to print Floyd Triangle

#include <stdio.h>
  int main()
  {
     int n, i, c, a = 1;
     printf("Enter the number of rows of Floyd\'s triangle to print:\n");
     scanf("%d", &n);
     for (i = 1; i <= n; i++)
    {
        for (c = 1; c <= i; c++)
         {
            printf("%d ",a);
            a++;
        }
         printf("\n");
    }
   return 0;
  } 

C Program to Find the value of sin(x)

   #include <stdio.h>
   #include <conio.h>
   #include <math.h>
   #include <stdlib.h>
   void main()
   {
     int n, x1;
     float acc, term, den, x, sinx=0, sinval;
     clrscr();
     printf("Enter the value of x (in degrees)\n");
     scanf("%f",&x);
     x1 = x;
     /* Converting degrees to radians*/
     x = x*(3.142/180.0);
     sinval = sin(x);
     printf("Enter the accuary for the result\n");
     scanf("%f", &acc);
     term = x;
     sinx = term;
     n = 1;
     do
     {
       den = 2*n*(2*n+1);
       term = -term * x * x / den;
       sinx = sinx + term;
       n = n + 1;
     } while(acc <= fabs(sinval - sinx));
     printf("Sum of the sine series = %f\n", sinx);
     printf("Using Library function sin(%d) = %f\n", x1,sin(x));
   } 

C Program to Find the value of cos(x)

  

   #include <stdio.h>
   #include <conio.h>
   #include <math.h>
   #include <stdlib.h>
   void main()
   {
     int n, x1;
     float acc, term, den, x, cosx=0, cosval;
     clrscr();
     printf("Enter the value of x (in degrees)\n");
     scanf("%f",&x);
     x1 = x;
     /* Converting degrees to radians*/
     x = x*(3.142/180.0);
     cosval = cos(x);
     printf("Enter the accuary for the result\n");
     scanf("%f", &acc);
     term = 1;
     cosx = term;
     n = 1;
     do
     {
       den = 2*n*(2*n-1);
       term = -term * x * x / den;
       cosx = cosx + term;
       n = n + 1;
       } while(acc <= fabs(cosval - cosx));
     printf("Sum of the cosine series = %f\n", cosx);
     printf("Using Library function cos(%d) = %f\n", x1,cos(x));
   } 

C Program to Find The Roots Of A Quadratic Equation



   #include <stdio.h> 
   #include <conio.h> 
   #include <math.h> 
   void main() 
   { 
     float a, b, c, d, realp, imgp, r1, r2; 
     clrscr(); 
     printf(" Enter the 3 numbers\n "); 
     scanf(" %f %f %f " ,&a, &b, &c); 
     if ( a == 0 || b == 0 || c == 0 ) 
     { 
       printf(" Error input only non zero numbers\n "); 
     } 
     else 
     { 
       d = b * b - 4 * a * c; 
       if ( d == 0 ) 
       { 
         printf(" Roots are equal\n "); 
         r1 = r2 = - b / ( 2 * a ); 
         printf(" Root1 = %f, Root2 = %f ", r1, r2 ); 
       } 
       else if(d>0) 
       { 
         printf( "Roots are real & distinct\n" ); 
         r1 = ( - b + sqrt ( fabs ( d ) ) ) / ( 2 * a ); 
         r2 = ( - b - sqrt ( fabs ( d ) ) ) / ( 2 * a ); 
         printf(" Root1 = %f, Root2 = %f", r1, r2); 
       } 
       else 
       { 
         printf(" Roots are imaginary\n "); 
         realp = - b / ( 2 * a ); 
         imgp = sqrt ( fabs ( d ) ) / ( 2 * a ); 
         printf(" Root1 = %f + i%f, Root2 = %f - i%f ",realp, imgp, realp, imgp); 
       } 
     } 
     getch(); 
   } 

C Files Program - File operations

    #include <stdio.h>
   void main()
   {
     FILE *fptr;
     char name[20];
     int age;
     float salary;
     fptr = fopen ("emp.txt", "w"); /*open for writing*/
     if (fptr == NULL)
     {
       printf("File does not exists \n");
       return;
     }
     printf("Enter the name\n");
     scanf("%s", name);
     fprintf(fptr, "Name = %s\n", name);
     printf("Enter the age\n");
     scanf("%d", &age);
     fprintf(fptr, "Age = %d\n", age);
     printf("Enter the salary\n");
     scanf("%f", &salary);
     fprintf(fptr, "Salary = %.2f\n", salary);
     fclose(fptr);
   } 

C Files Program - How the data stored on the disk is read

   #include <stdio.h>
   #include <stdlib.h>
   void main()
   {
     FILE *fptr;
     char filename[15];
     char ch;
     printf("Enter the filename to be opened\n");
     gets(filename);
     fptr = fopen (filename, "r"); /*open for reading*/
     if (fptr == NULL)
     {
       printf("Cannot open file\n");
       exit(0);
     }
     ch = fgetc(fptr);
     while (ch != EOF)
     {
       printf ("%c", ch);
       ch = fgetc(fptr);
     }
     fclose(fptr);
   } 

C Files Program - Copy one file contents to another



   #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();
   }   

C Program to find the Factorial of a number

 #include <stdio.h>
  #include <conio.h>
  long int factorial(int n);
  void main()
  {
     int n;
     clrscr();
     printf("Enter the number:\n");
     scanf("%d",&n);
     printf("Factorial of %d is %ld",n,factorial(n));
     getch();
  }
  long int factorial(int n)
  {
     if(n<=1)
     {
        return(01);
     }
     else
     {
        n=n*factorial(n-1);
        return(n);
     }
  }      

C Program to Count the Digit in a Number

  #include<stdio.h>
   int main()
   {
     int num,count=0;
     printf("Enter a number: ");
     scanf("%d",&num);
     while(num)
     {
       num=num/10;
       count++;
     }
     printf("Total digits is:%d",count);
     return 0;
   } 

C Program of Counting Frequencies Of Elements Of Array


   #include <stdio.h>
   #include <conio.h>
   #include <stdlib.h>
   #define S 6
   main()
   {
     int a[S], freq[S];
     int i, j, k,n = S;
     clrscr();
     for(i = 0; i < S; i++)
     {
       printf(" \n Enter a[%d] element: ", i);
       scanf(" %d ", &a[i]);
       freq[i] = 1;
     }
     printf(" Original Array\n ");
     for(i = 0; i < S; i++)
     printf(" %d   ", a[i]);
     /* Main Logic Starts Here */
     for(i = 0; i < n; i++)
     for(j = i + 1; j < n; j++)
     {
       if(a[i] == a[j])
       {
         for(k = j; k < n; k++)
         a[k] = a[k+1];
         freq[i]++;
         n--;
       }
     }
     printf(" \nArray with freq\n ");
     printf(" \nElement  Freq\n ");
     for(i = 0; i < n; i++)
     printf("%d  %d\n ", a[i], freq[i]);
     getch();
   } 

Saturday, 25 October 2014

C Program to copy a string

  #include<stdio.h>
  void stringCopy(char[],char[]);
  int main()
  {
      char str1[100],str2[100];
      printf("Enter any string: ");
      scanf("%s",str1);
      stringCopy(str1,str2);
      printf("After copying: %s",str2);
      return 0;
  }
  void stringCopy(char str1[],char str2[])
  {
      int i=0;
      while(str1[i]!=\'\u0000\')
      {
          str2[i] = str1[i];
          i++;
      }
      str2[i]=\'\u0000\';
  } 

C Program to convert Binary to Decimal,Octal,Hexadecimal



   #include<stdio.h>
   #include<string.h>
   void hexadecimal();
   void main()
   {
     int num, bnum, dec = 0, base = 1, rem ,dec1=0,oct[25],dec2=0,flag=0,i=0,counter=0,j;
     printf("Enter the binary number(1s and 0s)\n");
     scanf("%d", &num);
     bnum = num;
     while( num > 0)
     {
       rem = num % 10;
       if((rem==0) || (rem==1))
       {
         dec = dec + rem * base;
         num = num / 10 ;
         base = base * 2;
         flag=1;
       }
       else
       {
         flag=0;
         printf("\n Enter binary number \n");
         break;
       }
     }
     if(flag==1)
     {
       printf("The Binary number is = %d\n", bnum);
       printf("Its decimal equivalent is =%d\n", dec);
       dec1=dec;
       dec2=dec1;
       while(dec>0)
       {
         rem=dec%8;
         oct[i]=rem;
         dec=dec/8;
         i++;
         counter++;
       }
       counter--;
       printf("\n Its octal equivalent is:");
       while(counter>=0)
       {
         printf("%d" ,oct[counter]);
         counter--;
       }
       printf("\nIts Hexa Decimal equivalant is: ");
       hexadecimal(dec2);
     }
   }
   void hexadecimal(long n)
   {
     long i;
     if(n>0)
     {
       i=n%16;
       n=n/16;
       hexadecimal(n);
       if(i>=10)
       {
         switch(i)
         {
           case 10:
             printf("A");
             break;
           case 11:
             printf("B");
             break;
           case 12:
             printf("C");
             break;
           case 13:
             printf("D");
             break;
           case 14:
             printf("E");
             break;
           case 15:
             printf("F");
             break;
         }
       }
       else
         printf("%ld",i);
     }
   }   

C Program to find Combinations and Permutations



  #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( "%d C %d is %d \n", n,r,ncr( n , r));
          printf("%d P %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));
  }    

C Program to find whether the entered alphabet a vowel or not

   #include <stdio.h>
  main()
  {
      char ch;
      printf("Enter a character\n");
      scanf("%c", &ch);
      if (ch == \'a\' || ch == \'A\' || ch == \'e\' || ch == \'E\' || ch == \'i\' || ch == \'I\' || ch ==\'o\' || ch==\'O\' || ch == \'u\' || ch == \'U\')
          printf("%c is a vowel.\n", ch);
      else
          printf("%c is not a vowel.\n", ch);
      return 0;
  } 

C Program of converting Celsius to Fahrenheit


  #include <stdio.h>
  #include <conio.h>
  void main()
  {
      float c, f;
      clrscr();
      printf(" Enter temp in centigrade: ");
      scanf("%f",&c);
      f = ( 1.8 * c ) + 32;
      printf(" Temperature in Fahrenheit = %f", f);
      getch();
  } 

Friday, 24 October 2014

C Program for Bucket Sort


  #include<stdio.h>
  void Bucket_Sort(int array[], int n)
  {
      int i, j;
      int count[n];
      for(i=0; i < n; i++)
      {
          count[i] = 0;
      }
      for(i=0; i < n; i++)
      {
          (count[array[i]])++;
      }
      for(i=0,j=0; i < n; i++)
      {
          for(; count[i]>0;(count[i])--)
          {
              array[j++] = i;
          }
      }
  }
  int main()
  {
      int array[100];
      int num;
      int i;
      printf("Enter How many Numbers : ");
      scanf("%d",&num);
      printf("Enter the %d elements to be sorted:\n",num);
      for(i = 0; i < num; i++ )
      {
          scanf("%d",&array[i]);
      }
      printf("\n The array of elements before sorting : \n");
      for (i = 0;i < num;i++)
      {
          printf("%d ", array[i]);
      }
      printf("\n The array of elements after sorting : \n");
      Bucket_Sort(array, num);
      for (i = 0;i < n;i++)
      {
          printf("%d ", array[i]);
      }
      printf("\n");
      return 0;
  }      

C Program for Bubble Sort


#include <stdio.h> 
 void bubble_sort(long [], long);
 int main()
 {
      long array[100], n, c, d, swap;
      printf("Enter number of elements:");
      scanf("%ld", &n);
      printf("Enter %ld longegers\n", n);
      for (c = 0; c < n; c++)
      scanf("%ld", &array[c]);
      bubble_sort(array, n);
      printf("Sorted list in ascending order:n");
      for ( c = 0 ; c < n ; c++ )
      printf("%ld\n", array[c]);
      return 0;
 }
 void bubble_sort(long list[], long n)
 {
      long c, d, t;
      for (c = 0 ; c < ( n - 1 ); c++)
       {
           for (d = 0 ; d < n - c - 1; d++)
            {
                if (list[d] > list[d+1])
                 {
                      t = list[d];
                      list[d] = list[d+1];
                      list[d+1]= t;
                }
           }
      }
 }  

C Program for Binary Search


 int BinarySearch(int *array, int number_of_elements, int key)
 {
      int low = 0, high = number_of_elements-1, mid;
      while(low <= high)
      {
          mid = (low + high)/2;
          if(array[mid] < key)
          {
                  low = mid + 1;
          }
          else if(array[mid] == key)
          {
                  return mid;
          }
          else if(array[mid] > key)
          {
                  high = mid-1;
          }
      }
      return -1;
 }
 int main()
 {
      int number_of_elements;
      scanf("%d",&number_of_elements);
      int array[number_of_elements];
      int iter;
      for(iter = 1;iter < number_of_elements;iter++)
      {
          if(array[iter]< array[iter - 1])
          {
                 printf("Given input is \n not sorted\n");
 return 0;
          }
      }
      int key;
      scanf("%d",&key);
/* Calling this functions searches for the key and returns its index. It returns -1 if key is not found.*/
      int index;
      index = BinarySearch(array,number_of_elements,key);
     if(index==-1)
     {
              printf("Element not found\n");
     }
     else
     {
             printf("Element is at index %d\n ",index);
     }
     return 0;
 }  

C Program to add without ADD operater

   #include<stdio.h>
  int main()
  {
      int a,b;
      int sum;
      printf("Enter any two integers: ");
      scanf("%d%d",&a,&b);
      //sum = a - (-b);
      sum = a - ~b -1;
      printf("Sum of two integers:%d",sum);
      return 0;
  } 

C Program to add Complex Numbers

#include <stdio.h>
  #include <conio.h>
  #include <stdlib.h>
  struct complex
  {
      int real;
      int img;
  };
  int main()
  {
      struct complex a, b, c;
      printf(" Enter a and b where a + ib is the first complex number. ");
      printf("\n a = ");
      scanf(" %d ", &a.real);
      printf(" b = ");
      scanf(" %d ", &a.img);
      printf(" Enter c and d where c + id is the second complex number. ");
      printf("\n 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 ", c.real, c.img);
      else
          printf(" Sum of two complex numbers = %d %di ", c.real, c.img);
      getch();
      return 0;
  }