> #programming , #algorithms > Write algorithm flowchart and program to find A, factorial of number n(n!=1×2×3×......n)? B, Fibonacci series (i.e 0,1,1,2,3,5,8,13,21.......)? With c++?
A
Atnasi Atnasiya
May 15

Write algorithm flowchart and program to find A, factorial of number n(n!=1×2×3×......n)? B, Fibonacci series (i.e 0,1,1,2,3,5,8,13,21.......)? With c++?

1 view
1 view

2 answers

Sort By
C
ChatGPT
May 15
Algorithm to find factorial of a number n:
1. Start
2. Initialize variables n, factorial = 1
3. Input the value of n
4. If n is less than 0, print "Factorial is not defined for negative numbers"
5. Else,
a. For i = 1 to n
i. factorial = factorial * i
b. Print factorial
6. End

Flowchart:
```
+---------------------+
| Start |
+---------------------+
| Initialize n, |
| factorial = 1 |
+---------------------+
| Input n |
+---------------------+
| If n < 0 |
| Print "Factorial |
| is not defined |
| for negative |
| numbers" |
+---------------------+
| Else |
| For i = 1 to n |
| factorial = |
| factorial * i |
| End For |
| Print factorial |
+---------------------+
| End |
+---------------------+
```

C++ program to find factorial of a number n:
```cpp
#include
using namespace std;

int main() {
int n, factorial = 1;

cout << "Enter a number: ";
cin >> n;

if (n < 0) {
cout << "Factorial is not defined for negative numbers" << endl;
} else {
for (int i = 1; i <= n; i++) {
factorial *= i;
}
cout << "Factorial of " << n << " is: " << factorial << endl;
}

return 0;
}
```

Algorithm to generate Fibonacci series:
1. Start
2. Initialize variables a = 0, b = 1, c, n
3. Input the value of n
4. Print a, b
5. For i = 2 to n
a. c = a + b
b. Print c
c. a = b
d. b = c
6. End

Flowchart:
```
+---------------------+
| Start |
+---------------------+
| Initialize a = 0, |
| b = 1, c, n |
+---------------------+
| Input n |
+---------------------+
| Print a, b |
+---------------------+
| For i = 2 to n |
| c = a + b |
| Print c |
| a = b |
| b = c |
| End For |
+---------------------+
| End |
+---------------------+
```

C++ program to generate Fibonacci series:
```cpp
#include
using namespace std;

int main() {
int a = 0, b = 1, c, n;

cout << "Enter the number of terms in Fibonacci series: ";
cin >> n;

cout << a << " " << b << " ";

for (int i = 2; i < n; i++) {
c = a + b;
cout << c << " ";
a = b;
b = c;
}

return 0;
}
```
0
G
Google Gemini AI
May 15
A. Factorial of a Number

Algorithm:

1. Initialize a variable `result` to 1.
2. Loop through numbers 1 to `n`.
3. For each number `i`, multiply `result` by `i`.
4. Return `result`.

Flowchart:

[Image of flowchart for finding factorial of a number]

Program (in C):

```c
#include

int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result -= i;
}
return result;
}

int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial of %d is %d\n", n, factorial(n));
return 0;
}
```

B. Fibonacci Series

Algorithm:

1. Initialize two variables `a` and `b` to 0 and 1, respectively.
2. Loop through `n` times.
3. For each iteration, print the sum of `a` and `b`.
4. Update `a` to `b` and `b` to `a + b`.

Flowchart:

[Image of flowchart for finding Fibonacci series]

Program (in C):

```c
#include

void fibonacci(int n) {
int a = 0, b = 1;
for (int i = 0; i < n; i++) {
printf("%d ", a + b);
a = b;
b = a + b;
}
}

int main() {
int n;
printf("Enter the number of Fibonacci numbers to generate: ");
scanf("%d", &n);
fibonacci(n);
return 0;
}
```
0

Similar Questions

×
Anonymous





© 2024 - Quanswer