Wednesday, March 18, 2009

Puzzle 7 – Solution

The trick here is to generate a '-1' and then multiple the number to be subtracted with it to get a negative of the subtractant.

package com.twisters;
import java.util.Scanner;
public class SubtractFloat{

public static void main(String args[]){

float a,b,result;
System.out.print("Enter the first num : ");
a = new Scanner(System.in).nextFloat();
System.out.print("Enter the second num : ");
b = new Scanner(System.in).nextFloat();
result = a + (~0)*b; //alternately we can use cos(PIE) instead of ~0
to generate -1
System.out.println("Result of subtracting is : "+result);
}
}

Sorry no correct answers this week!

No comments:

Post a Comment

Solution for this question?