The Program1
The Program1
Question 1:
Example 1:
Output:
Sum from 1 to 5:
12345
Algorithm
Final Result:
Java Code:
public class SumRecursion {
if (n == 1) {
System.out.println(1);
return;
sum(n - 1);
Example 1:
Input:
LEVEL
Output:
It is a palindrome word.
Example 2:-
Input:
HELLO
Output:
It is not a palindrome number
Algorithm
Java Code:
import java.util.Scanner;
// Base case: if the string is empty or has only one character, return the string
if (s.length() <= 1) {
return s;
} else {
// Recursive step: reverse the rest of the string and append the first character
return s.equals(reversedString);
}
if (isPalindrome(inputString)) {
} else {
scanner.close();
}
Variable Description Table
Ouestion 3:
Write a program by using recursive function that finds
and prints the tokens present in the given string.
Hint: Use String Tokenizer to display all the tokens of
the string.
Output:
Understanding
Computer
Science
Algorithm
Example: If the first token "Hello" was printed, the remaining string to process
might be "world! This is a sample string.".
Step 8: Termination
Once all tokens have been printed, the recursion ends and the program terminates.
Java Codes:
import java.util.StringTokenizer;
Ouestion 4:
Write a program to input a number. Use a recursive
function that finds and displays the factors of the
number.
Example 2:-
Input: 12
Output: The factors are:
1,2,3,4,6,12
Algorithm
Step 1: Start
Input the number n for which you want to find the factors.
If n % i == 0, then i is a factor of n.
o Print i as one of the factors of n.
Repeat Step 3, Step 4, and Step 5 incrementing i by 1 each time until i becomes
greater than n.
When i > n, the recursion stops, and all the factors have been printed.
Java Codes:
import java.util.Scanner;
if (i > num) {
return;
if (num % i == 0) {
findFactors(num, i + 1);
findFactors(number, 1);
}
Variable Description Table:
Question 5:
A company manufactures packing cartons in four sizes,
i.e. cartons to accommodate 6 boxes,
12 boxes, 24 boxes and 48 boxes. Design a program to
accept the number of boxes to be
packed (N) by the user (maximum up to 1000 boxes)
and display the break-up of the cartons
used in descending order of capacity (i.e. preference
should be given to the highest capacity
available, and if boxes left are less than 6, an extra
carton of capacity 6 should be used.)
Example:-
INPUT:
N = 726
OUTPUT:
48 * 15 = 720
6*1=6
Remaining boxes = 0
Total number of boxes = 726
Total number of cartons = 16
Algorithm
Step 1: Input the total number of boxes to be packed (N).
Step 2: Validate the input:
Step 4: Initialize an array cartonCount[] to store the number of cartons used for each size.
Step 5: Loop through each carton size (starting from largest to smallest):
Step 6: If there are leftover boxes, use one 6-box carton to pack the remaining boxes.
Java Codes:
import java.util.Scanner;
Example:-
INPUT:
N=3
Team 1: Emus
Team 2: Road Rols
Team 3: Coyote
OUTPUT:
ERC
moo
uay
sdo
t
Re
o
l
s
Algorithm
Step 1: Input the Number of Teams
Java Codes:
import java.util.Scanner;
int N = scanner.nextInt();
if (N < 3 || N > 8) {
return;
teams[i] = scanner.nextLine();
int maxLength = 0;
maxLength = team.length();
}
for (int i = 0; i < maxLength; i++) { // Loop through the longest name length
if (i < teams[j].length()) {
} else {
scanner.close();
Output the generated date and the future date after adding N days.
Java Codes:
import java.util.Scanner;
import java.time.LocalDate;
import
java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
public class DateCalculator {
try {
// Take day number and year as
input
System.out.print("Enter the day
number (1 to 366): ");
int dayNumber = scanner.nextInt();
System.out.print("Enter the year (4
digits): ");
int year = scanner.nextInt();
} catch (Exception e) {
System.out.println("Invalid input!
Please enter valid numbers.");
} finally {
scanner.close();
}
}
}
Algorithm
Step 1: Input N
Step 5: Output
Java Codes:
import java.util.Scanner;
import java.util.Arrays;
scanner.close();
}
}
Algorithm
Step 1: Accept Input
Step 2.1: If the sentence does not end with a valid punctuation mark, output "Invalid
sentence".
Step 2.2: Remove the last character (punctuation).
Step 2.3: Split the sentence into words and check if all words are uppercase and
separated by a single space.
Step 2.4: If invalid (e.g., contains lowercase or extra spaces), output "Invalid
sentence".
Step 3: Process Words
Java Codes:
import java.util.Scanner;
return false;
return true;
}
// Convert a non-palindrome word to a palindrome by appending its reverse (excluding last
character)
if (sentence.isEmpty() ||
return false;
return false;
}
return true;
if (!isValidSentence(sentence)) {
System.out.println("Invalid sentence.");
return;
if (isPalindrome(word)) {
} else {
convertedSentence.append(convertToPalindrome(word)); // Convert non-
palindrome words
convertedSentence.append(sentence.charAt(sentence.length() - 1));