Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 646407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:36:30+00:00 2026-05-13T21:36:30+00:00

here is the code (java): class prime { public static boolean prime (int a,

  • 0

here is the code (java):

class prime
{

    public static boolean prime (int a, int b) 
    { 
        if (a == 0) 
        {
            return false; 
        }
        else if ((a%(b-1) == 0) && (b>2))
        {
            return false; 
        }
        else if (b>1) 
        {
            return (prime (a, b-1)) ;
        }
        else
        {
            return true; 
        }

    }

    public static void main (String[] arg) 
    {
        System.out.println (prime (7, 7)) ; 
    }
}

This is the error message i get when i try to run it (it compiles fine):

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at prime.prime(prime.java:10)
    at prime.prime(prime.java:16)
    at prime.prime(prime.java:16)
    at prime.prime(prime.java:16)
    at prime.prime(prime.java:16)
    at prime.prime(prime.java:16)
    at prime.prime(prime.java:16)
    at prime.main(prime.java:27)

So this means i devided by zero some how right? or does it mean something else? I don’t see how i’m dividing by zero. What went wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:36:30+00:00Added an answer on May 13, 2026 at 9:36 pm

    Try turning this around

    if ((a%(b-1) == 0) && (b>2))
    

    to

       if ((b>2) && a%(b-1)==0)
    

    What’s happening is that the a%(b-1) operation is being executed before the b>2 test.

    After the switch, you are taking advantage of short-circuit evaluation. Once the b>2 test returns false, then there’s no need to calculate the modulus (hence avoiding the division)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code: import javax.swing.*; import java.awt.*; public class PanelModel { public static
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
Here is a simple piece of code: import java.io.*; public class Read { public
Here is the code: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class TestGrid {
Here is the server code package echoserver; import java.net.*; import java.io.*; public class EchoServer
// Here's my code: Main Class: import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.DataBufferInt; import java.awt.image.BufferStrategy;
I happen to come across a Java code at my work place. Here's the
I am trying to call a java script function from java code. Here is
i created 2 files... namely Shirt.java and ShirtTest.java The code for the Shirt.java class
Here is code: import java.awt.*; import java.util.*; import javax.media.*; import javax.media.protocol.*; import javax.media.control.*; import

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.