This blog explains about Soft Suave Interview Programs with Answers and is explained below :
Accepts three inputs from the user using the Scanner class:
limit: the number to compare the final sum with
start: the starting index for Fibonacci series
end: the ending index for Fibonacci series
Answer: It takes three inputs using the Scanner
class:
start
: the starting index for the Fibonacci seriesend
: the ending index for the Fibonacci serieslimit
: a number to compare the final sum with
Then it computes the sum of the Fibonacci numbers from index start
to end
(inclusive), and compares the sum with limit
.
import java.util.Scanner;
public class FibonacciSumComparison {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Accept input
System.out.print("Enter starting index: ");
int start = scanner.nextInt();
System.out.print("Enter ending index: ");
int end = scanner.nextInt();
System.out.print("Enter limit to compare the sum with: ");
int limit = scanner.nextInt();
// Calculate Fibonacci sum from start to end
int sum = 0;
for (int i = 0; i <= end; i++) {
int fib = fibonacci(i);
if (i >= start) {
sum += fib;
}
}
// Output the result
System.out.println("Sum of Fibonacci numbers from index " + start + " to " + end + " is: " + sum);
if (sum > limit) {
System.out.println("The sum is greater than the limit.");
} else if (sum < limit) {
System.out.println("The sum is less than the limit.");
} else {
System.out.println("The sum is equal to the limit.");
}
scanner.close();
}
// Helper method to calculate Fibonacci number at position n
public static int fibonacci(int n) {
if (n <= 1) return n;
int a = 0, b = 1, temp;
for (int i = 2; i <= n; i++) {
temp = a + b;
a = b;
b = temp;
}
return b;
}
}
Enter starting index: 2
Enter ending index: 5
Enter limit to compare the sum with: 10
OUTPUT:-
Sum of Fibonacci numbers from index 2 to 5 is: 12
The sum is greater than the limit.
Java Interview Questions – 2 Generates Fibonacci numbers from the start to end index (inclusive).
- Accepts input using
Scanner
- Generates Fibonacci numbers from the
start
toend
index (inclusive) - Compares their sum with a given
limit
import java.util.Scanner;
public class FibonacciRange {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input: start, end, and limit
System.out.print("Enter starting index: ");
int start = scanner.nextInt();
System.out.print("Enter ending index: ");
int end = scanner.nextInt();
System.out.print("Enter the limit to compare the sum with: ");
int limit = scanner.nextInt();
// Edge case: if start > end
if (start > end || start < 0) {
System.out.println("Invalid input: make sure start <= end and start >= 0.");
return;
}
int sum = 0;
System.out.println("Fibonacci numbers from index " + start + " to " + end + ":");
// Generate Fibonacci numbers from 0 to end, but only print and sum from start
for (int i = 0; i <= end; i++) {
int fib = fibonacci(i);
if (i >= start) {
System.out.print(fib + " ");
sum += fib;
}
}
System.out.println("\nSum = " + sum);
// Compare with limit
if (sum > limit) {
System.out.println("The sum is greater than the limit.");
} else if (sum < limit) {
System.out.println("The sum is less than the limit.");
} else {
System.out.println("The sum is equal to the limit.");
}
scanner.close();
}
// Method to compute nth Fibonacci number
public static int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
int a = 0, b = 1, fib = 0;
for (int i = 2; i <= n; i++) {
fib = a + b;
a = b;
b = fib;
}
return fib;
}
}
Enter starting index: 3
Enter ending index: 7
Enter the limit to compare the sum with: 50
Output:-
Fibonacci numbers from index 3 to 7:
2 3 5 8 13
Sum = 31
The sum is less than the limit.
From these Fibonacci numbers:
Fibonacci numbers from index 3 to 7:
2 3 5 8 13
Sum = 31
Step 1: Find the prime numbers
We check each for primality:
- 2 → Prime
- 3 → Prime
- 5 → Prime
- 8 → Not prime
- 13 → Prime
So the prime Fibonacci numbers are:2, 3, 5, 13
Step 2: Sum of prime Fibonacci numbers
2 + 3 + 5 + 13 = 23
Final Output:-
Prime Fibonacci numbers: 2 3 5 13
Sum of prime Fibonacci numbers: 23
Finally:
If the sum is less than the limit, print “NO”.
- Accepts
start
,end
, andlimit
- Generates Fibonacci numbers from index
start
toend
- Identifies prime Fibonacci numbers in that range
- Calculates their sum
- Prints “NO” if the prime sum is less than the limit
import java.util.Scanner;
public class FibonacciPrimeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input
System.out.print("Enter starting index: ");
int start = scanner.nextInt();
System.out.print("Enter ending index: ");
int end = scanner.nextInt();
System.out.print("Enter the limit to compare the prime sum with: ");
int limit = scanner.nextInt();
if (start > end || start < 0) {
System.out.println("Invalid input: start should be <= end and >= 0.");
return;
}
int primeSum = 0;
System.out.println("Fibonacci numbers from index " + start + " to " + end + ":");
for (int i = start; i <= end; i++) {
int fib = fibonacci(i);
System.out.print(fib + " ");
if (isPrime(fib)) {
primeSum += fib;
}
}
System.out.println("\nSum of prime Fibonacci numbers: " + primeSum);
// Final condition check
if (primeSum < limit) {
System.out.println("NO");
}
scanner.close();
}
// Fibonacci generator
public static int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
int a = 0, b = 1, fib = 0;
for (int i = 2; i <= n; i++) {
fib = a + b;
a = b;
b = fib;
}
return fib;
}
// Prime checker
public static boolean isPrime(int num) {
if (num <= 1) return false;
if (num == 2) return true;
if (num % 2 == 0) return false;
for (int i = 3; i <= Math.sqrt(num); i += 2) {
if (num % i == 0) return false;
}
return true;
}
}
start = 3
end = 7
limit = 30
Fibonacci numbers: 2, 3, 5, 8, 13
Prime Fibonacci numbers: 2, 3, 5, 13
Sum of primes: 23
Result: "NO"
(since 23 < 30)
Conclusion:-
Accepts three inputs from the user:
start
— starting index of the Fibonacci seriesend
— ending indexlimit
— a number to compare the sum with
It generates Fibonacci numbers from start
to end
index (inclusive).
Identifies which of those are prime.
Calculates the sum of the prime Fibonacci numbers.
Finally, it prints “NO” if that sum is less than the limit.