recent

Given an array a[] and a number b, check for pair in a[] with sum as b using Function

Using Function:-Given an array a[] and a number b, check for pair in a[] with sum as b
Write a C program that, given an array a[] of n numbers and another number b, determines whether or not there exist two elements in a[] whose sum is exactly b

#include<stdio.h>

void sum(int a[],int n,int b)

{
 int i,j,sum;
   for(i=0;i<n-1;i++)
     {
      for(j=i+1;j<n;j++)
       {
         sum=a[i]+a[j];
         if(sum==b)
         printf("Sum of Pair a[%d] & a[%d] equal to b=%d\n",i,j,b);
       }
   
     }
}

main()
{

 int a[20],i,n,b;

  printf("Enter Number b:\n");
  scanf("%d",&b);

  printf("Enter Number Of Element in Array:\n");
  scanf("%d",&n);

  printf("Read Array Element From User:");

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

  sum(a,n,b);
 
}
Powered by Blogger.