-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQues-11.cpp
73 lines (59 loc) · 1.36 KB
/
Ques-11.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
QUESTION-11:
Write a program to calculate factorial and to compute the factors of a given no. (i)using recursion, (ii) using iteration
SOLUTION:
*/
#include<iostream>
using namespace std;
int i = 1;
int factorial(int n);
void factor(int n, int i);
int iter_factorial(int n);
void iter_factor(int n);
int main()
{
cout << "\n\t ~~~~~~~~~~~~~~~~~~~~~~~Practical 11~~~~~~~~~~~~~~~~~~~~\n\t\t\t\t \n";
int n;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factorial of " << n << " using recursion = " << factorial(n);
cout<<endl;
cout << "Factors of " << n << " using recursion = " ;
factor(n,i);
cout<<endl;
cout << "Factorial of " << n << " using iteration = " << iter_factorial(n);
cout<<endl;
cout << "Factors of " << n << " using iteration = " ;
iter_factor(n);
cout<<endl;
return 0;
}
void factor(int n,int i)
{
if (i<= n) {
if (n % i == 0) {
cout << i << " ";
}
factor(n, i + 1 );
}
}
int factorial(int n)
{
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}
int iter_factorial(int n){
int fact = 1;
for(int i=1;i<=n;i++){
fact = fact *i;
}
return fact;
}
void iter_factor(int n){
for(int j = 1; j <= n; j++) {
if(n % j == 0)
cout << j << " ";
}
}