Jump to content

Need Help with finding a Factor

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Hi All

i am doing a few revision questions before i go back to college next week and i think i have done this wrong ( the code i put together finds the factor of the numbered entered ) here is the question and the code i have done so far.

Write a program that will determine whether a number entered has either 3 or 11 as a factor.



// revision practical 6c question 5

// Gman

// 4.11.11



import java.util.Scanner;

	public class NumberFactorEntered

	{

	

		public static void main(String[] args)

		{

			

			

			Scanner kb = new Scanner(System.in);

				// declare variable

			int num1 = 0;

				// get number

			System.out.print("Enter a number : ");

			num1 = kb.nextInt();

				 // calculate number to find the factors

			for ( int i = 1; i <= num1; i++)

			{

					// if number has no remainder than display

				if ( num1 % i == 0 )

				{

				System.out.println(i + " is a Factor of " + num1);

				}

			}

		

		}

	

	}




#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Why are you not just testing for divisibility by 3 and 11?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts

WingedPanther said:

Why are you not just testing for divisibility by 3 and 11?

only starting learning java how would i go about that then.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
// revision practical 6c question 5
// Gman
// 4.11.11


import java.util.Scanner;
    public class NumberFactorEntered
    {
    
        public static void main(String[] args)
        {
            
            
            Scanner kb = new Scanner(System.in);
                // declare variable
            int num1 = 0;
                // get number
            System.out.print("Enter a number : ");
            num1 = kb.nextInt();
                 // calculate number to find the factors
            if ( num1 % 3 == 0 )
            {
                System.out.println(3 + " is a Factor of " + num1);
            }
            if ( num1 % 11 == 0 )
            {
                System.out.println(11 + " is a Factor of " + num1);
            }
        
        }
    
    }

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Thanks for help WingedPanther so i was going about it wrong..

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
One thing I've learned, it's much easier to solve a different, but similar problem, than to solve the actual problem :) Happens in math and programming.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
very true , thanks again.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users