Factorial of a number using while loop

+1 vote

import java.util.*;
public class WhileFactorial {
    public static void main(String[] args)
    {
        System.out.println("Enter a number");
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        int fact=1;
        while (num !=0);
        {
            fact = fact * num;
            num-=1;
        }
        System.out.println("Factorial of the number is "+fact);
    }
}

Nov 23, 2018 in Java by Nitesh
• 3,080 points
4,143 views

2 answers to this question.

0 votes
import java.util.*;
public class WhileFactorial {
    public static void main(String[] args)
    {
        System.out.println("Enter a number");
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        int fact=1;
        while (num !=0)
        {
            fact = fact * num;
            num-=1;
        }
        System.out.println("Factorial of the number is "+fact);
    }
}

You have added a semicolon to while that completely changes the meaning of using while here.

answered Nov 23, 2018 by Namitha
0 votes
import java.util.*;

public class Fact

{

    public static void main(String[] args)

       {

             int i=1,fact=1;

             System.out.println("Enter the number :");

             Scanner sc=new Scanner(System.in);

             int num=sc.nextInt();

             while(i<=n)

               {

                  fact=fact*i;

                  i++;

                }

               System.out.println("Factorial is :",+fact);

        }

}
answered Mar 19, 2020 by leela
• 140 points

Related Questions In Java

0 votes
2 answers

How can I sort values of a Map in Java using its key

Assuming TreeMap is not good for you ...READ MORE

answered Oct 10, 2018 in Java by Sushmita
• 6,920 points
1,207 views
0 votes
1 answer

Number of lines in a file in Java

I found this an efficient way in counting ...READ MORE

answered Jul 4, 2018 in Java by scarlett
• 1,290 points
605 views
0 votes
1 answer

How to count the number of occurrences of an element in a List?

We can use the static frequency() method. int ...READ MORE

answered Aug 21, 2018 in Java by sharth
• 3,370 points
5,663 views
0 votes
1 answer

How to convert a string representation of a hex dump to a byte array using Java?

public static byte[] hexStringToByteArray(String s) { ...READ MORE

answered Sep 26, 2018 in Java by sharth
• 3,370 points
2,138 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,332 views
+1 vote
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
1,190 views
+1 vote
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,680 points
3,752 views
+1 vote
1 answer

How to print factorial of a number using While loop?

While also works like for just you ...READ MORE

answered Nov 22, 2018 in Java by Namitha
1,363 views
+4 votes
4 answers

Using while loop i want to print 10 even numbers

Hello @Nitesh, you are pretty much there. ...READ MORE

answered Nov 22, 2018 in Java by Priyaj
• 58,020 points
93,106 views
0 votes
2 answers

How can I invoke a method when the method name is in the form of a given string?

You could probably use method invocation from reflection: Class<?> ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
3,075 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP