Pronic Number Checking in java
Steps to do this:-
1. take two integers i and n (n is for user input number i is for loop and calculating the consecutive number multiplication )2. take a boolean value b=false;
3.take the user input value
4.start the for loop (i=1;i<=n;i++) now check this
if(i*(i+1)==n)
{
b=true;
break;
}
close the for loop and check
if(b) // here b is true
{
print"this is prionic number"
}
else
{
print "this is not a prionic number"
}
now the loop will run from 1 to user input number
suppose user input 30
then the loop will run i=1to4 and it will not match the answer, but when the i=5 then
if(5*(5+1)==30 // 30==30
then b will be true loop will break, and it will print that 30 is prionic number because 30 is the product of 5 and 6 two consecutive numbers
5.save and run
The Source Code
import java.util.Scanner;
class prionic
{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int i,n;// i is for loop and n is for the number which will be user input
boolean b=false;// for condition checking
System.out.println("enter the number");
n=s.nextInt();
for(i=0;i<=n;i++)
{
if(i*(i+1)==n)
{
b=true;
break;
}
}
if(b)
{
System.out.println("the number you entered "+n+" is the product of "+i+" and "+(i+1));
System.out.println("those are consecutive number ");
System.out.println("this is a prionic number");
}
else{
System.out.println("the number is not a product of two consecutive number");
System.out.println("this is not a prionic number");
}
}
}
Watch My Video
Comments
Post a Comment
if you like it or have any doubts please let me know