0% found this document useful (0 votes)
12 views43 pages

Programming Challenges for Beginners

The document contains a series of programming tasks that require writing code to perform various operations such as counting factors, determining lucky ticket winners, reversing numbers, checking leap years, calculating factorials, and more. Each task includes input and output formats along with sample test cases to illustrate expected results. The tasks cover a wide range of programming concepts and algorithms suitable for practice and assessment.

Uploaded by

Shobana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views43 pages

Programming Challenges for Beginners

The document contains a series of programming tasks that require writing code to perform various operations such as counting factors, determining lucky ticket winners, reversing numbers, checking leap years, calculating factorials, and more. Each task includes input and output formats along with sample test cases to illustrate expected results. The tasks cover a wide range of programming concepts and algorithms suitable for practice and assessment.

Uploaded by

Shobana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. Write a program to accept an integer and count the factors of a number.

Input format :

The input consists of an integer n.

Output format :

The output prints the count of the factors of the number.

Sample test cases :

Input 1 :

25

Output 1 :

3
2. Create a program to determine if the last digit of a visitor's ticket is 3 or 8. The "Fantasy
Kingdom" amusement park held its opening ceremony, and the park administration
offered several lucky prizes to the guests on the first day. The attendees whose ticket
numbers have the last digit of 3 or 8 are therefore determined to be the lucky winners, and
appealing rewards are waiting to be presented.

Input format :

The input consists of an single integer.

Output format :

The output should display "Lucky Winner" if the last digit of the ticket number is 3 or 8. Otherwise,
print "Not a Lucky Winner."

Sample test cases :

Input 1 :

43

Output 1 :

Lucky Winner

Input 2 :

41

Output 2 :

Not a Lucky Winner


3. Write a program to reverse the given number.

Input format :

The input consists of the number.

Output format :

The output prints the reversed number.

Sample test cases :

Input 1 :

12458

Output 1 :

85421
4. Write a program to check whether the given year is leap year or not.

Note: Use nested if else statement.

Input format :

Input to get a year.

Output format :

Display input followed by Leap year or Not leap year.

Code constraints :

year (integer type).

Sample test cases :

Input 1 :

1789

Output 1 :

1789 Not leap year

Input 2 :

1880

Output 2 :

1880 Leap year


5. Write a program to find the factorial of a given number.

Input format :

The input consists of the number

Output format :

Factorial of the given number

Sample test cases :

Input 1 :

Output 1 :

24
6. Seven Segment Display

The event planning company "Buzzcraft" sought to buy seven segment screens for use in their events
to display any numerical information on display boards, scrolling ad banners, etc. The company
outsourced its order to Orange Labs' MDC team, which creates embedded sensing nodes and offers
connectivity to connect them to the internet of things. They are constructing displays with seven
segments. However, the company was curious about the number of seven-segment displays required
to depict an integer x. To represent one digit of an integer, they employ a single seven-segment
display. For instance: Three seven-segment boards are required to represent the integer 100. Help
them find out how many displays they need.

Input format :

The input consists of an integer.

Output format :

The output displays the number of digits of that integer.

Sample test cases :

Input 1 :

Output 1 :

Input 2 :

1000

Output 2 :

4
7. After attending maths class, Karthick found an interesting link between a number and its
proper divisors. The sum of proper divisors of a number is equal to that number itself. Using
his mathematical knowledge he found the smallest number is 6, which is the sum of 1, 2, and
3(1, 2 and 3 are proper divisors of 6). So he wants to check other numbers too. Help Karthick
by writing a program to simplify his work.

Input format :

The input consists of a number.

Output format :

Number followed by Perfect number / Not perfect number

Sample test cases :

Input 1 :

Output 1 :

6 Perfect number

Input 2 :

Output 2 :

9 Not perfect number


8. The Magic Castle, the home of the Academy of Magical Arts in California, has organized the
great 'Wonderworks Magic Show'. Renowned magicians were invited to mystify and thrill
the crowd with their spectacular magic tricks. The ticket booking for the show started two
days prior, and there were different types of tickets offered at different fares. The show
organizers wanted to place a scanning machine at the entrance of the venue for scrutiny.
The machine will take the input of a character denoting the various ticket types and display
the equivalent ticket type of the given character.

There are 5 types of tickets, each of which is denoted by a character (both uppercase and lowercase).
Please find the equivalent strings for the characters.

E or e - Early Bird Ticket

D or d - Discount Ticket

V or v - VIP Ticket

S or s - Standard Ticket

C or c - Children Ticket

Write a piece of code for the scanning machine that will take the input of a character and print the
equivalent string as given.

Input format :

The first line of the input is one of the characters that denotes one of the ticket types.

Output format :

Output should display the equivalent ticket type for the character.

Sample test cases :

Input 1 :

Output 1 :

Early Bird Ticket


9. Card Game

The Westland Game Fair is the premier event of its kind for kids interested in some intellectual and
cognitive brain games. Alan, a middle school boy is visiting the fair where he is very much drawn by
the Card game.

The game’s rules are:

A player needs to pick 3 cards from a big lot of cards. There are 4 types of Cards namely Spade(S),
Heart(H), Club(C) and Diamond (D). If all the 3 cards that the player picks are of the same type and
same number, they get a Double Bonanza. If all the 3 cards are of the same type or if they all have
the same number, they get a Bonanza. Otherwise, they do not get a Bonanza. Alan has now picked 3
cards and is awaiting to know if he has got a bonanza. Please help him to know if he has won the
Bonanza or not.

Input format :

There are 3 lines of input.

Each of the lines consists of character and integer input, which corresponds to the type of the card
and the number in it that Alan picked. The type of card and the number are separated by a single
space

Output format :

The output should display "Double Bonanza" or "Bonanza" or "No Bonanza" based on the conditions
given.

Sample test cases :

Input 1 :

S5

S5

S5

Output 1 :

Double Bonanza

Input 2 :

S6

H5

S5

Output 2 :

No Bonanza
10. Given an integer N as input, which represents the number of electricity units consumed.

Calculate the total electricity bill according to the given conditions:

 For the first 50 units $ 0.50/unit

 For the next 100 units $ 0.75/unit

 For the next 100 units $ 1.20/unit

 For units above 250 $ 1.50/unit

An additional surcharge of 20% is added to the bill.

Input format :

The input consists of an integer that represents the units consumed.

Output format :

The output prints the total charge.

Note: Round off the output to two decimal places.

Sample test cases :

Input 1 :

225

Output 1 :

228.00

Input 2 :

300

Output 2 :

354.00

Input 3 :

150

Output 3 :

120.00
[Link] machines in Sunrise Logistics is not working. Raju, the manager of the division

wants to calculate the total weight of received goods. Weight is printed in the goods label.

Write a suitable code to help Raju.

Input format :

Number of received goods in first line.

Weight of goods in Second line (Space separated).

Output format :

The output prints the total weight.

Sample test cases :

Input 1 :

10

1928374686

Output 1 :

54
12. Camel case

Camel case (stylized as camelCase or CamelCase) is the practice of writing compound words or

phrases such that each word or abbreviation in the middle of the phrase begins with a capital letter,

with no intervening spaces or punctuation. Event names should be entered in camel case format.

But many users failed to follow this convention. To maintain uniformity, you have to change all the

event names into camel case. Write a program to convert event names to camel case format.

Create a driver class called Main. In the Main method, obtain the inputs from the console and print

the names of the events in camel case.

Input format :

Input consist of the event name

Output format :

Output prints every first letter of the word in uppercase

Sample test cases :

Input 1 :

book sale

Output 1 :

BookSale

Input 2 :

welcome to 007

Output 2 :

WelcomeTo007
13. Find largest and smallest number in an array.

Input format :

Input consists of n+1 integer inputs.

First line of the input describes the array size 'n',

Followed by n number of array elements.

Output format :

Output displays the smallest and largest number in the array.

Sample test cases :

Input 1 :

12 4 2 5 22

Output 1 :

smallest value: 2

largest value: 22

Input 2 :

20 30 50 4 71 100

Output 2 :

smallest value: 4

largest value: 100


14. Write a program to obtain a matrix and find the sum of the elements in the upper triangular

matrix(i.e., the elements on the diagonal and the upper elements).

Note: Only square matrix

Input format :

The first line of the input consists of the number of rows and columns.

The next input is the matrix.

Output format :

The output prints the sum of the upper triangular matrix.

Sample test cases :

Input 1 :

33

654

125

797

Output 1 :

29

Input 2 :

33

12 23 45

56 78 89

95 51 20

Output 2 :

267
15. Write a program to count the vowels in the string.

Input format :

First line to get the input string S.

Output format :

Displays the number of vowels

Code constraints :

S > 0

Sample test cases :

Input 1 :

string

Output 1 :

1
16. Write a program to display the middle value of the string. If the string is odd then display the

single element in the middle, if even then display two elements from the middle.

Input format :

Input to get a string.

Output format :

Display the middle element as shown in the sample output.

Sample test cases :

Input 1 :

carry

Output 1 :

Input 2 :

lemons

Output 2 :

mo

Input 3 :

-8

Output 3 :

-8

Input 4 :

###111

Output 4 :

#1
17. Write a program to find all pairs of elements in an array whose sum is equal to the given

value. Help Guru to write a program to complete this task.

Input format :

Required Sum in first line.

Number of array elements in the second line.

Array elements in third line separated by space.

Output format :

Number pair with sum as shown in the sample output.

Sample test cases :

Input 1 :

30

14 -15 9 16 25 45 12 8

Output 1 :

14 + 16 = 30

-15 + 45 = 30
18. Write a program to find the first and last occurrence of an element in a sorted array.

Input format :

The first line of the input consists of the value n.

Next input is the array elements.

The last input is the element.

Output format :

The output prints the first and last occurrence of the element separated by a space.

Sample test cases :

Input 1 :

1 3 5 5 5 5 67 123 125

Output 1 :

25

Input 2 :

1 3 5 5 5 5 7 123 125

Output 2 :

66
19 Write a program to obtain a matrix and find the sum of its diagonal elements.

Note: Only square matrix.

Input format :

The input consists of the number of rows and columns separated by a space.

The next input is the matrix.

Output format :

The output prints the sum of diagonal elements.

Sample test cases :

Input 1 :

33

123

456

789

Output 1 :

15

Input 2 :

44

12 23 45 56

78 89 98 87

65 54 32 21

14 25 36 58

Output 2 :

191
20. Write a Java program to Create a class named Player with the following member
variables/attributes

Create another class named Main and write a main method to test the above class.

Input Format

The first line consists of a string that represents the name

The second line consists of a string that represents the country

The third line consists of a string that represents the skill

Output Format

The Output Should Display the Player Details

Sample Input

Dhoni

India

Wicket-Keeper

Sample Output

Player Details

Player Name: Dhoni

Country Name: India

Skill : Wicket-Keeper

Sample Input Sample Output

Virat Kholi

India

Batsman

Player Details

Player Name: Virat Kholi

Country Name: India

Skill : Batsman
21 Write a program to check whether the given character is vowel or consonant.

Create two methods namely main method and alph. Create an object in the main method and access
the alph method, that performs the above operation.

Input Format

Input to get a character.

Output Format

The output prints whether the character is a vowel or consonant.

Display the output as shown in the sample output.


21. Create a class with two methods one to read the elements of an array and the other to find
all pairs of elements in an array whose sum is equal to a specified number.

Input Format

The first line of the input consists of the value of n.

Next input is the array elements.

The last input is the sum value.

Output Format

The output prints the pair whose sum is equal to a specified number.
22. Write a Java program to find the count of all digits of a number using class.

In this program, we will read a positive integer number and then calculate the count of all digits using
a class.

Input Format

The input consists of a number.

Output Format

The output prints the count of all digits in the number.

Sample Input

12345

Sample Output

Count of all digits: 5

Sample Input

22

Sample Output

Count of all digits: 2


23. Write a Java program to Create a class named Innings with the following public member
variables / Attributes

Include a method in the class named [Link] This method, display The details of The
innings in The format shown in The sample output. This method does not accept any arguments and
its return type is void.

Create another class named Main and write a main method to test the above class.

Input Format

The First line Consists of a String

The Second line consists of a String that represents battingTeam

The next Line consists of a Long that represents how many runs scored

Output Format

The Output Should display the InningsDetails

Sample Input

First Innings

CSK

190

Sample Output

Innings Details

Innings number: First Innings

Batting Team : CSK

Runs scored :190


24. Problem Statement:

Write a program to find the area of the wall. Create a class Wall with the following private attributes

length - double

height - double

Include parameterized constructor Wall(double length, double height) and a method


calculateArea() which returns the area of the wall. In the main method get the length and breadth of
two walls from the user.

Input Format

The first line of input consists of the length and breadth of wall1 separated by space.

The second line of input consists of the length and breadth of wall2 separated by space.

Output Format

The output prints the areas of wall1 and Wall2.

Refer to the sample input and output for formatting specifications.

Sample Input Sample Output

10.5 8.6

8.5 6.3

Area of Wall 1: 90.30

Area of Wall 2: 53.55


25. Given an unsorted array of unique integers in the range from 1

to N+1. Find the missing element in the array without sorting the

array. Develop a java program using classes and objects for the given problem statement.

Sample Input

15

4 6 5 7 3 1 2 9 8 10 12 15 14 13 16

Sample Output

11
26. . Build a Java program to display the day of a week.

Note: Create a constructor and perform the above task, the

object in main method should pass the value to the constructor.

Input Format

Input to get an integer N.

10. write java program to find the number of occurrences of a character in a string. Create a
constructor with two parameters, pass the value from the main method to the constructor, and
perform the mentioned task in the constructor and display it.

Input format

Input to get a string in the first line and a character in the second line.

Output format

Output the number of occurrences of a character in a string.

Sample testcases:

Input 1

utter

t
27. A company maintains a database that has the details of all the employees. There are two
levels of employees where level 1 is the top management having a salary of more than 100
dollars and level 2 is the staff who are getting a salary of less than 100 dollars. Create a class
named Employee with empId and salary as attributes. Create another class empLevel that
extends employee and categorizes the employee into various levels.

Whitelist: extends, class Employee(these words must be included in the code section)

Input Format

The input should contain only the employee id and salary of the employee separated by space.

Employee id should be of integer type and salary float type.

Output Format

The output of the program must display the employee id, salary, and level of the employee one
below the other in the same order.
28. Account Details:

Create a class ‘Account’ in which all the fields are declared private.

Attributes are as follows:

int account_number;

int account_balance;

Create another class user with username as an attribute and inherit the Account class for accessing
account_number and account_balance

Get n user details and store them in the private variables. The account number to which the deposit
must be made is then read. If an account number already exists, print the account balance;
otherwise, display "Account Number Does Not Exist."

Input Format

The first integer input represents n number of users.

Each user's account number and initial deposit amount are represented by the next n entries.

The last integer input represents the account number for which the balance needs to be displayed.

Output Format

Display the balance for the given account number.

Refer to the sample output.

Sample Input
3

100

50000

aravind

200

70000

michael

300

90000

viswa

200

Sample Output

70000
29. Employee Details:

Create a class called "Employee" in which the employee's name, ID, and salary are declared as
private attributes, and the company name is a static variable. The data types of the members are
as follows:

 String eName;

 int eId;

 int eSalary;

 String companyName ; // static variable

Note: Use the public setter and getter methods to set and read the value of the attributes.

Initial companyName = "ABC Corp"

Whitelist: static String, class Employee(these words must be present in the code section)

Input Format

The first input represents the number of employee details to be entered.

The next n inputs represent employee details with name, ID, and salary.

The last input represents the company name to be updated.

Output Format

Display the employee details entered by the user and also display the updated employee details.

Refer to the sample input and output for format specifications.

Sample Input

Sachin

40

90000

Dhoni

42

95000

XYZCorp

Sample Output

Employee Name: Sachin

Employee ID: 40

Employee Salary: 90000

Employee Company Name: ABC Corp


30. Function Overloading

An ice-cream vendor sells his ice-creams in cone(radius r and height h) and ball(radius r) shaped
containers. However, he is unsure of the quantity that can be filled in the two containers. You are
required to write a program in java that prints the volume of the containers given its respective
dimensions as input. Your class must be named ‘Icecream’ which has two methods with same name
‘Quantity’ each having the respective dimensions of the containers as the parameters.

Function Header:

public void Quantity(int r, int h)

public void Quantity(int r)

Input Format

If the quantity of the cone is to be calculated, the input must have the radius(r) and height(h) in the
same line separated by a space.

For calculating the quantity of the ball, the input must have its radius(r).

Note: Input type should be integer.

Output Format

The output must display the volume of the container rounded off to two decimal places for which
the dimensions are given.
31. Create a class named 'Hello'. Define a method 'sayHello'

Create an object obj.

Call method 'sayHello' without argument, Output should display 'Hello'.

Call method 'sayHello' with one argument, Output should display 'Hello 'argument value'' (Ex: If the
argument passed is 'John' Output should display 'Hello John')

Input Format

The input contains a string.

Output Format

The first line of the output should display 'Hello'.

The second line of the output should display 'Hello <input>'.


32. METHOD OVERLOADING USING TYPE CONVERSION

Create a class named 'Main'. Define a method 'print'

1. Create an object obj.

2. Call method 'print' with one argument in an Integer type, Output should display given
Integer.

3. Call method 'print' with one argument in a String type, Output should display the given
String.

4. Call method 'print' with one argument in a Boolean type, Output should display the given
Boolean.

Input Format

No console input.

Output Format

The first line of the output should display 35

The second line of the output should display 'Hello World

The third line of the output should display 24.35


33. Write two subclasses named Dog and Cat to override the method Animal.

Input Format

No console input.

Output Format

Print the String from subclass named Dog and Cat in seperate lines.
34. Create a parent class that consists of two methods m1 and m2.

m1 doesn't take any arguments and it just prints from parent.

m2 takes an integer value as parameter and prints the same.

Create a child class that extends parent class and override its methods.

m1 doesn't take any arguments and it just prints from child.

m2 takes an integer value as parameter and prints the same.

In the main class, create objects for the above classes and call the corresponding methods.

Input Format

The input consists of the integer value for both the classes separated by a space.

Output Format

The output displays the result. Refer sample output.

Constraints

integers only.
35. Write a Java program to compute the area of a rectangle using method overriding for
runtime polymorphism. The program should prompt the user to enter the length and
breadth of the rectangle, create an instance of the Rectangle class with the provided
dimensions, and call the calculateArea() method to compute and print the area of the
rectangle to the console.

Input Format

The program should prompt the user to enter the length and breadth of the rectangle on separate
lines.

Output Format

The program should print a single line to the console, which shows the area of the rectangle. The line
should be in the following format:

"Area of rectangle is: [area]"

where [area] is the calculated area of the rectangle, rounded to two decimal places.
36. Write a java program that creates an abstract class called "Shape". This class should have the
following methods:

abstract void rectangleArea();

abstract void squareArea();

abstract void circleArea();

Then, create a class called "Area" that extends the "Shape" class. This class should calculate and print
the area of all shapes.

Finally, create a Main class that gets the inputs and passes them to the appropriate methods.

Input Format

The first line of the input consists of the length and breadth.

The second line consists of the side.

The third line consists of the radius.

Output Format

The output prints the area of a rectangle, square, and circle.


37. Write a java program to create an interface called "ShapeCalculator" that has a method
called "calc(int n)".

Then, create two classes called "Square" and "Circle" that implement the "ShapeCalculator" interface
and implement the "calc(int n)" method.

Your program should calculate the area and perimeter of both squares and circles.

Input Format

The input to your program will be a single integer that represents the side of the square and the
radius of the circle.

Output Format

The output of your program should be the area and perimeter of each shape. The output should be
displayed in two lines. The first line should contain the area and perimeter of the square separated
by a space, and the second line should contain the details of the circle in a similar format.

Please note that the details of the square should be calculated using integer type while the details of
the circle should be calculated using double type
38. Given a Book class and the Main class, write a MyBook class that does the following:

Inherits from Book

Has a parameterized constructor taking these 3 parameters:

string title

string author

int price

Implements the Book class' abstract display() method so it prints the title, author, and price.

Input Format

The Main class creates a Book object and calls the MyBook class constructor (passing it the necessary
arguments). It then calls the display method on the Book object.

Output Format

The void display() method should print and label the respective title, author, and price of the
MyBook object's instance (with each value on its own line) .

Constraints

Strings and integers only.


39. Divide by zero exception.

Write a program to obtain two numbers and print their quotient. In case of exception print the same.

Input Format

Given a single line input separated by [Link] the Integer N1 and N2

Output Format

Display the quotient if there is no exception, else print the Exception message.

Constraints

Integers only.
40.

NullPointerException

Another prominent exception is NullPointerException. It occurs when you try to access a null value.
Assign the null value to a string and obtain an index position and try to access it. Print the exception.

Input Format

Input consists of an integer.

Output Format

Output prints the null pointer exception.


41.

Input Mismatch Exception

Input Mismatch exception occurs when an input of a different datatype is given other than the
required. In common practice, it occurs when a String or double datatype is given for an int datatype.
Let's handle this exception for practice. Obtain int type input from the user using the Scanner class.
Display the obtained input if no exception occurs. If an exception occurs, prompt the user as
specified in Sample I/O.

Create a driver class called Main. In the Main method, obtain integer input from the user and
perform actions as specified above.

Input Format

Input consists of a integer. If input is not an integer then print exception.

Output Format

Output prints the given input otherwise throws InputMismatchException.


42. ArrayIndexOutOfBoundsException:
The prominent exception which you will see is ArrayIndexOutOfBoundsException. It occurs when the program tries to
access the array beyond its size. As we know arrays have fixed sizes. So when you try to use an array beyond its size it
throws this exception. Let's try to handle this [Link] an Array of size N and get an index, then print the Array[index].
If the index is greater or equal to the array size(N), then print the Exception.

Divide by zero exception:

When you try to divide any number by Zero it will throw ArithmeticException: / by Zero. Get two numbers Then print the
quotient if the divisor is 0 then print the Exception.

NullPointerException:

Another prominent exception is NullPointerException. It occurs when you try to access a null value. Assign a null value to a
string and obtain an index position and try to access it. Print the exception.

Input Format

The first line consists of the array size(N).The second line consists of N integers separated by [Link] third line consists of
the Index value to retrieve the array [Link] fourth line consists of two integers(Dividend and Divisor) separated by
[Link] fifth line consists of an index value to get the character from the string.

Output Format

The first line consists of Array[Index] or [Link] second line consists of the result of division or
[Link] third line consists of String(Which is assigned to null value).The fourth line consists of
NullPointerException.

Refer to the sample input and output for formatting specifications.

Sample Input

12345

40

Sample Output

Array index out of bound.

[Link]: / by zero

null

[Link]

Sample Input

10 89 76 12

42

Sample Output

12

You might also like