Question
find all pronic numbers in a given range.
(The pronic number can be defined as the number which is a product of two consecutive numbers. )
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class PronicNumbersInGivenRange
{
/*isPronicNumber() will determine whether a given number is pronic number or not*/
public static boolean isPronicNumber(int num)
{
boolean flag = false;
for(int j = 1; j <= num; j++)
{
/* Checks for pronic number by multiplying consecutive numbers */
if((j*(j+1)) == num)
{
flag = true;
break;
}
}
return flag;
}
public static void main(String[] args)
{
int lowerRange=0,upperRange=0,i=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the lower range");
lowerRange=sc.nextInt();
System.out.println("Enter the Upper range");
upperRange=sc.nextInt();
//Displays pronic numbers between lowerRange and upperRange
System.out.println("Pronic numbers between "+lowerRange+" and "+upperRange);
for(i = lowerRange; i <=upperRange; i++)
{
if(isPronicNumber(i))
{
System.out.print(i + " ");
}
}
}
}