Computer Science, asked by Anonymous, 4 hours ago

write a program to compute the factorial of a given number using a while loop.
(java)

Answers

Answered by BrainlyProgrammer
3

Answer:

import java.util.*;

class num{

public static void main (String ar []){

int n=sc.nextInt();

int p=1,I=1;

while(I++<=n){

p*=I;

}

System.out.println("Factorial:"+p);

}

}

Logic:-

  • Initialise I=1,p=1
  • run while loop with condition I++<=n (I will automatically increase here)
  • multiply I with p
  • print p
Answered by CopyThat
5

Program :-  {JAVA}

import java.util.Scanner;

public class Factorial

{

    public static void main(String args [ ] )

    {

          Scanner keyboard = newScanner(System.in);

          int factorial = 1;

          System.out.println(''Enter a number: '');

          int num = keyboard.nextInt();

          while (num > 0)

          {

              factorial*= num;

              num--;

          }

         System.out.printl(''Factorial is: '' + factorial);

         keyboard.close();

      }

}

Output :-

Enter a number: 7

Factorial is: 5040

Note :

7 = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040

Similar questions