## Apple Interview Question

Software Engineer / Developers**Country:**United States

**Interview Type:**In-Person

Sum of Odd numbers

1*1 = 1 // 1

2*2 = 4 // 1+3

3*3 = 9 // 1+3+5

4*4 =16// 1+3+5+7

and so on

public class App {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number to find its square: ");

int num = scanner.nextInt();

scanner.close();

int squaredVal = 0;

for(int i=0; i<Math.abs(num); i++){

squaredVal = squaredVal + num;

}

System.out.println("Squared value is " + Math.abs(squaredVal));

}

}

```
public class App {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number to find its square: ");
int num = scanner.nextInt();
scanner.close();
int squaredVal = 0;
for(int i=0; i<Math.abs(num); i++){
squaredVal = squaredVal + num;
}
System.out.println("Squared value is " + Math.abs(squaredVal));
}
}
```

```
and
public class App {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number to find its square: ");
int num = scanner.nextInt();
scanner.close();
int squaredVal = 0;
for(int i=0; i<Math.abs(num); i++){
squaredVal = squaredVal + num;
}
System.out.println("Squared value is " + Math.abs(squaredVal));
}
}
```

```
public class App {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number to find its square: ");
int num = scanner.nextInt();
scanner.close();
int squaredVal = 0;
for(int i=0; i<Math.abs(num); i++){
squaredVal = squaredVal + num;
}
System.out.println("Squared value is " + Math.abs(squaredVal));
}
```

}

```
public class App {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number to find its square: ");
int num = scanner.nextInt();
scanner.close();
int squaredVal = 0;
for(int i=0; i<Math.abs(num); i++){
squaredVal = squaredVal + num;
}
System.out.println("Squared value is " + Math.abs(squaredVal));
}
```

}

```
public class App {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number to find its square: ");
int num = scanner.nextInt();
scanner.close();
int squaredVal = 0;
for(int i=0; i<Math.abs(num); i++){
squaredVal = squaredVal + num;
}
System.out.println("Squared value is " + Math.abs(squaredVal));
}
}
```

```
public class App {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number to find its square: ");
int num = scanner.nextInt();
scanner.close();
int squaredVal = 0;
for(int i=0; i<Math.abs(num); i++){
squaredVal = squaredVal + num;
}
System.out.println("Squared value is " + Math.abs(squaredVal));
}
}
```

```
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number to find its square: ");
int num = scanner.nextInt();
scanner.close();
int squaredVal = 0;
for(int i=0; i<Math.abs(num); i++){
squaredVal = squaredVal + num;
}
System.out.println("Squared value is " + Math.abs(squaredVal));
```

Any number can be expressed as sum of powers of 2.

n = 2**k + 2**l + ...

using that principle square(n) = n*(2**k) + n*(2**l) + ...

=(n << k) + (n<<l) + ...

code below

```
int getsquareNoAsterisk (int n) {
int sn= 0;
int pos = 0;
int curN = n;
while (curN != 0) {
if ( (curN & 1) == 1 ) {
// This number has 2**pos value so add it with mulitplied
// by same number using << operator
sn += (n << pos);
}
curN >>= 1;
pos++;
}
return sn;
}
```

Does this make sense?

- newbiev2 August 30, 2014