Question

In: Computer Science

Having a problem with syntax error in Java: JAVA (Perfect Numbers) An integer number is said...

Having a problem with syntax error in Java:

JAVA (Perfect Numbers) An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a method isPerfect that determines whether  parameter number is a perfect number. Use this method in an application that displays all the perfect numbers between 1 and and integer entered by the user. Display the factors of each perfect number to confirm that the number is indeed perfect. Challenge the computing power of your computer by testing numbers much larger than 1000. Display the results.

import java.util.Scanner;
public class IsPerfect {
   static boolean isPerfect(int n)
   { int Sum = 0;
   for (int i=1; i<n ; i++) {
       if(n% i == 0) {
           Sum = Sum + i;
       }
   }
  
   if (Sum == n) {
       return true;
   }
  
   else {
       return false;
   }
}

   public static void main(String[] args) {
   int user_input; // taking user input
   try (Scanner input = new Scanner(System.in)){
  
   System.out.println("Perfect numbers between 1 and "+ user_input + "are:");
   for (int i=1; i<= user_input; i++){
if (isPerfect(i)){
   System.out.println(i);

}
       }
   }
  
}

Solutions

Expert Solution

Answer:

Here is the corrected java code as per your requirement

Raw code:

import java.util.Scanner;

//class named IsPerfect

public class IsPerfect {

//static method to verify weather Perfect number of not

static boolean isPerfect(int n)

//declaring sum value

{

int Sum = 0;

//for loop over the value time

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

//checking if the value leaves reminds 0 multiplied with i

if(n% i == 0) {

//if yes increment sum

Sum = Sum + i;

}

}

//if sum is same value

if (Sum == n) {

//return true it as IsPerfect number

return true;

}

//else no

else {

return false;

}

}

//main method

public static void main(String[] args) {

//declaring varialble

int user_input;

// taking user input

Scanner input = new Scanner(System.in);

//getting integer

user_input=input.nextInt();

//print on the console

System.out.println("Perfect numbers between 1 and "+ user_input + "are:");

//iterate over the values from 1 to user_input range

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

//check every value if its Perfect number

if (isPerfect(i)){

//if it is print the valu

System.out.println(i);

}

}

}

}

Editor:

output:

Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.

"Please refer to the screenshot of the code to understand the indentation of the code".

Thank you! Do upvote.


Related Solutions

Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
(Prime Numbers) An integer is said to be prime if it is divisible by only 1...
(Prime Numbers) An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write pseudocode and function called isPrime that receives an integer and determines whether the integer is prime or not. Write a test program that uses isPrime to determine and print all the prime numbers between 1 and 1000. Display 10 numbers per line. Twin primes...
SOLVE USING WHILE. A perfect number is a positive integer that is equal to the sum...
SOLVE USING WHILE. A perfect number is a positive integer that is equal to the sum of its positive divisors except the number itself. The first two perfect numbers are 6 and 28 since 1+2+3=6 and 1+2+ 4+7+14=28. Write a matlab computer program that finds the first n perfect number (the user must input the value of n) and show them in a vector thanks! xo
Using Java Prime numbers. Write a program that prompts the user for an integer and then...
Using Java Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print 2 3 5 7 11 13 17 19 Recall that a number is a prime number if it is not divisible by any number except 1 and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class PrimePrinter whose main method...
//Java Language Read an integer number from the user. If the number is not positive, change...
//Java Language Read an integer number from the user. If the number is not positive, change its sign.    1   Count the digits of the number 2   Count the odd digits of the number 3   Calculate the sum of the digit values of the number
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
Java Write a program that reads 4 integer numbers from the user combine it into a...
Java Write a program that reads 4 integer numbers from the user combine it into a single number. You need to ask the user to first specify how to combine the numbers (forward or backward). Note that for the backward option you need to check that the last digits should be 0, also for the forward option you need to check that the fist digit is not 0. Use for loops and switch for the menu.                       4. Write an application...
There is a special calculator that when adding numbers rounds the number to the closest integer....
There is a special calculator that when adding numbers rounds the number to the closest integer. For example, 1.1 + 2 + 3.6 will be calculated as 1 + 2 + 4. The error from each addition follows a uniform distribution of (-0.5, 0.5). a. When 1500 numbers are added, what is the probability that the absolute value of the total error is greater than 15? b. How many numbers can be added until the probability of the absolute value...
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT