How to print factorial of a number using While loop

+1 vote
Factorial of a number is easy to implement using for loop. I am new to Java and want to know as can I do it using while loop?
Nov 22, 2018 in Java by Nitesh
• 3,080 points
1,107 views

1 answer to this question.

0 votes

While also works like for just you need to be manipulative with the initialization and conditions.

You can take a reference from this program.

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);
    }
}
answered Nov 22, 2018 by Namitha

Related Questions In Java

+1 vote
2 answers

Factorial of a number using while loop

import java.util.*; public class Fact { ...READ MORE

answered Mar 19, 2020 in Java by leela
• 140 points
3,887 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,276 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
1,647 views
0 votes
1 answer

How to get the number of digits in an int?

You can find out the length of ...READ MORE

answered May 14, 2018 in Java by Akrati
• 3,190 points
726 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
966 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
964 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,337 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,090 points
90,202 views
0 votes
2 answers

How to parse a date?

SimpleDateFormat parser=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz ...READ MORE

answered Dec 6, 2018 in Java by Sushmita
• 6,910 points
625 views
0 votes
1 answer

How to handle infinite loop in Java?

You can simply add a increment statement ...READ MORE

answered Nov 22, 2018 in Java by Anoop
995 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