Pronic Number | Write a program in Java to check whether a given number is a Pronic Number or not. | ISC Computer Science | ICSE Computer Application
A pronic number, oblong number, rectangular number or heteromecic number, is a number which is the product of two consecutive integers, that is, n (n + 1).
Algorithm
- Start
- x:=0
- Print "Enter a number"
- Read x
- n=(int)Math.sqrt(x)
- If n*(n+1)==true, go to step 7 or else go to step 8
- Print "The given number is a Pronic Number". Go to step 9.
- Print "The given number is not a Pronic Number"
- Stop
Program
Buffered Reader
import java.io.*;
class Pronic
{
int x;
void accept() throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
System.out.println("Enter a number");
x=Integer.parseInt(br.readLine());
}
boolean isPronic()
{
int n=(int)Math.sqrt(x);
if(n*(n+1)==x)
{
return true;
}
else
{
return false;
}
}
void displayt()
{
System.out.println("The given number is a Pronic Number");
}
void displayf()
{
System.out.println("The given number is not a Pronic Number");
}
void main() throws IOException
{
accept();
if(isPronic()==true)
{
displayt();
}
else
{
displayf();
}
}
}
Scanner
import java.util.*;
class Pronic
{
int x;
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
x=sc.nextInt();
}
boolean isPronic()
{
int n=(int)Math.sqrt(x);
if(n*(n+1)==x)
{
return true;
}
else
{
return false;
}
}
void displayt()
{
System.out.println("The given number is a Pronic Number");
}
void displayf()
{
System.out.println("The given number is not a Pronic Number");
}
void main()
{
accept();
if(isPronic()==true)
{
displayt();
}
else
{
displayf();
}
}
}
Documentation
Variable Description
Data Type | Variable Name | Description |
int | x | To store the number entered by the user |
int | n | To store the square root of the number |
Method Description
Return Type | Method Name | Description |
void | accept() | To accept data from the user |
boolean | isPronic() | To check if the given number is Pronic or not |
void | displayt() | To display the statement if the condition is true |
void | displayf() | To display the statement if the condition is false |
void | main() | To act as an entry gateway for the compiler. |
Output
Share with all ISC Computer Science Students you know.
Comment below with all your program requests and blog suggestions.
Follow us on social media by clicking on these links.
Instagram : @IscJavaCode
Twitter: @IscCode
Quora: ISC Java Code
Stay Healthy
Stay Happy
Stay Helpful
Comments
Post a Comment