Divide function

package tests;

import org.testng.annotations.Test;

/**
* Divide without standard divison and using add.
* @author asharda
*
*/
public class DivisonTest {

public int divide(int dividend,int divisor) throws Exception
{
int count=0;
int temp=divisor;
if((divisor ==0) && (dividend!=0))
{
throw new Exception(“Divide by zero error”);
}
else if((dividend==0) && (divisor!=0) )
{
return 0;
}
else
{
while(temp<=dividend)
{
temp=temp+divisor;
if(temp==0)
{
return 1;
}
count++;

}
}//end of else
return count;
}

@Test
public void testDivison() throws Exception
{
System.out.println(“Divisor seen is “+divide(50,5));
}

}

Advertisements

About qainterviews

Software Geek,QA Expert,Blogger
This entry was posted in Java, LeetCode Solutions, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s