Find factorial of a number using function

  

Problem Statement:-  Program to print factorial of any given number using a user-defined function.


Sample Input/Output:-


Sample Input First:

5

Sample Output First: 

120

Sample Input Second: 

8

Sample Output Second: 

40320


Data requirement:-


  Input Data:- num


  Output Data:-fact


Program in C

Here is the source code of the C Program to Find the factorial of a number using function.


Code:

#include <stdio.h>/*printf,scanf definitions */
#define li long long int
/* This function find factorial of a number*/
li factorial_of_number(int num)
{
    int fact=1;
    for(int i=num;i>=1;i--)
        fact*=i;
    return fact;
}
int main()
{
    int num;
    /* Get the number input*/
    printf("Enter the Number:");
    scanf("%d", &num);
    /* Display the factorial of a given number*/
    printf("Factorial Enter number is: %lld",factorial_of_number(num));
}


Input/Output:

Enter the Number:5

Factorial Enter number is: 120


Program in C++

Here is the source code of the C++ Program to print factorial of any given number using a user-defined function.


Code:

#include<iostream>
using namespace std;
#define li long long int
/* This function find factorial of a number*/
li factorial_of_number(int num)
{
    int fact=1;
    for(int i=num;i>=1;i--)
        fact*=i;
    return fact;
}
int main()
{
    int num;
    /* Get the number input*/
    cout<<"Enter the Number:";
    cin>>num;
    /* Display the factorial of a given number*/
    cout<<"Factorial Enter number is: "<<factorial_of_number(num);
}


Input/Output:

Enter the Number:8

Factorial Enter number is: 40320


Program in Java

Here is the source code of the Java Program to Find the factorial of a number using function.


Code:

import java.util.Scanner;

public class FindFactorial {
    /* This function find factorial of a number */
    static long FactorialOfNumber(int num) {
        long fact = 1;
        for (int i = num; i >= 1; i--)
            fact *= i;
        return fact;
    }

    public static void main(String[] args) {
        java.util.Scanner cs = new Scanner(System.in);
        /* Get the number input */
        System.out.print("Enter the Number:");
        int num = cs.nextInt();
        /* Display the factorial of a given number */
        System.out.println("Factorial Given Number is: " + FactorialOfNumber(num));
        cs.close();
    }
}


Input/Output:

Enter the Number:9

Factorial Given Number is: 362880


Program in Python

Here is the source code of the Python Program to print factorial of any given number using a user-defined function.


Code:

""" This function find factorial of a number"""
def factorial_of_number(num):
    fact=1
    for i in range(num,1,-1):
        fact*=i
    return fact
""" Get the number input """
num=int(input("Enter the Number:"))
""" Display the factorial of a given number """
print("Factorial Enter number is: ",factorial_of_number(num))


Input/Output:

Enter the Number:3

Factorial Enter number is:  6

Most Recommend Questions:-








More Questions:-



Post a Comment

0 Comments