1.
Program to Find Factorial of a Number (Using
Recursion)
Algorithm:
1. Start
2. Input n
3. If n == 0 or n == 1, return 1 (Base case)
4. Else, call the recursive function factorial(n-1) * n
5. Output the result
6. End
Flowchart:
plaintext
Copy code
Start
|
Input n
|
Is n == 0 or n == 1?
|
Yes --> Return 1
|
No --> Return n * factorial(n-1)
|
Output result
|
End
C++ Code:
cpp
Copy code
#include <iostream>
using namespace std;
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Factorial of " << n << " is " << factorial(n) <<
endl;
return 0;
}
Sample Output:
mathematica
Copy code
Enter a number: 5
Factorial of 5 is 120
2. Program to Implement Merge Sort
Algorithm:
1. Start
2. Split the array into halves recursively until each part
contains a single element
3. Merge the parts in sorted order
4. Continue merging until the full array is sorted
5. Output the sorted array
6. End
Flowchart:
plaintext
Copy code
Start
|
Input array, n
|
Divide array into two halves
|
If array size > 1
|
Merge sort left half
|
Merge sort right half
|
Merge two halves in sorted order
|
Output sorted array
|
End
C++ Code:
cpp
Copy code
#include <iostream>
using namespace std;
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (int i = 0; i < n1; i++)
L[i] = arr[l + i];
for (int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr) / sizeof(arr[0]);
cout << "Given array: ";
for (int i = 0; i < arr_size; i++)
cout << arr[i] << " ";
mergeSort(arr, 0, arr_size - 1);
cout << "\nSorted array: ";
for (int i = 0; i < arr_size; i++)
cout << arr[i] << " ";
return 0;
}
Sample Output:
c
Copy code
Given array: 12 11 13 5 6 7
Sorted array: 5 6 7 11 12 13
3. Program to Read and Write to a File
Algorithm:
1. Start
2. Open a file for writing
3. Write data to the file
4. Close the file
5. Open the same file for reading
6. Read data from the file and display it
7. End
Flowchart:
plaintext
Copy code
Start
|
Open file for writing
|
Write data to file
|
Close file
|
Open file for reading
|
Read data from file
|
Display data
|
End
C++ Code:
cpp
Copy code
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outFile;
outFile.open("example.txt");
outFile << "Hello, this is a test file.";
outFile.close();
ifstream inFile;
inFile.open("example.txt");
string content;
while (getline(inFile, content)) {
cout << content << endl;
}
inFile.close();
return 0;
}
Sample Output (File Content):
kotlin
Copy code
Hello, this is a test file.
4. Program to Find Fibonacci Series Using Recursion
Algorithm:
1. Start
2. Input n
3. Define a recursive function fibonacci(n):
o If n == 0 return 0
o If n == 1 return 1
o Else return fibonacci(n-1) + fibonacci(n-2)
4. Call fibonacci(n) to get the result
5. Output the result
6. End
Flowchart:
plaintext
Copy code
Start
|
Input n
|
If n == 0 --> Return 0
|
If n == 1 --> Return 1
|
Return fibonacci(n-1) + fibonacci(n-2)
|
Output result
|
End
C++ Code:
cpp
Copy code
#include <iostream>
using namespace std;
int fibonacci(int n) {
if (n == 0) return 0;
else if (n == 1) return 1;
else return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
cout << "Enter number of terms: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << fibonacci(i) << " ";
}
return 0;
}
Sample Output:
mathematica
Copy code
Enter number of terms: 10
0 1 1 2 3 5 8 13 21 34
5. Program to Find GCD of Two Numbers Using
Euclidean Algorithm
Algorithm:
1. Start
2. Input two numbers a and b
3. Repeat until b == 0:
o Set temp = b
o Set b = a % b
oSet a = temp
4. Output a as the GCD
5. End
Flowchart:
plaintext
Copy code
Start
|
Input a, b
|
While b != 0
|
temp = b
|
b=a%b
|
a = temp
|
End While
|
Output GCD = a
|
End
C++ Code:
cpp
Copy code
#include <iostream>
using namespace std;
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "GCD of " << a << " and " << b << " is " << gcd(a,
b) << endl;
return 0;
}
Sample Output:
mathematica
Copy code
Enter two numbers: 48 18
GCD of 48 and 18 is 6
6. Program to Convert Decimal to Binary
Algorithm:
1. Start
2. Input n
3. Initialize an empty string binary
4. Repeat while n > 0:
o Set binary = (n % 2) + binary
o Set n = n / 2
5. Output binary
6. End
Flowchart:
plaintext
Copy code
Start
|
Input n
|
While n > 0
|
binary = (n % 2) + binary
|
n=n/2
|
End While
|
Output binary
|
End
C++ Code:
cpp
Copy code
#include <iostream>
using namespace std;
string decimalToBinary(int n) {
string binary = "";
while (n > 0) {
binary = to_string(n % 2) + binary;
n /= 2;
}
return binary;
}
int main() {
int n;
cout << "Enter a decimal number: ";
cin >> n;
cout << "Binary representation of " << n << " is " <<
decimalToBinary(n) << endl;
return 0;
}
Sample Output:
vbnet
Copy code
Enter a decimal number: 10
Binary representation of 10 is 1010
7. Program to Check Prime Number
Algorithm:
1. Start
2. Input n
3. Set flag = true
4. If n <= 1, set flag = false
5. For i = 2 to sqrt(n):
o If n % i == 0, set flag = false
6. If flag == true, output "Prime", else output "Not Prime"
7. End
Flowchart:
plaintext
Copy code
Start
|
Input n
|
Is n <= 1?
|
Yes --> Output "Not Prime"
|
No --> For i = 2 to sqrt(n)
|
If n % i == 0 --> Output "Not Prime"
|
Else Output "Prime"
|
End
C++ Code:
cpp
Copy code
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
if (isPrime(n)) {
cout << n << " is a prime number." << endl;
} else {
cout << n << " is not a prime number." << endl;
}
return 0;
}
Sample Output:
less
Copy code
Enter a number: 17
17 is a prime number.
8. Program to Implement Selection Sort
Algorithm:
1. Start
2. Input the array and its size
3. Repeat for each element in the array:
o Find the minimum element
o Swap it with the first unsorted element
4. Output the sorted array
5. End
Flowchart:
plaintext
Copy code
Start
|
Input array, n
|
For i = 0 to n-1
|
Find minimum element
|
Swap with array[i]
|
End For
|
Output sorted array
|
End
C++ Code:
cpp
Copy code
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
Sample Output:
c
Copy code
Sorted array: 11 12 22 25 64
9. Program to Find LCM of Two Numbers
Algorithm:
1. Start
2. Input a and b
3. Find the GCD of a and b
4. Calculate LCM = (a * b) / GCD(a, b)
5. Output the result
6. End
Flowchart:
plaintext
Copy code
Start
|
Input a, b
|
Find GCD(a, b)
|
LCM = (a * b) / GCD
|
Output LCM
|
End
C++ Code:
cpp
Copy code
#include <iostream>
using namespace std;
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "LCM of " << a << " and " << b << " is " << lcm(a,
b) << endl;
return 0;
}
Sample Output:
mathematica
Copy code
Enter two numbers: 12 18
LCM of 12 and 18 is 36
10. Program to Reverse a String
Algorithm:
1. Start
2. Input a string
3. Loop from start to half of the string:
o Swap the characters at opposite ends
4. Output the reversed string
5. End
Flowchart:
plaintext
Copy code
Start
|
Input string
|
For i = 0 to len/2
|
Swap string[i] with string[len-i-1]
|
End For
|
Output reversed string
|
End
C++ Code:
cpp
Copy code
#include <iostream>
#include <string>
using namespace std;
void reverseString(string &str) {
int n = str.length();
for (int i = 0; i < n / 2; i++) {
swap(str[i], str[n - i - 1]);
}
}
int main() {
string str;
cout << "Enter a string: ";
cin >> str;
reverseString(str);
cout << "Reversed string: " << str << endl;
return 0;
}
Sample Output:
c
Copy code
Enter a string: hello
Reversed string: olleh
Program 11: Find the Greatest of Three Numbers
Algorithm:
1. Start
2. Input three numbers: a, b, c
3. Compare a, b, and c
o If a > b and a > c, then a is the greatest
o If b > a and b > c, then b is the greatest
o Else, c is the greatest
4. Display the greatest number
5. Stop
Flowchart:
plaintext
Copy code
Start
|
Input a, b, c
|
Is a > b and a > c?
/ | \
Yes No Check b > c?
/ \ / \
a is G Yes No b is G c is G
| | |
Display greatest number
|
Stop
C++ Code:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
if (a > b && a > c) {
cout << "Greatest is: " << a << endl;
} else if (b > a && b > c) {
cout << "Greatest is: " << b << endl;
} else {
cout << "Greatest is: " << c << endl;
}
return 0;
}
Output:
plaintext
Copy code
Enter three numbers: 45 67 23
Greatest is: 67
Program 12: Check If a Number is Prime
Algorithm:
1. Start
2. Input number n
3. Initialize flag = 1
4. Check divisibility of n from 2 to n/2
o If n is divisible by any number, set flag = 0
5. If flag = 1, print "Prime"; otherwise, print "Not Prime"
6. Stop
Flowchart:
plaintext
Copy code
Start
|
Input n
|
Set flag = 1
|
Check divisibility from 2 to n/2
/ | \
Yes No Set flag = 0
| |
Not Prime Prime
|
Stop
C++ Code:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int n, flag = 1;
cout << "Enter a number: ";
cin >> n;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
flag = 0;
break;
}
}
if (n == 1)
flag = 0;
if (flag == 1)
cout << n << " is Prime" << endl;
else
cout << n << " is Not Prime" << endl;
return 0;
}
Output:
plaintext
Copy code
Enter a number: 29
29 is Prime
Program 13: Factorial of a Number
Algorithm:
1. Start
2. Input number n
3. Initialize fact = 1
4. Multiply fact by each number from 1 to n
5. Display fact
6. Stop
Flowchart:
plaintext
Copy code
Start
|
Input n
|
Set fact = 1
|
Multiply fact by i from 1 to n
|
Display fact
|
Stop
C++ Code:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int n, fact = 1;
cout << "Enter a number: ";
cin >> n;
for (int i = 1; i <= n; i++) {
fact *= i;
}
cout << "Factorial of " << n << " is " << fact << endl;
return 0;
}
Output:
plaintext
Copy code
Enter a number: 5
Factorial of 5 is 120
Program14: Fibonacci Series Up to N Terms
Algorithm:
1. Start
2. Input number n
3. Initialize a = 0, b = 1
4. Print first n Fibonacci numbers
5. Stop
Flowchart:
plaintext
Copy code
Start
|
Input n
|
Set a = 0, b = 1
|
Print Fibonacci series up to n
|
Stop
C++ Code:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int n, a = 0, b = 1, next;
cout << "Enter number of terms: ";
cin >> n;
cout << "Fibonacci Series: " << a << " " << b << " ";
for (int i = 2; i < n; ++i) {
next = a + b;
cout << next << " ";
a = b;
b = next;
}
cout << endl;
return 0;
}
Output:
plaintext
Copy code
Enter number of terms: 6
Fibonacci Series: 0 1 1 2 3 5
Program 15: Sum of Digits of a Number
Algorithm:
1. Start
2. Input number n
3. Initialize sum = 0
4. Extract each digit of n and add it to sum
5. Display sum
6. Stop
Flowchart:
plaintext
Copy code
Start
|
Input n
|
Set sum = 0
|
Extract digit and add to sum
|
Is n > 0? -> No
|
Display sum
|
Stop
C++ Code:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int n, sum = 0, digit;
cout << "Enter a number: ";
cin >> n;
while (n > 0) {
digit = n % 10;
sum += digit;
n /= 10;
}
cout << "Sum of digits is: " << sum << endl;
return 0;
}
Output:
plaintext
Copy code
Enter a number: 456
Sum of digits is: 15