Showing posts with label Factorial. Show all posts
Showing posts with label Factorial. Show all posts

TO FIND FACTORIAL OF A NUMBER USING C PROGRAM

How TO FIND FACTORIAL OF A NUMBER USING C PROGRAM.


#include<stdio.h>
int main(){
  int i=1,f=1,num;
  printf("\nEnter a number:");
  scanf("%d",&num);
  while(i<=num){
      f=f*i;
      i++;
  }
  printf("\nFactorial of %d is:%d",num,f);
  return 0;
}

C Program 0.1 + 0.2 != 0.3 Solved

How to write a C Program 0.1 + 0.2 != 0.3 Solved in C Programming Language ?


Solution For C Program :

C Program to Find Factorial Number

How to write a C Program to find the factorial of a number in C Programming Language ?

Solution of C Program: 

/*C Program to Find Factorial Number*/
#include<stdio.h>
void main()
{
int x, n, fact;
printf("\nEnter the value of n : ");
scanf("%d", &n);
x = 1;
fact = 1;
while(x <= n)
{
fact*=x;
x++;
}
printf("\nThe Factorial of a Number is : %d.", fact);
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable


Factorial Program In C Using Function

How to write a Factorial Program In C Using Function ?

Solution:

Factorial Program In C Using Function

#include<stdio.h>

long factorial(int);

main()
{
   int number;
   long fact = 1;

   printf("Enter a number to calculate it's factorial\n");
   scanf("%d",&number);

   printf("%d! = %ld\n", number, factorial(number));

   return 0;
}

long factorial(int n)
{
   int c;
   long result = 1;

   for( c = 1 ; c <= n ; c++ )
         result = result*c;

   return ( result );
}

Factorial Program In C Using Recursion

How to write a C Program Factorial program in c using recursion in C Programming Language ?


Solution:

Factorial program in c using recursion


#include<stdio.h>

long factorial(int);

main()
{
   int num;
   long f;

   printf("ENTER A NUMBER TO FIND FACTORIAL :");
   scanf("%d",&num);

   if(num<0)
      printf("NEGATIVE NUMBERS ARE NOT ALLOWED");
   else
   {
      f = factorial(num);
      printf("%d!=%ld",num,f);
   }
   return(0);
}

long factorial(int n)
{
   if(n==0)
      return(1);
   else
      return(n*factorial(n-1));
}

C program to add first seven terms of the following series using a for loop: (1/1!) + (2/2!) + (3/3!) + .....

How to Write a C Program to add first seven terms of the following series using a for loop:

(1/1!) + (2/2!) + (3/3!) + .....

This C Program to add first seven terms of the following series using a for loop: (1/1!) + (2/2!) + (3/3!) + .....


  1. #include<stdio.h>
  2. main()
  3. {
  4. float result, temp_calc, num, temp, factorial;
  5. result=0;
  6. for (num=1; num<=7; num++)
  7. {
  8. for (temp=1, factorial=1; temp<=num; temp++)
  9. {
  10. factorial = factorial*temp;
  11. temp_calc = (num/factorial);
  12. }
  13. result=result+temp_calc;
  14. }
  15. printf("(1/1!) + (2/2!) + (3/3!) + (4/4!) + (5/5!) + (6/6!) + (7/7!) = %f", result);
  16. }

Factorial in C Programming Language

How to write a C Program Factorial in C Programming Language Example ?

Solution:
  1. #include <stdio.h>
  2.  
  3.  
  4. int fac1(int n, int acc){
  5.    return (> 0 ) ? fac1(n-1, n*acc) : acc ;
  6. }
  7.  
  8. int fac(int n){
  9.    int acc = 1;
  10.    return fac1(n, acc);
  11. }
  12.  
  13. int fac2(int n){
  14.    int acc = 1;
  15.    for(int i = 0; i < n; i++)
  16.       acc *= (n-i);
  17.    return acc;
  18. }
  19. int main(int argc, char *argv[]) {      
  20.    int num;
  21.    scanf("%d", &num);
  22.    printf("%d\n", fac(num));    
  23.    printf("%d\n", fac2(num));  
  24.         return 0;
  25. }