Wednesday 23 March 2016

WAP to implement Selection Sort.

//WAP to implement Selection Sort.

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

void main()
      {
        int array[10], i, j, N, temp;

        int findmax(int b[10], int k);  // function prototype (declaration)
        void exchang(int b[10], int k); // function prototype (declaration)

        clrscr(); // clearing the console screen

        printf("Enter the size of array\n");
        scanf("%d",&N);  // inputting array size

        printf("Enter the elements one by one\n");
        for(i=0; i<N ; i++)
              {
                scanf("%d",&array[i]);  // inputting array elements
               }

        exchang(array, N);  // function calling
/* Selection sorting begins */

        printf("Sorted array is...\n");
        for(i=0; i< N ; i++)
             {
               printf("%d\n",array[i]);
              }

      } // End of main()


int findmax(int b[10], int k)/* function to find the maximum array element*/
      {
        int max=0,j;
        for(j = 1; j <= k; j++)
             {
               if ( b[j] > b[max])
                {
                  max = j;
                 }
              }
        return(max);
      }


void exchang(int b[10],int k)  // function to sort and swap elements of array
      {
        int temp, big, j;
        for ( j=k-1; j>=1; j--)
             {
               big = findmax(b,j);
               temp = b[big];
               b[big] = b[j];
               b[j] = temp;
              }
        return;
      }

WAP to implement Array Search.

//WAP to implement Array Search
#include <stdio.h>
#include <conio.h>

void main()
      {
        int array[10], i, N, keynum, found=0;
        clrscr();

        printf("Enter the size of array\n");
        scanf("%d",&N); // inputting array size

        printf("Enter the elements one by one\n");
              for(i=0; i<N ; i++)
               {
                  scanf("%d",&array[i]); // inputting array elements
               }

        printf("Enter the element to be searched\n");
        scanf("%d", &keynum); // element to be searched 'keynum'

              for ( i=0; i < N ; i++)
               {
                 if( keynum == array[i] ) // logic for searching
                  {
                    found = 1;
                    break;
                   }
                }
        
        if ( found == 1)
             printf("SUCCESSFUL SEARCH\n"); // search successful
        else
             printf("Search is FAILED\n"); // otherwise

        getch(); // for holding the console screen
      }

Monday 21 March 2016

WAP in C to Concatenate strings using standard library functions.


//WAP in C to Concatenate strings using standard library functions

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

void main()
{
      int len1, len2, i;
      char string1[50],string2[50];

      clrscr();

       printf("Enter the first String\n");
       gets(string1);
       printf("\nEnter the second String\n");
       gets(string2);

       len1=strlen(string1); //calculating length of string1
       len2=strlen(string2); //calculating length of string2

       printf("\n\nString after concatenation is:\n ");

//code for string concatenation

       for(i=0;i<=len2;i++)
       {
            string1[len1+i]=string2[i]; //concatenating string1 and string2
       }

//OR
        string1=strcat(string1,string2); // built-in library function

        printf("%s",string1); //printing the concatenated string

      getch(); // for holding the console screen
}

Saturday 19 March 2016

WAP in C to convert a string from Upper case to Lower case using library functions

//WAP in C to convert a string from Upper case to Lower case

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

void main()
{
      char string[100];
      int count=0, ch, i;

      clrscr(); // to clear the console screen

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

      for(i=0;string[i]!='\0';i++)
      {
            count++;  //count the number of characters in the string
       }

      printf("The entered string is : %s",string); //printing the entered string

       printf("\nLowerCase string is: ");
     
      for(i=0; i < count; i++)
      {
            ch = tolower(string[i]); //changing upper case character to lower case
            printf("%c",ch);
       }

      getch(); // for holding the console screen
}

WAP in C to convert a string from Lower case to Upper case using standard library functions


//WAP in C to convert a string from Lower case to Upper case

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

void main()
{

       char string[50];
       int count=0, ch, i;

      clrscr(); // for clearing the console screen

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

      for(i=0;string[i]!='\0';i++)
      {
         count++;  //count the number of characters in the string or use strlen()
       }

      printf("The entered string is : %s",string); //printing the entered string

      printf("\nUpperCase string is: ");

// logic for converting the string in uppercase 
      for(i=0; i < count; i++)
      {
          ch = toupper(string[i]); //changing lower case character to upper case
          printf("%c",ch);
      }

       getch(); // holding the console screen
} // End of main()

Thursday 17 March 2016

Program to find roots of a quadratic equation (Ax^2 +Bx +C)

//Program to find roots of a quadratic equation (Ax^2 +Bx +C)
#include<stdio.h>
#include<math.h>
#include<conio.h>

main()
{
      int a, b, c;
      float d, root1, root2;
      clrscr(); // for clearing the console screen

      for(;;)
      {
        printf("\nEnter the non zero co-efficients A, B and C (Ax^2 +Bx +C): \n");
        scanf("%d%d%d",&a,&b,&c);

        if((a==0) || (b==0) || (c==0)) // case 1 : if no such equation exists
        {
          printf("\nPlease enter non zero co-efficients \n");
        }
        else // case 2 : if they exists
        {
          d=((b*b)-(4*a*c)); // calculating discriminant

          if(d>0)  // if the roots are real
          {
             printf("Roots are real! \n");
             root1=(-b-(sqrt(d)))/(2.0*a);
             root2=(-b+(sqrt(d)))/(2.0*a);

             printf("Roots are: %f and %f \n", root1, root2);

           }

           else if(d<0) // if the roots are imaginary
           {
             printf("Roots are Imaginary:\n");
             root1=-b/(2.0*a);

             printf("First root: %lf +%fi \n", root1, sqrt(-d)/(2.0*a));
             printf("Second root:%lf -%lfi\n", root1, sqrt(-d)/(2.0*a));
           }

           else // if the roots are equal
           {
             printf("Roots are equal!\n");
             root1= -b/(2.0*a);
             printf("Equal; roots are:%f and %f \n", root1,root1);
           }
        }
      }
      getch(); // for holding the console screen
      return 0;

}

WAP to implement Linear Search.

//WAP to implement Linear Search.
#include <stdio.h>
#include <conio.h>

void main()
      {
        int array[10], i, N, keynum, found=0; // required variables
        clrscr(); // clearing the console screen

        printf("Enter the size of array\n");
        scanf("%d",&N); //inputting array size
       
        printf("Enter the elements one by one\n");
              for(i=0; i<N ; i++)
               {
                scanf("%d",&array[i]); // inputting array elements
               }

        printf("Enter the element to be searched\n");
        scanf("%d", &keynum); // element to be searched

//Searching the element inside the array

              for ( i=0; i < N ; i++)
               {
                if( keynum == array[i] )
                 {
                   found = 1; // in case the element is found
                   break;
                  }
                }
                
                if ( found == 1)
                   printf("SUCCESSFUL SEARCH\n");
                else
                   printf("Search is FAILED\n");

      getch(); // for holding the console screen
      }

WAP to implement Binary Search

//WAP in C to take an array as an input and search a element using Binary Search Algorithm
#include<stdio.h>
 int main()
      {
        int num, arr[30], item, i, j, mid, top, bottom; // required variables

        printf("Enter how many elements you want:\n");
        scanf("%d", &num); // input array size
     
        printf("Enter the %d elements in ascending order\n", num);
              for (i = 0; i < num; i++)
               {
                 scanf("%d", &arr[i]); // input array elements in "ascending order"
               }

        printf("\nEnter the item to search\n");
        scanf("%d", &item); // element to be searched
       
       bottom = 1;
       top = num;

/*initializing the value of mid, top and bottom according to the keyord to be searched */
              do
               {
                 mid = (bottom + top) / 2;
                 if (item < arr[mid])
                     top = mid - 1;
                 else if (item > arr[mid])
                      bottom = mid + 1;
               }

/*Searching the array element in the required half of the array*/

              while (item != arr[mid] && bottom <= top);
                 if (item == arr[mid]) // element must be found at middle position after each iteration
                   {
                     printf("Binary search successfull!!\n");
                     printf("\n %d found in position: %d\n", item, mid + 1);
                    }
                 else
                   {
                    printf("\n Search failed\n %d not found\n", item);
                   }
   return 0;
   getch(); // to hold the console screen
  }

Program to compare two strings.

//WAP in C to compare two strings
#include<stdio.h>
#include<conio.h>

void main()
{

      char string1[50],string2[50]; //used to store string
      int count1=0, count2=0, i;
      clrscr();

      printf("Enter a string: ");
      gets(string1); //input string 1

     printf("Enter another string:");
     gets(string2); // input string 2

/*Count the number of characters in string1*/
      while (string1[count1]!='\0')
                count1++;
/*Count the number of characters in string2*/

      while (string2[count2]!='\0')
                 count2++;
      i=0;

//Comparing the length of the string


      if(count1>count2)
      { 
             printf("String1 is greater than string2\n");
      } 
      else if(count1<count2)
      { 
             printf("String1 is less than string2\n");
      }

/*If the length of the strings are equal ,the string comparison starts with the first character in each string and continues with subsequent characters until the corresponding characters differ or until the end of the strings is reached.*/

      else if(count1 == count2) 
      { 
             while ( (i < count1) && (i < count2)) 
             { 
                    if (string1[i] == string2[i]) 
                    { 
                        i++; 
                        continue; 
                    }
                    else
                        printf("Strings are not equal");
              } 
        }
       getch(); // to hold the console screen
}

WAP to implement Bubble Sort

//WAP in C to take an array as an input and sort it using Bubble Sort Algorithm
#include<stdio.h>
#include<conio.h>

int main()
{
      int arr[50], num, i, j, temp = 0; // required variables
      clrscr(); // clearing the console screen 

      printf("Enter how many numbers you want: "); 
      scanf("%d", &num); // taking array length
      printf("\nEnter the %d elements:\n", num); 

     for (i = 0; i < num; i++)
     { 
       scanf("%d", &arr[i]); // taking array input 
     }

// sorting the array using temporary variable

     for (i = 0; i < num; i++)      
     { 
       for (j = i + 1; j < num; j++)
       { 
         if (arr[i] > arr[j])
         {
           temp = arr[i]; 
           arr[i] = arr[j]; 
           arr[j] = temp;
         }
       } 
     } 

// printing the sorted array
      printf("\n\n\n\t\tThe sorted array using Bubble sort is:\n"); 
      for (i = 0; i < num; i++) 
      { 
        printf("\n\t\t%d", arr[i]); 
      }
return 0;  
getch(); // for holding the console screen
}

WAP in C to calculate/extract years, weeks and remaining days

#include <stdio.h>
#include<conio.h>
#define daysinaweek 7

void main()
{
      int no_of_days, days, weeks, years; // required variables
      clrscr(); //clearing the console screen

      printf("Enter the number of days: \n");
      scanf("%d" , &no_of_days); //taking input in form of days

      years = no_of_days / 365; //extracting years
      weeks = (no_of_days % 365) / daysinaweek ; // extracting weeks
      days = (no_of_days % 365) %  daysinaweek ; // remaining days

//Output required

      printf ("%d is equivalent to %d years, %d weeks and %d days\n", no_of_days, years, weeks, days);
}