0% found this document useful (0 votes)
17 views5 pages

C++ Code 1 For Factorials

The document contains 5 code snippets that demonstrate different approaches to calculating the factorial of a number in C programming language. The first code uses a while loop, the second uses a for loop, the third uses pointers, the fourth defines a function, and the fifth calculates the factorial series within a given range. All programs prompt the user to input a number, calculate its factorial, and output the result.

Uploaded by

Fetsum Lakew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

C++ Code 1 For Factorials

The document contains 5 code snippets that demonstrate different approaches to calculating the factorial of a number in C programming language. The first code uses a while loop, the second uses a for loop, the third uses pointers, the fourth defines a function, and the fifth calculates the factorial series within a given range. All programs prompt the user to input a number, calculate its factorial, and output the result.

Uploaded by

Fetsum Lakew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Code 1:

1. C code for factorial of a number


2. C program to find the factorial of a given number
3. Factorial program in c using while loop
4. Factorial program in c without using

recursion
#include<stdio.h>
int main(){
int i=1,f=1,num;
printf("Enter a number: ");
scanf("%d",&num);
while(i<=num){
f=f*i;
i++;
}

printf("Factorial of %d is: %d",num,f);


return 0;

Sample output:
Enter a number: 5
Factorial of 5 is: 120
Code 2:
1. Factorial program in c using for loop
2. Simple factorial program in c
3. C program to calculate factorial
#include<stdio.h>
int main(){
int i,f=1,num;
printf("Enter a number: ");

scanf("%d",&num);
for(i=1;i<=num;i++)
f=f*i;
printf("Factorial of %d is: %d",num,f);
return 0;
}
Code 3:
1. Factorial program in c using pointers
2. How to calculate factorial in c
3. Factorial program in c language
#include<stdio.h>
void findFactorial(int,int *);
int main(){
int i,factorial,num;
printf("Enter a number: ");
scanf("%d",&num);
findFactorial(num,&factorial);
printf("Factorial of %d is: %d",num,*factorial);
}

return 0;

void findFactorial(int num,int *factorial){


int i;
*factorial =1;
for(i=1;i<=num;i++)
*factorial=*factorial*i;
}

Code 4:
1. Factorial program in c using function
2. C program to find factorial of a number
#include<stdio.h>
int findFactorial(int);
int main(){
int i,factorial,num;
printf("Enter a number: ");
scanf("%d",&num);
factorial = findFactorial(num);
printf("Factorial of %d is: %d",num,factorial);
}

return 0;

int findFactorial(int num){


int i,f=1;
for(i=1;i<=num;i++)
f=f*i;
return f;
}
Sample output:
Enter a number: 8
Factorial of 8 is: 40320
Code 5:
1. Factorial series in c
#include<stdio.h>
int main(){
long f=1;
int i,num,min,max;

printf("Enter the minimum range: ");


scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Factorial series in given range: ");
for(num=min;num<=max;num++){
f=1;
for(i=1;i<=num;i++)
f=f*i;
}

printf("%ld ",f);

return 0;
}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 10
Factorial series in given range: 1 2 6 24 120 720 5040
40320 362880 3628800

Algorithm:
Factorial value
Factorial of number is defined as:
Factorial (n) = 1*2*3 * n
For example: Factorial of 5 = 1*2*3*4*5 = 120
Note: Factorial of zero = 1

Using for loop

#include<iostream.h>
int main()
{
int num,factorial=1;
cout<<" Enter Number To Find Its Factorial: ";
cin>>num;
for(int a=1;a<=num;a++)
{
factorial=factorial*a;
}
cout<<"Factorial of Given Number is ="<<factorial<<endl;
return 0;
}

You might also like