亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Logic behind Perfect number
How to Check Perfect number in Java?
1.? By using while loop
2. By using Static Method
3. By using the recursion Method
Examples
Example #3
Conclusion
Home Java javaTutorial Perfect Number in Java

Perfect Number in Java

Aug 30, 2024 pm 04:28 PM
java

Java provides a different kind of number system to the user; the perfect number is also one kind of number system that is provided by java. We can consider any number as a perfect number in Java. If all factors excluding the number it is equal to the given number, then we can consider the given number to be a perfect number. In Java, we can use different ways to find the perfect number. Basically, the perfect number is nothing but any number whose base is 10 in the number system, and it is a subfield of the number system in mathematics. As per user requirements, we can use the perfect number system.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Logic behind Perfect number

Now let’s see the logic behind the perfect number in java as follows.

The basic logic of the perfect number is very simple. First, we need to find out the positive factor of a given number then make a sum of all factors excluding the number itself. If the sum of the factor is equal to the given number, then we can say that the given number is the perfect number, and if the sum of the factor is not equal to the given number, then we can say that the given number is not a perfect number. Let’s see an example of a perfect number; then we will get a detailed idea as follows.

Suppose we need to check if 8 is a perfect number or not.

  • First find out the positive factor that is 1, 2, 4, and 8. Now make the addition of all factors excluding the 8, and we get the result is 7. Now compare the result with the given number; see here both numbers are different. That means we can say that this number is not a perfect number.
  • Now let’s consider another number: 6.
  • Find the positive factor of 6 is that 1, 2, 3, and 6.
  • The Sum of all factors is 6, excluding 6.
  • Now compare the result with the given number, see here the given number and result is the same that means we can say that the given number is a perfect number.

How to Check Perfect number in Java?

Now let’s see how to check the perfect number java as follows. In java programming, there are three different ways to check the perfect number as follows.

1.? By using while loop

In the while loop, we need to follow some steps as follows.

1. First, we need to read the number entered from the user.

2. The loop will continue until the condition (j<=no/2) is false. If the remainder of no/j is 0, then add the value of j into the sum and increase the value of j. After completion of all iterations, compare the result. If the booth number is equal, then print the given number as a perfect number; else, print the given number as not a perfect number.

3. For example no=8, j=1 no/2=4, so j <=4 means condition is true, now find the factor.

8%j=0 true then sum =1

J=2 2<4 is true 8%2 =0 is true and sum= 1+3=3.

So in this way, we complete all the iterations to find the perfect number.

2. By using Static Method

In this method, we can call a static method to check the perfect number; in this method, we just need to call the PerfacOrNot method. It automatically calculates the sum of all positive factors and checks if the given number is perfect or not.

3. By using the recursion Method

In this method, we also call the PerfectOrNot () method using objects. In this method, execution starts itself and calls PerfectOrNot (). It repeats the iteration until the j<= number/2 is false.

Examples

Now let’s see the different examples of perfect numbers in Java as follows.

Example #1

Code:

import java.util.Scanner;
class Perfect_number1
{
public static void main(String arg[])
{
long num,s=0;
Scanner s_c=new Scanner(System.in);
System.out.println("Enter number");
num=s_c.nextLong();
int j=1;
while(j<=num/2)
{
if(num%j==0)
{
s+=j;
}
j++;
}
if(s==num)
{
System.out.println(num+" the given number is perfect number");
}
else
System.out.println(num+" the given number is not perfect number");
}
}

Explanation

By using the above program, we try to implement perfect numbers in java with a while loop. The coding of the above program is very simple here we created the main method; inside the main method, we use scanner class and while loop to find a factor of a given number and add that factor into the s variable that is the sum of the factor as shown in the above program. Finally, we compare both numbers and print the message as per the comparison. The final output of the above program we illustrate by using the following screenshot as follows.

Perfect Number in Java

Example #2

Now let’s see another example of a perfect number by using the static method as follows.

Code:

import java.util.Scanner;
class Perfect_Method
{
public static void main(String arg[])
{
long num,m;
Scanner s_c=new Scanner(System.in);
System.out.println("Enter number");
num=s_c.nextLong();
m=perfectOrNot(num);
if(m==num)
System.out.println(num+" The given number is perfect number");
else
System.out.println(num+" The given number is not perfect number");
}
static long perfectOrNot(long n)
{
long s=0;
for(int j=1;j<=n/2;j++)
{
if(n%j==0)
{
s+=j;
}
}
return s;
}
}

Explanation

In the above program, we use a static method to check if a given number is a perfect number or not. In the above program, we use the perfecOrNot () method. After that, we use a for loop to find the factors of the given number, and the remaining process is the same, that is, to compare the result with the given number and print a message according to the comparison. The final output of the above program we illustrate by using the following screenshot as follows.

Perfect Number in Java

Example #3

Now let’s see another example to check perfect numbers by using the recursive Method as follows.

Code:

public class Recursive
{
static int n = 200;
static int s = 0;
static int d = 1;
static int findPerfect(int n, int d) {
{
if(d<=n/2)
{
if(n%d==0)
{
s+=d;
}
d++;
findPerfect(n,d);
}
return s;
}
}
public static void main(String args[])
{
int r = findPerfect(n,d);
if(r == n)
System.out.println(" The given number is perfect Number");
else System.out.println("The given number is not perfect Number");
}
}

Explanation

In the above program, we are using a recursive method to check the perfect number. The final output of the above program we illustrate by using the following screenshot as follows.

Perfect Number in Java

Conclusion

We hope from this article you learn Perfect Number in java. From the above article, we have learned the basic logic of Perfect Numbers, and we also see different examples of Perfect Numbers. From this article, we learned how and when we use the Perfect Number in java.

The above is the detailed content of Perfect Number in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Grow A Garden - All Animals And How To Get Them
4 weeks ago By 尊渡假賭尊渡假賭尊渡假賭
How to Remove & Clean Ink in Cash Cleaner Simulator
3 weeks ago By 尊渡假賭尊渡假賭尊渡假賭
Roblox: Grow A Garden - Complete Weather Guide
1 months ago By 尊渡假賭尊渡假賭尊渡假賭
Revenge Of The Savage Planet: Every Outfit And How To Unlock It
3 weeks ago By 尊渡假賭尊渡假賭尊渡假賭

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use JavaScript to determine whether two arrays are equal? How to use JavaScript to determine whether two arrays are equal? May 23, 2025 pm 10:51 PM

In JavaScript, you need to use a custom function to determine whether two arrays are equal, because there is no built-in method. 1) Basic implementation is to compare lengths and elements, but cannot process objects and arrays. 2) Recursive depth comparison can handle nested structures, but requires special treatment of NaN. 3) Special types such as functions and dates need to be considered, and further optimization and testing are required.

Use VSCode to perform version fallback operation of code Use VSCode to perform version fallback operation of code May 15, 2025 pm 09:42 PM

In VSCode, you can use Git for code version fallback. 1. Use gitreset--hardHEAD~1 to fall back to the previous version. 2. Use gitreset--hard to fall back to a specific commit. 3. Use gitrevert to safely fall back without changing history.

How to correctly handle this pointing in a closure? How to correctly handle this pointing in a closure? May 21, 2025 pm 09:15 PM

The methods to correctly handle this pointing in JavaScript closures include: 1. Use arrow functions, 2. Use bind methods, 3. Use variables to save this. These methods ensure that this intrinsic function correctly points to the context of the external function.

How to implement data encryption with JavaScript? How to implement data encryption with JavaScript? May 23, 2025 pm 11:12 PM

Using JavaScript to implement data encryption can use the Crypto-JS library. 1. Install and introduce the Crypto-JS library. 2. Use the AES algorithm for encryption and decryption to ensure that the same key is used. 3. Pay attention to the secure storage and transmission of keys. It is recommended to use CBC mode and environment variables to store keys. 4. Consider using WebWorkers when you need high performance. 5. When processing non-ASCII characters, you need to specify the encoding method.

What are the four categories of Java? Description of Java Basic Type System Classification What are the four categories of Java? Description of Java Basic Type System Classification May 20, 2025 pm 08:27 PM

Java's four basic type systems include integer types, floating point types, character types and boolean types. 1. Integer types (byte, short, int, long) are used to store numerical values ??without decimals. Choosing the appropriate type can optimize memory and performance. 2. Float type (float, double) is used for decimal values. Pay attention to accuracy issues. If necessary, BigDecimal is used. 3. Character type (char) is based on Unicode and is suitable for single characters, but String may be required in international applications. 4. Boolean types are used for true and false values, simplifying logical judgments and improving code readability.

Python concatenate list strings Python concatenate list strings May 14, 2025 am 12:08 AM

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

Environment configuration for running Ruby code in VSCode Environment configuration for running Ruby code in VSCode May 15, 2025 pm 09:30 PM

Configuring the Ruby development environment in VSCode requires the following steps: 1. Install Ruby: Download and install from the official website or using RubyInstaller. 2. Install the plug-in: Install CodeRunner and Ruby plug-ins in VSCode. 3. Set up the debugging environment: Install the DebuggerforRuby plug-in and create a launch.json file in the .vscode folder for configuration. This way, you can write, run, and debug Ruby code efficiently in VSCode.

How to copy and paste layer styles in PS? How to copy and paste layer styles in PS? May 16, 2025 am 06:00 AM

Copying and pasting layer styles in Photoshop is a key trick to improve productivity. Let's dive into how to do it, and the various details and techniques you may encounter in the process. When we talk about copying and pasting layer styles in Photoshop, the first thing we need to understand is that layer styles refer to effects applied to layers, such as shadows, glows, bevels, and reliefs. Mastering this feature not only saves time, but also ensures consistent design. To copy the style of a layer, right-click the layer you want to copy and select Copy Layer Style. This will copy all the styles of the layer into the clipboard. Next, select the target layer you want to apply these styles, right-click it, and select Paste

See all articles