pointers

Factorial calculation of a number using the pointer in C

Factorial calculation of a number using the pointer in C

In this tutorial, we will discuss the concept of Factorial calculation of a number  using the pointer in C programming language

In this post, we will learn how to calculate factorial of a number using the pointer in C language and go through the given C example to calculate the factorial of a given positive integer

What is factorial

Factorial is a product of all positive descending integer begins with a specified number (n) and calculates up to one

Example

factorial of 5 is

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

 

factorial of 4 is

4!=4*3*2*1=24

 

factorial of n is

n!=n*(n-1)*....2*1
Factorial calculation

Example

Program 1

The program allows the user to enter a number (a positive integer) and then it calculates factorial of given number using pointer variable in C programming language

//C program find factorial of given number in C
#include <stdio.h>
#include <stdlib.h>
void findFactorial(int, int *);//Function prototype
int main()
{
    int i,fact,num;
    printf("Enter a number: \n");
    scanf("%d",&num);//Takes input from the user
    findFactorial(num, &fact);//function call
    printf("Factorial of %d is: %d",num,fact);
    //display the factorial of given number
    getch();
    return 0;
}

void findFactorial(int num, int *fact){//function definition
int i;

*fact=1;

for(i=1; i<=num; i++){//find factorial using for loop
*fact=*fact*i;
}
}

When the above code is executed, it produces the following results

Enter a number
6
Factorial of 6 is : 720

 

Suggested for you

input-output function in C language

Variable in C language

data type in C language

the operator in C language

function in C programming language with example

user-defined function in C language

Pointer in C language

 

Similar post

Find factorial  of a number in C language

Find factorial of a number using the function in C

Find factorial  of a number in C++ language

Find factorial of a number using the function in C++ language

Factorial program in C++ using the pointer

 

C++ program to find the odd or even number using switch statements
Factorial program using the pointer in C++ language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago