-1

I already wrote some code and would like to know how to make all the prime numbers under 10, (2,3,5,7) to be multiplied by the first 5 digits above 0, (1,2,3,4,5). Here n on the 5th line would be the prime numbers under 10.

 package sct;


public class PrimeNumberMultiplicationTables {

    }
            public static void main(String[] args) {
                int n = ??? ;
                for(int i=1; i <= 5; i++)
                {
                    System.out.println(n+" multiplied by "+i+" = "+n*i);

           }
      }
}

Thank you in advance to whoever finds this.

Hot
  • 11
  • 1
  • Hint: you need an *array* of integers. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – Stephen C Jul 24 '18 at 03:12
  • what do you mean? – Hot Jul 24 '18 at 03:13
  • Read the link I just provided you. (A single `int` variable cannot represent 4 values at the same time .....) – Stephen C Jul 24 '18 at 03:14
  • Are you asking about how to find prime numbers? Or just about printing out multiplication tables? – Kevin Anderson Jul 24 '18 at 03:21
  • Yes but I want to do it as if I didnt know the prime number to be able to customise the program and do it for example with prime numbers from 0 to 100 get it? I can't enter each of them in an array list, that would take forever. Could you please help me do that? – Hot Jul 24 '18 at 03:23
  • I think you need to back up a little bit, and think about this: how many prime numbers could come out of the configured range? Maybe none. What would you display? Maybe just 1. Would you display it as multiplied by 1 and 2 and 3, etc...? And if so, what if it was many primes? If you think about it that way, I assume that you need to take each prime (yeah, you really need an array of integers), then in your output, show them multiplied by each number in that second list. – benc Jul 24 '18 at 05:21
  • @Hot - You have changed the requirements. In the question *and* the title, you explicitly ask how to implement this for prime numbers under 10. You really need to try to solve this for yourself. – Stephen C Jul 24 '18 at 07:38

1 Answers1

-2

Here it is in C#, you can convert it to java.

class Program
{
    static void Main(string[] args)
    {
        for (var i = 1; i <= 10; i++)
        {
            if (!IsPrime(i)) continue;

            for (var j = 1; j <= 5; j++)
            {
                var value = i * j;
                Console.WriteLine(i + " multiplied by " + j + " = " + value);
            }
        }

        Console.ReadLine();
    }

    public static bool IsPrime(int candidate)
    {
        if ((candidate & 1) == 0)
        {
            return candidate == 2;
        }

        for (var i = 3; (i * i) <= candidate; i += 2)
        {
            if (candidate % i == 0)
            {
                return false;
            }
        }
        return candidate != 1;
    }
}

Adding Java Version:

public class HelloWorld{

 public static void main(String []args){
    for (int i = 1; i <= 10; i++)
    {
        if (!IsPrime(i)) continue;

        for (int j = 1; j <= 5; j++)
        {
            int value = i * j;
            System.out.println(i + " multiplied by " + j + " = " + value);
        }
    }
 }

public static boolean IsPrime(int candidate)
{
    if ((candidate & 1) == 0)
    {
        return candidate == 2;
    }

    for (int i = 3; (i * i) <= candidate; i += 2)
    {
        if (candidate % i == 0)
        {
            return false;
        }
    }
    return candidate != 1;
}

}

Rob Stewart
  • 441
  • 4
  • 14