0% found this document useful (0 votes)
25 views7 pages

Palindrome and Fibonacci Solutions

fggf

Uploaded by

arunkumarmak651
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)
25 views7 pages

Palindrome and Fibonacci Solutions

fggf

Uploaded by

arunkumarmak651
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

CODING ROUND (Palindrome)

SOLUTION SET:
1. Given an integer x, return true if x is a palindrome, and false
otherwise.

Input:x=123454321
Output:x=True
Explanation: 123454321 reads as 123454321 from left to right and
from right to left.
CODE IN C:
#include <stdio.h>
#include <string.h>
// Function to check if a string is palindrome
int isPalindrome(char str[]) {
int left = 0;
int right = strlen(str) - 1;
while (right > left) {
if (str[left++] != str[right--])
return 0; // Not a palindrome
}
return 1; // Palindrome
}

int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

// Removing newline character


if (str[strlen(str) - 1] == '\n')
str[strlen(str) - 1] = '\0';

if (isPalindrome(str))
printf("%s is a palindrome.\n", str);
else
printf("%s is not a palindrome.\n", str);

return 0;
}
CODE IN JAVA:
import java.util.Scanner;

public class PalindromeChecker {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = scanner.nextLine();

if (isPalindrome(str))
System.out.println(str + " is a palindrome.");
else
System.out.println(str + " is not a palindrome.");
scanner.close();
}

public static boolean isPalindrome(String str) {


int left = 0;
int right = str.length() - 1;

while (right > left) {


if (str.charAt(left++) != str.charAt(right--))
return false; // Not a palindrome
}
return true; // Palindrome
}
}
CODE IN PYTHON:
def is_palindrome(s):
# Removing non-alphanumeric characters and converting to lowercase
s = ''.join(c.lower() for c in s if c.isalnum())
return s == s[::-1]

# Test the function


s = input("Enter a string: ")
if is_palindrome(s):
print(s, "is a palindrome.")
else:
print(s, "is not a palindrome.")
OUTPUT:
123454321

CODING ROUND(FIBONACCI SERIES)


1.Write a generator function that returns a generator object which yields
theFibonacci sequence
The Fibonacci sequence is defined by the relation Xn-1+Xn-2
The first few numbers of the series are 0,1,1,2,3,5,8,30

Input:callcount=10
Output:[0 1 1 2 3 5 8 13 21 34]
CODE IN C:
#include <stdio.h>

// Function to generate Fibonacci series


void fibonacci(int n) {
int first = 0, second = 1, next, i;

printf("Fibonacci Series up to %d terms:\n", n);

for (i = 0; i < 20; i++) {


if (i <= 1)
next = i;
else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
}

int main() {
int n;

printf("Enter the number of terms: ");


scanf("%d", &n);

fibonacci(n);

return 0;
}
CODE IN JAVA:
import java.util.Scanner;

public class FibonacciSeries {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = scanner.nextInt();

System.out.println("Fibonacci Series up to " + n + " terms:");


for (int i = 0; i <20; i++) {
System.out.print(fibonacci(i) + " ");
}
scanner.close();
}

public static int fibonacci(int n) {


if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
CODE IN PYTHON:
def fibonacci(n):
fib_series = []
a, b = 0, 1
for _ in range(20):
fib_series.append(a)
a, b = b, a + b
return fib_series

n = int(input("Enter the number of terms: "))


print("Fibonacci Series up to", n, "terms:")
print(fibonacci(n))

OUTPUT:
0 1 1 2 3 5 8 13 21 34

You might also like