Practice 2 Java code

advertisement

/**

* Write a description of class Practice2 here.

*

* @author (Mrs. D)

* @version (11/30/15)

*/ import java.util.Scanner; public class Practice2

{

public static void main(String[]args)

{Scanner kb=new Scanner(System.in);

System.out.println("How many lines of the pattern?");

int num=kb.nextInt(); //stores the user's response

pattern(num);

//System.out.println("What number do you want the divisors of?");

//int num2=kb.nextInt(); //stores the user's response

// divisors(num2);

//System.out.println();

//System.out.println("What number do you want check if prime?");

//int num3=kb.nextInt(); //stores the user's response

//prime(num3);

// System.out.println("print5PowersFor(1) = ");

// print5Powers(1);

// System.out.println();

// System.out.println("print5PowersFor(2) = ");

// print5Powers(2);

// System.out.println();

// System.out.println("print5PowersFor(3) = ");

//

}

/**

* example: print5Powers(2) -> 1,2,4,8,16

* example: print5Powers(3) -> 1,3,9,27,81

* example: print5Powers(4) -> 1,4,16,64,256

*/

public static void print5Powers(int n)

{

int product = 1;

int i = 1; //counter

while(i<5) // condition: happens 5 times

{

System.out.print(product + ", ");

product = product * n;

i++;

}

System.out.println(product);

}

public static void print5PowersFor(int n)

{

int product = 1;

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

{

System.out.print(product + ", ");

product = product * n;

}

System.out.println(product);

}

/**

* Write a method that will return the sum of the

* integers from a to b, inclusive. Use a for loop.

* precondition a<=b

* example:sumNums(3,6) -> 18

*/

public static int sumNums(int a, int b)

{

int sum = 0;

for(int i = a;i<=b;i++)

{

sum = sum + i ;

}

return sum;

}

/**

* Return true if a number is considered "perfect."

* example: 28 is perfect because 1+2+4+7+14 = 28

*/

public static boolean isPerfectNumber(int n)

{

int sum = 0; // stores the current sum of the factors

for(int i = 1; i<=n/2;i++)

{

int x = n%i; // x is the remainder when n is divided by i

if (x==0) // if(i is a factor)

{

sum = sum + i;// add i to the sum of the factors

}

}

if(sum==n)//test to see if sum of factors = the given number

{

return true;

}

else

{

return false;

}

// return sum==n;

}

/**

* Return the greatest common factor of two given integer parameters.

*/

public static int gcf(int a, int b)

{

int num1 = a;

int num2 = b;

int r = a%b; // starting value

while(r != 0)

{

num1 = num2;

num2 = r;

r = num1%num2;

}

return num2;

}

/**

* Nested loops! An integer value is to be taken as an input

* from the user and the following pattern is to be printed

* based on the input. If the input is seven,

* the following pattern needs to be printed.

*

**

***

****

*****

******

*******

*/

public static void pattern(int n)

{

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

{

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

{

System.out.print("*");

}

System.out.println();

}

}

/**

* Print the Divisors! Display the proper divisors of a given number

*/

public static void divisors(int n)

{ int limit = n/2;

for (int d=2; d<=limit; d++)

{

if(n%d==0)

System.out.print(d+"");

}

}

/**

* Determine if number is prime

*/

public static void prime(int n)

{int count =0;

int limit=n/2;

for (int d=2; d<=limit; d++)

{

if (n%d==0)

count++;

}

if (count!=0)

System.out.println("Not Prime");

else

}

System.out.println("Prime");

}

Download