Fibonacci
Fibonacci
Problem 01:
Write a program to display nth term of Fibonacci series of n terms.
Input Output
Enter Any Number: 9 Fibonacci Number: 34
Enter Any Number: 13 Fibonacci Number: 233
Solu on:
#include <stdio.h>
void fibonacci(int n) {
int fib[n + 2];
fib[0] = 0;
fib[1] = 1;
int main() {
int n;
fibonacci(n);
return 0;
}
Output:
1
2
Problem 02:
Are you know the Fibonacci series? If yes, given a posi ve value with t. Then
find the solu on & also print it.
Input Output
Test Case: 3 Result:
Number 1: 1 Fibonacci :1
Number 2: 2 Fibonacci :1
Number 3: 5 Fibonacci :5
Solu on:
#include <stdio.h>
void fibonacci(int n) {
int fib[n + 2];
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i <= n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
prin ("\nFibonacci: %d",fib[n]);
}
int main() {
int t,i;
for(i=0;i<t;i++){
prin ("Number %d: ",i+1);
scanf("%d",&arr[i]);
}
prin ("\nResult: ");
for(i=0;i<t;i++){
fibonacci(arr[i]);
}
return 0;
}
Output:
Coin change
Problem 01:
Suppose you went to a market, then you gave 15 tk note to the sales person,
and he returned some of the currency (1, 7, 7, 10S) that he had in abundance.
Then write the code which is find op mal solu on.
Input Output
Coin [] = {1,7,7,10} Coin Need:
Change Amount: 15 10 coin 1 mes
4
1 coin 5 mes
Solu on:
Output:
Problem 02:
Suppose, you have coin changing machine. Which has huge amount of coin.
Now if any one gave some money then it is return some of coins. Follow the
above approach and find the which coin could be needed and how many
mes?
Input Output
Enter Number of Coin: 5 Coin Need:
Coin [] = {2,5,3,4,6} 6 coin 2 mes
Change Amount: 12
Solu on:
Output:
LCS
Problem 01:
Suppose, you are working in medical office. Then you have two DNA sample.
Now, your job is finding out longest common sequence of DNA.
Input Output
First Sequence = abcbdab DNA Most Match = bcba
Second Sequence = bdcaba
Solu on:
5
Output:
Problem 02:
In a forensic lab, a DNA specialist found two DNA sample of two animal. Then
he is matching both them and want to find out the result. Now you write the
program.
Input Output
First Sequence = catcat DNA Most Match = tcat
Second Sequence = tgrcat
Solu on:
Output:
LIS
Problem 01:
Suppose, you have some of random data, Then your job is finding out the
longest increasing sequence of data.
Input Output
Data size: 10 LIS = 2 3 7 8 10 13
Data [] = {9,2,5,3,7,11,8,10,13,6}
Solu on:
6
#include <stdio.h>
#include <stdlib.h>
void printLIS(int arr[], int n, int *lis_dp, int *prev_dp, int idx) {
if (idx == -1) {
return;
}
printLIS(arr, n, lis_dp, prev_dp, prev_dp[idx]);
printf("%d ", arr[idx]);
}
printf("LIS: ");
7
free(lis_dp);
free(prev_dp);
}
int main() {
int n;
printf("Data size: ");
scanf("%d", &n);
printf("Data [] = ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
lis(arr, n);
free(arr);
return 0;
}
Output:
8
9
0/1 knapsack
Problem 01:
Suppose, you go to a market with a knapsack, which maximum capacity is 5kg.
There four items with weight and value. Then find the maximum profit and
print it.
Item Weight Value
Gold 2 12
Diamond 1 10
Pla num 3 20
Rubi 2 15
Input Output
Total Items: 4 Maximum Profit: 37
Weight [] = {2,1,3,2}
Value [] = {12,10,20,15}
Knapsack Weight: 5
Solu on:
#include <stdio.h>
return K[n][W];
}
int main() {
int n, W;
printf("Total Items: ");
scanf("%d", &n);
printf("\nWeight [] = ");
for (int i = 0; i < n; i++) {
scanf("%d", &wt[i]);
}
printf("\nValue [] = ");
for (int i = 0; i < n; i++) {
scanf("%d", &val[i]);
}
return 0;
}
Output:
11
Problem 02:
You are given some items with weight and value, put these items in a
knapsack of capacity 50 to get the maximum total value in the knapsack. Note
that we have only one quan ty of each item. Follow the above approach and
print the maximum profit.
Item Weight Value
1 100 20
2 50 10
3 150 30
Input Output
Total item: 3 Maximum Profit: 250
Weight [] = {20,10,30}
Value [] = {100,50,150}
Solu on:
#include <stdio.h>
return K[n][W];
}
int main() {
int n, W=50;
12
printf("\nWeight [] = ");
for (int i = 0; i < n; i++) {
scanf("%d", &wt[i]);
}
printf("\nValue [] = ");
for (int i = 0; i < n; i++) {
scanf("%d", &val[i]);
}
return 0;
}
Output: