Comparison between numbers without relational operators

Saturday, 7 June 2014

This is tricky question which you can solve without need of relational operator. This type of question may be asked in interview not so important. Just apply the logic behind the questions correctly. You can do this question in your own way. Please let me know if u have other idea. Let see how I do it.



// find smallest or largest no without using comparison operators


#include <stdio.h>

int main()
{
   int a,b,c;
   printf("Enter value of A\n");
   scanf("%d",&a);
   printf("Enter value of B\n");
   scanf("%d",&b);
   printf("Enter value of C\n");
   scanf("%d",&c);


if(a-b>0)
{
    if(a-c>0)
   {
       printf("a is largest");

   }




else if(b-c>0)
{
    if(b-a>0)
   {
      printf("b is largest");
   }
}


else
{
    printf("c is largest");
}

}










Continue Reading | comments

how to remove duplicate elements in Array with one loop

This program will tell u how to remove duplicates elements from  an array. In this program two concepts only used. First check out duplicates elements and then shift one elements towards left of that element.




First of all main call redundantElement() method that will call sort for sorting the array in first line. Now sorted array we compare current element with next element. If we able to find out then we will shift all array elements left means a[j-1]=a[j] upto ith position. display() function is used to display the values of array.





//Program to delete duplicates items from an array






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


void sort(int *a,int n)
{
     for(int i=0;i<n;i++)
    {
        for(int j=0;j<n-i;j++)
       {
          if(a[j]>a[j+1])
         {
            int temp=a[j];
            a[j]=a[j+1];
            a[j+1]=temp;
         }
      }
   }
}



int redundantElement(int *a,int n)
{

sort(a,n);
   

for(int i=1;i<n;)
{

  if(a[i-1]==a[i])
  {
      for(int j=i;j<n;j++)
       {

           a[j-1]=a[j];
       }
       n--;
       continue; 
  }
  else
  i++;
 }

 return n;
 

}


void display(int a[],int n)
{  
      printf("\n");
     for(int i=0;i<n;i++)
     printf("%d  ",a[i]);
}



int main()
{
    int a[10],n;
   printf("Enter No of Element\n");
   scanf("%d",&n);


for(int i=0;i<n;i++)
{
     scanf("%d",&a[i]);
}
n=redundantElement(a,n);
display(a,n);
return 0;
}



Output://



 

Continue Reading | comments

Insertion sort code in C

Tuesday, 3 June 2014

Insertion sort program use backtracking for comparing current value with previous values and insert it to the desired place.  That's why it is called insertion sort. Its average complexity is O(n^2). Moreover it is slightly complex in understanding and coding. Yet very efficient sorting algorithm. Most likely to be asked in interview. Be prepare












// Insertion Sort Program


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



void insertionSort(int a[],int );
void display(int a[],int );



void insertionSort(int a[],int max)
{
 int j;
 for(int i=1;i<max;i++)
 {
 
  j=i-1;
  int temp=a[i];
  while((j>=0)&&(temp<a[j]))
  {
   a[j+1]=a[j];
   j--;
  }
  a[j+1]=temp;
 }
}



void display(int a[],int mx)
{
 for(int i=0;i<mx;i++)
 printf("%d ",a[i]);
}


void main()
{
int a[12],n;
printf("Enter Number of Elements\n");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
 printf("Enter %d Array Elements\n",i);
 scanf("%d",&a[i]);
}
insertionSort(a,n);
display(a,n);
getch();
}








Continue Reading | comments
 
My Other Websites : Ad Networks | Games Copyright © 2014. Study Depth - All Rights Reserved