Computer Science, asked by np916872, 5 months ago

W.A.P. to enter a number and print its factorial.​

Answers

Answered by BrainlyProgrammer
1

Answer:

This is done in QBASIC

CLS

PRINT "ENTER A NO."

INPUT N

FOR I=1 to N

F=F*I

NEXT I

PRINT F

END

Answered by anindyaadhikari13
3

Required Answer:-

Given Question:

  • Write a program to enter a number and print it's factorial.

Program:

This is a very simple question. Check it out for solution.

package com.student;

import java.util.*;

public class Factorial

{

public static void main(String s[])

{

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int n = sc.nextInt();

long f=1L;

for(int i=2;i<=n;i++)

f*=i;

System.out.println("Factorial of the number is: "+f);

sc.close();

}

}

How it's solved?

Before we start, you must know what is factorial of an integer.

Factorial:- Factorial of any given integer (n) is the product of all the numbers from 1 to n.

For example,

Factorial of 5 is 120

5! = 5 × 4 × 3 × 2 × 1 = 120

Now, let's understand how the program works.

At first, I have imported Scanner class. Scanner class is used to take input from the keyboard. It is present in the utility package of Java. Then, I have created a class, main() method and objects of Scanner class. Now, I have taken the number as input from the user. The nextInt() method of Scanner class is used to take integer as input. Now, I have created a variable named f which will store the factorial of the given number. Then, I have created a for loop that iterates from i=2 to n. Then, I have multiplied each numbers with f. After the completion of loop, we have found the factorial of the number stored in variable f. Time to print.

Variable Description:

  • n (int):- To input the number.
  • f (long):- To store the factorial of n.
  • i (int):- Loop variable.

Output is attached.

Attachments:
Similar questions