Adv Java Program : Write a JSP program to check whether given number is Perfect or not. (Use Include directive).

 

Write a JSP program to check whether given number is Perfect or not. (Use Include directive). 







Slip2A.html


<!DOCTYPE html>

<html>

<head>

<title><</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

<body>

<form action="Slip2.jsp" method="post">

Enter Number :

<input type="text" name="num">

<input type="submit" value="submit">

</form>

</body>

</html>





Slip2A.jsp



<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<%

intnum = Integer.parseInt(request.getParameter("num"));

int n = num;


    // To store sum of divisors

    int sum = 1;

 

    // Find all divisors and add them

    for (int i = 2; i * i <= n; i++)

    {

        if (n % i==0)

        {

            if(i * i != n)

                sum = sum + i + n / i;

            else

                sum = sum + i;

        }

    }

    // If sum of divisors is equal to

    // n, then n is a perfect number

    if (sum == n && n != 1)

{

out.println("\perfect Number");

}

else

{

out.println("Not perfect Number");

}

%>



Post a Comment

0 Comments