Pages

Wednesday, July 10, 2024

Multiply a given number by 3.5 without using the multiplication operator (*)

#include<stdio.h>
int main()
{
    int num;
    int result;
    printf("Enter the number: ");
    scanf("%d",&num);

    result = ((num << 2) - (num >> 1));
    printf("\nResult = %d",result);
    return 0;
}


Explanation

Lets take the input as 10

10 = 0x1010 in hex

So,

                            0x1010 << 2 = 0x101000 = 40

                            0x1010 >>1 = 0x0101  = 5

hence 

                            result = 40 - 5 =35 = 10*3.5

You can try with other numbers as well.

No comments:

Post a Comment

What is anti- Debugging

  Anti- debugging is how we can stop somebody from debugging our system and getting all the information out of your subsystem.