[Solved] Art Competition with Java

Ocean Awareness Art Competition is a yearly art competition that engages students to promote the need to preserve, protect, and restore the world’s oceans and aquatic resources.  The Contest was organized at an outdoor auditorium in the City and large mass of school kids is expected to participate.

Every student participating in the contest will be registered at the venue entrance and be provided with an enrollment Id. To ensure the safety of the students, the Event organizers decided to make a disciplined seating arrangement by sticking the students’ enrollment Id on the chairs. There were N enrollments approximately expected for the contest and based on this, the event organizers wanted to print the enrollment numbers in white sheets to stick to the chairs.

You are to help the organizers in taking the printed formats of the enrollment numbers. Write a recursive method to print the enrollment numbers from 1 to N without the help of Loops.

Hence create a class named EnrollNumbers with the following method.

Method NameDescription
void printNumbers(int)                                                                  This recursive method should display the enrollment numbers from 1 to N without using loops.

Create a driver class called Main. In the Main method, obtain input from the user in the console and call the printNumbers method present in the EnrollNumbers class.

[Note: Strictly adhere to the Object Oriented Specifications given in the problem statement.

All class names, attribute names and method names should be the same as specified in the problem statement.]
Input Format:
First line of the input is an integer N. (1≤ N ≤ 50).

Output Format:
Output the enrollment numbers from 1 to N without using loops.
Refer sample input and output for formatting specifications.

Sample Input 1:
4

Sample Output 1:
1 2 3 4

Sample Input 2:
1

Sample Output 2:
1

Solution

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        printNumbers(a);
        
    }
    
    public static void printNumbers (int n)
    {
        if(n==0)
        return  ;
        printNumbers(n-1);
        System.out.print(n+" ");
        
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *