Skip to main content

Posts

Showing posts from January, 2015

C program to accept number and display it in words.

C program to accept number and display it in words. #include <stdio.h> #include <string.h> #include <conio.h>   void main(){      char init[ 27 ][ 12 ] = {" one "," two "," three ",                 " four "," five "," six ",                 " seven "," eight "," nine ",                 " ten "," eleven "," twelve ",                 " thirteen "," fourteen "," fifteen ",                 " sixteen "," seventeen "," eighteen ",                 " nineteen "," twenty "," thirty ",    ...

Bubble sort

  Bubble sort   Bubble sort , sometimes referred to as sinking sort , is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort , is named for the way smaller elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort . It can be practical if the input is usually in sort order but may occasionally have some out-of-order elements nearly in position. Bubble sort has worst-case and average complexity both О ( n 2 ), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of O ( n  log  n ). Even other О ( n 2 ) ...

Program to find factorial of a number using recursive function.

Program to find factorial of a number using recursive function. #include <stdio.h>   int factorial(int n); int main() {     int n,f;     printf( "Enter an positive integer: " );     scanf( "%d" ,&n);     f=factorial(n);     printf( "Factorial of %d = %d" , n,f );     return 0 ; } int factorial(int n) {     if (n!= 1 )      return (n*factorial(n-1)) ; } Output:  

Program to search the element in an array of N element using Linear search method.

Program to search the element in an array  of N element  using Linear search method. #include <stdio.h> int main() {    int array[ 100 ], search, c, n;    printf( "Enter the number of elements in array\n" );    scanf( "%d" ,&n);    printf( "Enter %d integer(s)\n" , n);     for (c = 0 ; c < n; c++)       scanf( "%d" , &array[c]);    printf( "Enter the number to search\n" );    scanf("%d", &search);    for (c = 0 ; c < n; c++)    {       if (array[c] == search)          {          printf( "%d is present at location %d.\n" , search, c+ 1 );          break;       }    }    if (c == n)       printf( "%d is not present in a...

Program to find if a given number is prime or not.

C Program to find if a given number is prime or not.   #include <stdio.h> int main() {   int n, i, check= 0 ;   printf( "Enter a positive integer: " );   scanf( "%d" ,&n);   for (i= 2 ;i<=n/ 2 ;++i)   {        if (n%i== 0 )       {           check= 1 ;           break;       }   }   if (check== 0 )       printf("%d is a prime number.",n);   else       printf("%d is not a prime number.",n);   getch();   return 0 ; } Output:

Program to insert a element in one dimensional array at a given position.

Program to insert a element in one dimensional array at a given position.      #include <stdio.h> int main ()  {    int arr [ 30 ] , element, num, i, location;    printf ( "\nEnter no of elements :" ) ;    scanf ( "%d" , & num ) ;    printf ( "\nEnter the %d elements:" ,num ) ;    for ( i = 0 ; i < num; i ++)       scanf("%d", & arr [ i ] );    printf ( "\nEnter the element to be inserted :" ) ;    scanf ( "%d" , & element ) ;    printf ( "\nEnter the location" ) ;    scanf ( "%d" , & location ) ;    //Create space at the specified location    for ( i = num; i > = location; i-- )       arr [ i ] = arr [ i - 1 ] ;    num ++ ;    arr [ location - 1 ] = element;    //Print out the result of insertion    for ( i = 0 ; i < ...

Program to reverse the array.

Program to reverse the array. #include <stdio.h> void main() {    int n, c, a[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] );       pdrintf( "Reversed array is \n" );       for (c=n;c>=0;c--)    printf( "%5d" , a[c] );    getch(); } Output:

Program to reverse the digit

Program to reverse the digit. #include <stdio.h> int 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 ; }

Program to find sum of digit of any entered number

Program to find sum of digit of any entered number. #include <stdio.h> int main() {    int n, t, sum = 0 , remainder;    printf( "Enter an integer\n" );    scanf( "%d" , &n);    t = n;    while (t != 0 )    {       remainder = t % 10 ;       sum       = sum + remainder;       t         = t / 10 ;    }    printf( "Sum of digits of %d = %d\n" , n, sum);    return 0 ; }