0% found this document useful (0 votes)
16 views4 pages

Ap Exp 3.2 - Alasso

The document outlines two programming assignments, the first to check if a number is a palindrome in binary representation and the second to calculate the nth Fibonacci number recursively. It provides the aim, objectives, scripts, and outputs for both programs to solve checking palindromes and calculating Fibonacci sequences.

Uploaded by

Anshul Somani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Ap Exp 3.2 - Alasso

The document outlines two programming assignments, the first to check if a number is a palindrome in binary representation and the second to calculate the nth Fibonacci number recursively. It provides the aim, objectives, scripts, and outputs for both programs to solve checking palindromes and calculating Fibonacci sequences.

Uploaded by

Anshul Somani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 3.2
Student Name: Nikita UID: 21BCS2785
Branch:BE-CSE Section/Group: IoT-626 B
Semester:5 Date of Performance:05-10-2023
Subject Name: Advance Programming Lab
Subject Code: 21CSP-314

1. Aim:
1.WAP to check palindrome
2.WAP to solve Fibonacci sequence
2. Objective:
1. You are given a number . In one operation, you can either increase the value of by 1 or decrease
the value of by 1. Determine the minimum number of operations required (possibly zero) to convert
number to a number such that binary representation of is a palindrome. Note: A binary
representation is said to be a palindrome if it reads the same from left-right and right-left.
2. The Fibonacci sequence appears in nature all around us, in the arrangement of seeds in a
sunflower and the spiral of a nautilus for example. The Fibonacci sequence begins with and as its
first and second terms. After these first two elements, each subsequent element is equal to the sum
of the previous two elements. Programmatically:

3. Script and Output:


Program 1:-
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mp make_pair
#define pb push_back
#define pob pop_back()
#define mod 1000000007
#define max INT_MAX
#define min INT_MIN
#define fi first
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

#define se second
#define fast_cin() ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL)
set < int > v;
void binarypalindrome(int s, int e, int x) {
if (s > e) {
v.insert(x);
return;
}
binarypalindrome(s + 1, e - 1, x);
if (s == e)
binarypalindrome(s + 1, e - 1, x + pow(2, s));
else
binarypalindrome(s + 1, e - 1, x + pow(2, s) + pow(2, e));
return;
}
int main() {
fast_cin();
int n, t;
v.insert(0);
v.insert(1);
v.insert(3);
for (int i = 3; i < 32; i++) {
int c = pow(2, i - 1) + 1;
binarypalindrome(1, i - 2, c);
}
cin >> t;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

while (t--) {
cin >> n;
auto ptr = v.lower_bound(n);
auto ptr2 = ptr--;
if (abs(n - * ptr) < abs(n - * ptr2))
cout << abs(n - * ptr) << endl;
else
cout << abs(n - * ptr2) << endl;
}
return 0;
}
Output:-

Program 2:-
#include <iostream>

using namespace std;

int fibonacci(int n) {
if (n ==0)
return 0;
else if (n== 1)
return 1;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

else
return fibonacci(n-1) + fibonacci (n-2);

}
int main() {
int n;
cin >> n;
cout << fibonacci(n);
return 0;
}

Output:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy