Monday, July 11, 2011

Program to find pair of elements in array that produce 25 as a result of addition operation.

#include<stdio.h>

#include<conio.h>
main()
{
      int *a,*b,f,i,j,k=0,n,p,q;
      printf("Enter number of elements you want in your array:");
      scanf("%d",&n);
      a=(int *)malloc(n*sizeof(int));
      b=(int *)malloc(n*sizeof(int));
      printf("\nEnter the elements in array:\n");
      for(i=0;i<n;i++)
      {
         scanf("%d",&a[i]);
         if(a[i]<=25)
         {
          b[k]=a[i];
          k++;
         }
      }
      b=(int*)realloc(b,k*sizeof(int));
      for(i=0;i<k;i++)
      {
       f=0;
       p=b[i];
       if(b[i]==-1000)
        continue;
       for(j=i+1;j<k;j++)
       {
         if(b[i]==b[j])
         {
          b[j]=-1000;
          continue;
         }
         if((b[i]+b[j])==25)
         {
           f=1;
           q=b[j];
           b[j]=-1000;
         }
       }
       if(f==1)
       printf("\npair found %d and %d produce 25 as result of the addition",p,q);
      }
      getch();
      return 0;
}

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More