Showing posts with label Find. Show all posts
Showing posts with label Find. Show all posts
C Program To Find Factorial of a Number Using Recursion
Example to find factorial of a non-negative integer (entered by the user) using recursion.
To understand this example, you should have the knowledge of following C programming topics:
The factorial of a positive number n is given by:
factorial of n (n!) = 1*2*3*4....n
The factorial of a negative number doesn't exist. And the factorial of 0 is 1.
You will learn to find the factorial of a number using recursion in this example. Visit this page to learn, how you can find the factorial of a number using loop.
Example: Factorial of a Number Using Recursion
#include <stdio.h>
long int multiplyNumbers(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n)
{
if (n >= 1)
return n*multiplyNumbers(n-1);
else
return 1;
}
OutputEnter a positive integer: 6 Factorial of 6 = 720Suppose the user entered 6.Initially, themultiplyNumbers()is called from themain()function with 6 passed as an argument.Then, 5 is passed to themultiplyNumbers()function from the same function (recursive call). In each recursive call, the value of argumentn is decreased by 1.When the value of n is less than 1, there is no recursive call.Check out these related examples:
C++: Program to calculate age in years, months and days.
Input the date of birth of a person and find his age in terms of year, month and days. using class.
Age Calculator of a Person
Code:
//for more program please visit www.programtubenotes.blogspot.in
#include<iostream>#include<conio.h>
#include<stdlib.h>
#define WELCOME "WELCOME TO AGE CALCULATOR"
using namespace std;
class Age
{
private:
int day,month,year;
public:
void getdata(); //input date
friend void show(Age t, Age b, Age a);//calculate age
};
void Age::getdata()
{
cin>>day>>month>>year;
}
void show(Age t, Age b, Age a)
{
if(t.day<b.day)
{
t.month--;
t.day+=30;
}
a.day=t.day-b.day;
if(t.month<b.month)
{
t.year--;
t.month+=12;
}
a.month=t.month-b.month;
a.year=t.year-b.year;
cout<<"\nYour Age is "<<a.year<<" year "<<a.month<<" month "<<a.day<<" day ";
cout<<"\n\n\nCreated By:- Mr. Maheswar Sahoo\nFor more application please contact:-maheswarsahoo47@gmail.com";
cout<<"\nThank You...\n\n\n";
}
int main()
{
system("COLOR 4C"); //for background color
Age birth;
Age today,age;
cout<<"\t\t\t\t"<<WELCOME;
cout<<"\n\n\nEnter date of today (dd mm yyyy) : ";
today.getdata();
system("COLOR 1C"); //change background color
cout<<"\nEnter date of birth (dd mm yyyy) : ";
birth.getdata();
show(today,birth,age);
return 0;
}
We suggest this video to watch:
Related word:
Class
Popular codes:
Subscribe to:
Posts (Atom)
C Programming Online Test 1
Following quiz provides Multiple Choice Questions (MCQs) related to C Programming Framework. You will have to read all the given answ...
-
Input the date of birth of a person and find his age in terms of year, month and days. using class. Age Calculator of a Person Co...
-
In our previous tutorial we have discussed all about Inheritance. What is it? Why it is required? and its different types! Moreover, we ...
-
Following quiz provides Multiple Choice Questions (MCQs) related to C Programming Framework. You will have to read all the given answ...