RANDOMIZED ALGORITHMS 7

QUESTION

Given a integer x, write a function that multiplies x with 3.5 and returns the integer result. You are not allowed to use %, /, *.\n\nExamples:\nInput: 2\nOutput: 7\n\nInput: 5\nOutput: 17 (Ignore the digits after decimal point)\n\nSolution:\n1. We can get x*3.5 by adding 2*x, x and x/2. To calculate 2*x, left shift x by 1 and to calculate x/2, right shift x by 2.

ANSWER

#include <stdio.h>
 
int multiplyWith3Point5(int x)
{
  return (x<<1) + x + (x>>1);
}    
 
/* Driver program to test above functions*/
int main()
{
  int x; 
  scanf("%d",&x);
  printf("%d", multiplyWith3Point5(x));
  getchar();
  return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO