CHECKING THE NUMBER IS PETERSON OR NOT IN JAVA

 package mypro;

import java.util.Scanner;

public class PetersonBetweenRang {

public static int fact(int n) {

  int fact=1;

  for(int i= 1; i<=n;i++) {

  fact= fact*i;

  }

  

  return fact;

}


public static void main(String[] args) {

System.out.println("Enter the range r1 and r2 to check all the number they are or not ");

Scanner obj = new Scanner(System.in);

int r1 = obj.nextInt();

int r2 = obj.nextInt();

for(int i=r1; i<=r2; i++) {

int n=i;

int restn=n,rem;int add=0;

while(n>0) {

rem=n%10;

add= add+fact(rem);

 

n= n/10;

}

// System.out.println(add);

if(restn==add) {

System.out.println("The numbe "+ restn+" is peterson ");

}

else {

System.out.println(" The number "+ restn+" is not peterson");

}

}

}

}


Comments

Popular posts from this blog

250+ C Programs for Practice

FUNCTION OVERRIDING IN CPP