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

Saturday, 26 December 2015

Program to Calculate the Sum of Digits of a number

//Program to Calculate the Sum of Digits of a number

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

void main()
{
      int  num, sum=0, temp; /* 'sum' is  used to store the sum of digits of a number and 'temp' to store the number for calculations*/
      int rem=0; // used to store the remainder of the number

      clrscr(); // for clearing the console screen

      printf("Enter the number : ");
      scanf("%d",&num);
      temp = num; //copies the value of 'number' in 'temp'

// logic for calculating the sum of digits of number

      while(temp != 0) // checking whether 'temp' is equal to 0 or not
      {
          rem=temp%10; // extracting the last digit by dividing it with 10
     sum=sum+rem; /* calculating the sum of digits of the number - sum is initialized with zero so that no garbage value is added */
     temp=temp/10; // dividing the number by 10 to break it in parts.
      } // end of while loop

      printf("The sum of the digits of a number %d = %d",number,sum); /* printing the sum of digits of number */

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