Wednesday, December 11, 2013

numberswhosedifferenceisminandproductisgivensum

#include <stdio.h>
#include <math.h>
int main(int argc,char* argv[])
{
 int big=0,small=0;
 int givenSum=0,product=0;
 printf("Enter a number :");
 scanf("%d",&givenSum);
 big = ceil(sqrt(givenSum));
 small = big -1;
 printf("big %d\n",big);
 product = big*big;
 if((product-givenSum) <= 2 && (product-givenSum)>=0)
 {
  printf("numbers are %d %d\n",big,big);
  return 0;
 }

 while(1)
 {
  product = big * small;
  if( ((product-givenSum) <= 2) && ((product-givenSum)>=0))
  {
  printf("numbers are %d %d\n",big,small);
  break;
  }
  if(product > givenSum)
   small--;
  else
     big++;
 }
 return 0;
}

No comments:

Post a Comment