Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: sphenic program problem

  1. #1
    Junior Member snow__16's Avatar
    Join Date
    Jul 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post sphenic program problem

    A Sphenic Number is a positive integer n which is product of exactly three distinct primes. The first few sphenic numbers are 30, 42, 66... Given a number n, determine whether it is a Sphenic Number or not.
    What's wrong with my code?
    mport java.util.*;
    class Sphenic
    {
    public boolean checkPrime(int n)
    {
    int i,c=0;
    {
    for(i=0;i<=n;i++)
    {
    if(n%i==0)
    {
    c++;
    }
    if(c==2)
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    }
    }
    public static void main()
    {
    Scanner in=new Scanner (System.in);
    int no,i,j,k,flag=0;
    System.out.println("Enter the number");
    no=in.nextInt();
    boolean ans1,ans2,ans3;
    Sphenic obj=new Sphenic();
    for(i=2;i<=no;i++)
    {
    ans1=checkPrime(i);
    if(ans1==true)
    {
    for(j=2;j<=no;j++)
    {
    ans2=checkPrime(j);
    if(ans2==true)
    {
    for(k=2;k<=no;k++)
    {
    ans3=checkPrime(k);
    if(ans3==true)
    {
    if(i!=j && j!=k && k!=i && i*j*k==no)
    {
    flag=1;
    }
    }
    }
    }
    }
    }
    }
    if(flag==1)
    {
    System.out.println("The number is a Sphenic number");
    }
    else
    {
    System.out.println("The number is not a Sphenic number");
    }
    }
    }
    Last edited by Norm; July 11th, 2021 at 11:44 AM. Reason: Shorten title

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: A Sphenic Number is a positive integer n which is product of exactly three distinct primes. The first few sphenic numbers are 30, 42, 66... Given a number n, determine whether it is a Sphenic Number or not.What's wrong with my code

    Can you explain why you think something is wrong with the code?
    Also please explain the algorithm you are using to solve the problem.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member snow__16's Avatar
    Join Date
    Jul 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sphenic program problem

    whenever i compile the program it always shows "non-static method cannot be referenced". hence don't know where do I need to do the correction and how .

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: sphenic program problem

    non-static method cannot be referenced
    change the method so that it is static. See the main method for an example.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. calculate the factors of a positive integer number
    By ATB in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2014, 07:48 PM
  2. Replies: 10
    Last Post: November 8th, 2012, 06:29 AM
  3. Replies: 2
    Last Post: November 7th, 2012, 10:45 PM
  4. The sum and product of a positive number between 1000 and 9999.
    By metaleddie13 in forum Member Introductions
    Replies: 1
    Last Post: September 15th, 2011, 04:39 AM
  5. Printing Sum of Primes of a number
    By CodeNewb in forum What's Wrong With My Code?
    Replies: 9
    Last Post: July 12th, 2010, 01:25 PM