Finding absolute value

This question can be asked on a phone interview or during a job fair. It gives a quick idea of problem solving skills of the candidate. To find out the absolute of any number, you have to know its concept.

Concept is 'If a number's value is < 0 then its positive equivalent is returned or else the number is returned as it.
For value=0, the number is returned as is' that means 0 is return.

Examples:

  1. If n = -10 then its absolute value will be 10. 
  2. If n = 10 then its absolute value will be 10. 
  3. If n = 0 then its absolute value will be 0. Following implementation find a number's absolute value.

 
void int findAbsoluteValue(int n)
{
 if(n < 0)
  return n*-1;
 else 
  return n;
}


You might also like:
Find White Spaces in Character Array
Find a character in the String
Number is prime or not
Notes on Sorting and Searching Algorithms
Common String Functions
Reverse a String
Product of all array location expect its own
Find a cycle in the Linked List
Find a binomial co-efficient
Remove duplicates from the array
Telephonic phone technical interview questions - Part 2
Counting sort algorithm
B-Tree

No comments:

Post a Comment

NoSQL

This one is reviewed but I need to delete its copy from hubpages or somewhere NoSQL Data models: key-value  Aggregate model.  key or i...