Samantha wants to develop a program to organize a set of numbers in an array. The program should accept the array size (n
) and n
integers as input, categorize them as even and odd, and display the grouped numbers.
It should contain a class called NumberClassifier
with attributes including n
(array size) and an array to store input numbers.
Input format:
The first line consists of an integer n
, representing the size of the array.
The second line consists of n
space-separated integers representing the array elements.
Output format:
The first line prints:
Even numbers:
followed by the even numbers from the given array, separated by a space.
The second line prints:
Odd numbers:
followed by the odd numbers from the given array, separated by a space.
Solution:
class NumberClassifier {
int n;
public void classifyNumbers(int[] arr) {
System.out.print("Even numbers: ");
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
System.out.print(arr[i] + " ");
}
}
System.out.println();
}
public void printEvenNumbers(int[] arr) {
// Even numbers are printed in classifyNumbers
}
public void printOddNumbers(int[] arr) {
System.out.print("Odd numbers: ");
for (int i = 0; i < n; i++) {
if (arr[i] % 2 != 0) {
System.out.print(arr[i] + " ");
}
}
System.out.println();
}
}
Arun is working on a project to convert seconds to a time format. He wants to create a program that accepts a time duration in seconds and converts it into hours, minutes, and seconds.
Help him write a logic under class SecondsToTime
with a constructor that gets input in seconds and converts it into hh:mm:ss
format.
Input format:
The input consists of a single integer, representing the time duration in seconds.
Output format:
The output prints the converted time in the format hh:mm:ss
where hh
represents hours, mm
represents minutes, and ss
represents seconds.
Solution:
class SecondsToTime {
int hours, minutes, seconds;
public SecondsToTime(int totalSeconds) {
this.hours = totalSeconds / 3600;
totalSeconds %= 3600;
this.minutes = totalSeconds / 60;
this.seconds = totalSeconds % 60;
}
public void displayTime() {
System.out.printf("%02d:%02d:%02d\n", hours, minutes, seconds);
}
}