-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3.cpp
More file actions
85 lines (72 loc) · 1.46 KB
/
day3.cpp
File metadata and controls
85 lines (72 loc) · 1.46 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
using namespace std;
int printSum(int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += i;
}
return sum;
}
int fact(int n)
{
if (n == 0)
{
return 1;
}
return n * (fact(n - 1));
}
int sumOfdigits(int n)
{
int sum = 0;
while (n > 0)
{
int digit = n % 10;
sum += digit;
n = n / 10;
}
return sum;
}
// binomial coefficient
float binomial(int n, int r)
{
return (float)fact(n) / (fact(r) * fact(n - r));
}
// fibonacci series
int fib(int n)
{
if (n == 0)
{
return 0;
}
if (n == 1)
{
return 1;
}
return fib(n - 1) + fib(n - 2);
}
int main()
{
// functions basics
// in context of c++ stack is one of the important data structures,
// the main() is stored in the stack frame (call stack)
// pass by value - copy of the arguments
// pass by reference - use &a,& etc or pointer *a
int n, r;
cout << "Enter numbers :: ";
cin >> n >> r;
cout << "sum upto " << n << " is :: " << printSum(n) << endl;
cout << "factoraial upto " << n << " is :: " << fact(n) << endl;
cout << "Binomial is :: " << binomial(n, r) << endl;
cout << "Fibonacci series :: ";
for (int i = 0; i < n; i++)
{
cout << fib(i) << " ";
}
int num;
cout << "Enter another number :: ";
cin >> num;
cout << "sum of digits is :: " << sumOfdigits(num) << endl;
return 0;
}