Archive Pages Design$type=blogging

Write a Program in C to Calculate the Factorial Value of an Integer.

Here is a program in C to calculate the factorial value of an integer. The factorial of a number is the product of all the integers between ...


Program in C to Calculate the Factorial Value of an Integer
Here is a program in C to calculate the factorial value of an integer.

The factorial of a number is the product of all the integers between 1 and that number.  For example factorial of 5 is 5*4*3*2*1. This can also be expressed as 5!=5*4!, where '!' stands for factorial.

Hence factorial of a number can also be programmed using recursion. Here I have given two methods for calculating factorial of a number, using non-recursive function and using recursive function.




Non-recursive function for calculating the factorial value of an integer

 

Steps:

  1. Declare prototype for the function named factorial() used to calculate factorial value.
  2. Declare two integers a and fact.
  3. Prompt the message to enter any number to calculate the factorial.
  4. Allow the user to enter number using scanf().
  5. Use function factorial() to calculate the factorial value and return that.
  6. Print the returned value to the screen using printf() command. 

 

code:


#include <stdio.h>


int factorial(int);


int main()
{
int a, fact;

printf("Enter any number");
scanf("%d",&a);

fact=factorial(a);
printf("Factorial value-%d\n", fact);
return 0;
}

int factorial(int x)
{
int f=1,i;

for(i=x;i>=1; i--)
f=f*i;

return(f);

}



Recursive function for calculating the factorial value of an integer


Steps:

 

  1. Declare prototype for the function named rec() used to calculate factorial value.
  2. Declare two integers a and fact.
  3. Prompt the message to enter any number to calculate the factorial.
  4. Allow the user to enter number using scanf().
  5. Use function rec() to calculate the factorial value using recursive method and return that.
  6. Print the returned value to the screen using printf() command. 

 


code:


#include <stdio.h>

int rec(int);
int main()
{
int a, fact;

printf("Enter any number");
scanf("%d",&a);

fact=rec(a);
printf("Factorial value-%d\n", fact);
return 0;
}

int rec(int x)
{
int f;

if(x==1)
return(1);
else
f=x*rec(x-1);
return(f);

}






Related Posts:


تعليقات

الاسم

Artificial Intelligence(AI) Backlinking Basic IT Best List Blogging Tips C C# C++ Computer Architecture Computer Fundamental Computer Security Computer/IT Officer Exam CSS Data Mining and Warehousing Data Recovery Tools Data Structure Database Management System E-commerce E-government Internet & Web Designing IT Law IT Tips and Tricks IT Tutorials Java JavaScript Keyword Research Tools MIS Multiple Choice Question (MCQ) Networking Online Earning Online IT Jobs Operating System Oracle Forms and Reports Programming Guide Programming Language SEO Social Networking Sites Software Download Software Engineering System Analysis and Design Top List VB.Net
false
rtl
item
Max6: Write a Program in C to Calculate the Factorial Value of an Integer.
Write a Program in C to Calculate the Factorial Value of an Integer.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjD4DADRAkGRAVnnOYP_Pp-HklDpA1HYYZM4R8wWyYNxjwubj8zTGYrFtGM6rUdTbWA0CxSvvuAj77eaHnSbnoGwQYeVfa9F5gUpWFdY0IKVhVIkVrtXhU1XKsUJhcNW6R2ssvi6BIXNCfs/s1600/factorial.PNG
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjD4DADRAkGRAVnnOYP_Pp-HklDpA1HYYZM4R8wWyYNxjwubj8zTGYrFtGM6rUdTbWA0CxSvvuAj77eaHnSbnoGwQYeVfa9F5gUpWFdY0IKVhVIkVrtXhU1XKsUJhcNW6R2ssvi6BIXNCfs/s72-c/factorial.PNG
Max6
https://maxs29.blogspot.com/2014/07/write-program-in-c-to-calculate.html
https://maxs29.blogspot.com/
http://maxs29.blogspot.com/
http://maxs29.blogspot.com/2014/07/write-program-in-c-to-calculate.html
true
4303790511137642940
UTF-8
لم يتم العثور على اى مواضيع شاهد الكل اقرأ المزيد رد احذف الرد حذف بواسطة الرئيسية صفحات مواضيع شاهد الكل نوصى لك التسميات الارشيف بحث لم يتم العثور على ماتبحث عنه عودة للرئيسية الاحد الاثنين الثلاثاء الاربعاء الخميس الجمعة السبت الاحد الاثنين الثلاثاء الاربعاء الخميس الجمعة السبت يناير فبراير مارس ابريل مايو يونيو يوليو اغسطس سبتمبر اكتوبر نوفمبر ديسمبر يناير فبراير مارس ابريل مايو يونيو يوليو اغسطس سبتمبر اكتوبر نوفمبر ديسمبر just now 1 دقيقة مضت $$1$$ دقيقة مضت 1 hour ago $$1$$ ساعة مضت الامس $$1$$ يوم مضى $$1$$ اسبوع مضى اكثر من 5 اسابيع مضت