Skip to content

Instantly share code, notes, and snippets.

@sikanrong
Created August 2, 2018 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sikanrong/369996d9f6ccb8f0e50e72d83d03d961 to your computer and use it in GitHub Desktop.
Save sikanrong/369996d9f6ccb8f0e50e72d83d03d961 to your computer and use it in GitHub Desktop.
//https://www.geeksforgeeks.org/calculate-square-of-a-number-without-using-and-pow/
//Calculate a number's square without using /, *, or pow().
#include <stdio.h>
int main(){
int n;
printf("Please enter an integer: ");
scanf("%i", &n);
unsigned int x = n;
unsigned int r = 0;
unsigned int i = 0;
while(x){
if(x & 1){
r += (n << i);
}
x >>= 1;
i++;
}
printf("n^2: %i", r);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment