×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Menu Based Program in Java Using Switch case Neon Numbers In Java - ICSE Examples
Perfect Numbers In Java - ICSE Examples - Java
6091    Arnab De    14/05/2018

Perfect No In Java - ICSE Examples

Write a program to print all the perfect nos between two limit enterd by the user with help of the following function
 

boolean IsPerfect(int num) - to return true if num is perfect otherwise print false.

A Number is said to be perfect if sum of all factors excluding itself should be equal to the number. Let N=6, Its factors are 1,2,3 and the sum of the factors (1+2+3)=6. So 6 is the perfect no. Examples of some Other perfect numbers are 28 and 496.


void showPerfectNumbers(int start,int end) - to print all the perfect nos between start and end limits by using functions IsPerfect().

Write a main function to execute the same.

softetechnologies
import java.util.*;
public class PerfectNumbers
{
    public boolean IsPerfect(int num)
    {
        int sum=0;
        for(int k=1;k<= num/2;k++)
        {
            if(num%k==0)
            {
                sum=sum+k;
            }
        }
        if(sum==num)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public void showPerfectNumbers(int start, int end)
    {
        System.out.println("Perfect Numbers are \n================");
        for(int i=start;i<=end;i++)
        {
             if(IsPerfect(i))
             {
                   System.out.println(i);
             }
        }
    }
    public static void main(String []args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter Start Number:");
        int s=sc.nextInt();
        System.out.print("Enter end Number:");
        int e=sc.nextInt();
        PerfectNumbers pn=new PerfectNumbers();
        pn.showPerfectNumbers(s,e);
    }
}
softetechnologies

Output


Enter Start Number:2
Enter end Number:900

Perfect Numbers are
================
6
28
496

Menu Based Program in Java Using Switch case Neon Numbers In Java - ICSE Examples
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Sri Sri Ramakrishna Paramahamsa
Have love for everyone, no one is other than you.
Sri Sri Ramakrishna Paramahamsa
1384
69.48
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     50608
25/06/2018     42596
01/01/2018     41778
28/06/2017     39560
02/08/2017     38349
01/08/2017     32573
06/07/2017     32270
15/05/2017     31726
14/07/2017     27859
11/09/2018     27598
softetechnologies