1
import java.util.*;
class Factorial{

         void factNum(int n){
                int fact=1;
          ArrayList<Integer> al=new ArrayList<Integer>();
               for(int i=1;fact<=n;i++){
                     fact=fact*i;
                         al.add(i);
                    if(fact==n){
                        for(int j:al){
                        System.out.print(+j);
                        }}

    }

}

        public static void main(String args[]){

          Scanner scan=new Scanner(System.in);
           Factorial f=new Factorial();
           System.out.println("Enter the Integer");
           int num=scan.nextInt();
           f.factNum(num);
}
}

I wrote this code so that when I input number 24 which is factorial I will get 1 2 3 4 as a output and when I input 23 i should get output as false. My code is almost correct but the problem is that when I enter number which does not belong to factorial series, I am not getting False as output. Please can anyone help me where in this code I can keep System.out.print("False"); Statement.

2
  • Don't print a result from factNum(). Have factNum() return the ArrayList<Integer>. If the returned result has elements, print them. If it doesn't, print "False".
    – David
    Nov 20, 2016 at 12:54
  • 1
    You should try formatting the program better to make it easier for people to understand what you're trying to achieve. Nov 20, 2016 at 12:54

3 Answers 3

2

You are not printing "False" anywhere.Keep a boolean flag to check if the given number is factorial or not and print accordingly.

void factNum(int n){
        int fact=1;
        boolean flag = true;
        ArrayList<Integer> al=new ArrayList<Integer>();
        for(int i=1;fact<=n;i++){
            fact=fact*i;
            al.add(i);
            if(fact==n){
                flag = false;//if factorial then make it false
                for(int j:al){
                    System.out.print(+j);
                }}

        }
        if(flag)
            System.out.println("False");

    }
1

You can use flag value as shown in below code to keep track of the condition of whether the number is factorial or not.

public static void factNum(int n) {

    int fact = 1;
    ArrayList<Integer> al = new ArrayList<Integer>();

    boolean flag = false;

    for (int i = 1; fact <= n; i++) {
        fact = fact * i;
        al.add(i);
        if (fact == n) {
            flag = true;
            for (int j : al) {
                System.out.print(+j);
            }
        }
    }

    if(!flag)
        System.out.println("false");

}
0

Try this code.

void factNum(int n) {
        int fact = 1;
        ArrayList<Integer> al = new ArrayList<Integer>();
        int check = 0;
        for (int i = 1; fact <= n; i++) {
            fact = fact * i;
            al.add(i);
            if (fact == n) {
                for (int j : al) {
                    System.out.print(+j);
                    check = 1;
                }
            }
            else if (fact>n && check == 0)
            {
                System.out.println("False");            
            }
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.