99,602 views
45 votes
45 votes
Please Help! (Language=Java) This is due really soon and is from a beginner's computer science class!

Assignment details:
CHALLENGES
Prior to completing a challenge, insert a COMMENT with the appropriate number.

1) Get an integer from the keyboard, and print all the factors of that number. Example, using the number 24:

Factors of 24 >>> 1 2 3 4 6 8 12 24
2) A "cool number" is a number that has a remainder of 1 when divided by 3, 4, 5, and 6. Get an integer n from the keyboard and write the code to determine how many cool numbers exist from 1 to n. Use concatenation when printing the answer (shown for n of 5000).

There are 84 cool numbers up to 5000
3) Copy your code from the challenge above, then modify it to use a while loop instead of a for loop.

5) A "perfect number" is a number that equals the sum of its divisors (not including the number itself). For example, 6 is a perfect number (its divisors are 1, 2, and 3 >>> 1 + 2 + 3 == 6). Get an integer from the keyboard and write the code to determine if it is a perfect number.

6) Copy your code from the challenge above, then modify it to use a do-while loop instead of a for loop.

User Amseager
by
2.5k points

2 Answers

15 votes
15 votes

Final answer:

To complete these challenges, use loops to determine factors of a number, count cool numbers, and modify code to use different loop types.

Step-by-step explanation:

Challenge 1:

To print all the factors of a given number, you can use a for loop to iterate from 1 to the given number. Within the loop, check if the number is a factor by using the modulo operator to determine if the remainder is 0. If it is, print the number as a factor.

Challenge 2:

To determine how many cool numbers exist from 1 to a given number, use a for loop to iterate from 1 to the given number. Within the loop, check if the number gives a remainder of 1 when divided by 3, 4, 5, and 6. If it does, increment a variable that keeps count of the cool numbers. Finally, print the count of cool numbers.

Challenge 3:

To modify the previous code to use a while loop, the initialization and increment statements for the loop need to be moved outside the loop. Create a variable to keep track of the current number, initialize it to 1, and increment it within the loop. The loop condition should check if the current number is less than or equal to the given number.

User KML
by
3.7k points
16 votes
16 votes

Answer:

For challenge 1:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

// Get an integer from the keyboard

Scanner scanner = new Scanner(System.in);

System.out.print("Enter an integer: ");

int num = scanner.nextInt();

// Print all the factors of the integer

System.out.print("Factors of " + num + " >>> ");

for (int i = 1; i <= num; i++) {

if (num % i == 0) {

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

}

}

}

}

For challenge 2:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

// Get an integer from the keyboard

Scanner scanner = new Scanner(System.in);

System.out.print("Enter an integer: ");

int n = scanner.nextInt();

// Count the number of cool numbers from 1 to n

int coolCount = 0;

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

if (i % 3 == 1 && i % 4 == 1 && i % 5 == 1 && i % 6 == 1) {

coolCount++;

}

}

// Print the result using concatenation

System.out.println("There are " + coolCount + " cool numbers up to " + n);

}

}

For challenge 3:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

// Get an integer from the keyboard

Scanner scanner = new Scanner(System.in);

System.out.print("Enter an integer: ");

int n = scanner.nextInt();

// Count the number of cool numbers from 1 to n using a while loop

int coolCount = 0;

int i = 1;

while (i <= n) {

if (i % 3 == 1 && i % 4 == 1 && i % 5 == 1 && i % 6 == 1) {

coolCount++;

}

i++;

}

// Print the result using concatenation

System.out.println("There are " + coolCount + " cool numbers up to " + n);

}

}

For challenge 5:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

// Get an integer from the keyboard

Scanner scanner = new Scanner(System.in);

System.out.print("Enter an integer: ");

int num = scanner.nextInt();

// Determine if the integer is a perfect number

int sum = 0;

for (int i = 1; i < num; i++) {

if (num % i == 0) {

sum += i;

}

}

if (sum == num) {

System.out.println(num + " is a perfect number.");

} else {

System.out.println(num + " is not a perfect number.");

}

}

}

For challenge 6:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

// Get an integer from the keyboard

Scanner scanner = new Scanner(System.in);

System.out.print("Enter an integer: ");

int num = scanner.nextInt();

// Determine if the integer is a perfect number using a do-while loop

int sum = 0;

int i = 1;

do {

if (num % i == 0) {

sum += i;

}

i++;

} while (i < num);

if (sum == num) {

System.out.println(num + " is a perfect number.");

} else {

System.out.println(num + " is not a perfect number.");

}

}

}

User Roger Garza
by
2.7k points