Welcome To Java
Welcome To Java
(Part 1 - if else)
INDEX
INDEX 1
1. Welcome to Java 3
2. Java Stdin and Stdout 4
3. Sum and Difference of x and y 5
4. Area and Perimeter 6
5. Fahrenheit and Celsius 7
6. Add Last Digits 8
7. Greater than 100 9
8. Sum is less than 150 or not. 10
9. XYZW 11
10. Even_Or_Odd 12
11. Grace Marks 13
12. Basic Calculator 14
13. Print two-digit number 15
14. Toggle and 2 jumps left 16
15. Marks and Rank 17
16. Adult or not 18
17. High Sum or Low Sum 19
18. Grade the student 20
19. Shop Discount 21
20. Print Bonus 22
21. Print final salary 23
22. Print z and x divisible by 3 24
23. Print the oldest among three 25
24. Rich Adult Young 26
25. Print final z 27
26. Tell about x y 28
27. Print the final incremented salary 30
2
28. Top Management or not 31
29. Print final z given xyz 32
30. Small Capital or Digit 33
31. Add if a digit 34
32. Toggle the character 35
33. Print character at 3rd index 36
34. Concatenate_Two_Strings 37
35. string concatenate 2 38
Return to index
3
Welcome to Java
In this challenge, we practise printing to stdout. Welcome to the world of Java!
Input Format
Output Format
Sample Output
Hello World.
Hello Java.
Return to index
4
Java Stdin and Stdout
Most challenges require that you read input from stdin (standard input) and write output to stdout
(standard output). A popular method for reading input from stdin is by utilizing the Scanner class,
while specifying the Input Stream as System.in. For example:
The code above creates a Scanner object named and uses it to read a String and an int. It then
closes the Scanner object because there is no more input to read, and prints to stdout using
System.out.println(String). So, if our input is:
Hi 5
myString is: Hi
myInt is: 5
Task
In this challenge, you must read byte, short, int, long, float, double boolean char, string from stdin
and then print them to stdout as shown. Each must be printed on a new line.
Input Format
There are lines of input, and each line contains a single integer.
Sample Input Sample output
1 Byte: 1
2 Short: 2
12 Int: 12
12000000000 Long: 12000000000
1.2 Float: 1.2
1.324 Double: 1.324
J Char: J
Java String: Java
Return to index
5
Sum and Difference of x and y
First integer input should be stored in x, Second integer input should be stored in y. You will be
given two integers x and y. You have to print the sum of x and y in the first line, and the difference
of x and y in the second line.
Input Format
In the first line the value of x will be given and in the second line the value of y will be given.
Constraints
Output Format
Sum of x and y will be printed in the first line i.e x + y Difference of x and y will be printed in the
second line i.e x - y
Sample Input
40
10
Sample Output
50
30
Return to index
6
Area and Perimeter
Take the length and breadth of the rectangle as input.
Print the area of the rectangle in the first line and perimeter of the rectangle in the second line.
Input Format
Output Format
In the first line Area of the rectangle should be printed. In the second line the perimeter of the
rectangle should be printed.
Sample Input 0
10
20
Sample Output 0
200
60
Sample Input 1
20
30
Sample Output 1
600
100
Return to index
7
Fahrenheit and Celsius
You will be given Fahrenheit as input that should be stored in a double variable and print your
answer in Celsius of data-type double.
Input Format
Constraints
Output Format
For each test-case, you have to print Celsius in the double format.
Sample Input
32.0
Sample Output
0.0
Return to index
8
Add Last Digits
You will be given two numbers of int data-type as input, and you have to print the sum of their last
digits as output.
Expected Output: 13
Explanation: The last digit of 2357 is 7 and the last digit of 48986 is 6, and the sum of these last
digits is 13. Hence the output is 13.
Input Format
In the first line, the first number will be given as input. In the second line, the second number will
be given as input.
Constraints
Output Format
Sample Input
23456
9873
Sample Output
Explanation
Since the last digit of 23456 is 6, and the last digit of 9873 is 3. Therefore the sum is 6+3 or 9.
Return to index
9
Greater than 100
You will be given an integer as input, you have to print true if the number is greater than 100, and
false otherwise.
Input: 110
Output: true
Explanation: Since the given input is greater than 100, we printed true.
Input Format
Constraints
Output Format
Sample Input 0
120
Sample Output 0
true
Explanation 0
Sample Input 1
90
Sample Output 1
false
Explanation 1
Return to index
10
Sum is less than 150 or not.
You will be given three integer inputs x, y, z.
Print true if the sum is less than 150 and false otherwise.
Input Format
Output Format
Sample Input
20
30
50
Sample Output
true
Explanation
Sum of 20, 30, 50 is 100, since the sum is less than 150 so we print true.
Return to index
11
XYZW
You will be given four integer inputs x, y, z, w. Print true if x * y is equal to z * w and false
otherwise.
Input Format
For each test-case In the first you will get x as integer input. In the second you will get y as integer
input. In the third you will get z as integer input. In the fourth you will get w as integer input.
Output Format
Sample Input
5
8
10
4
Sample Output
true
Explanation
Return to index
12
Even_Or_Odd
Take an integer value as input.
Print "Even" if the number is even, print "Odd" if the number is odd.
Input Format
1. An integer value.
Output Format
Sample Input 0
98
Sample Output 0
Even
Sample Input 1
57
Sample Output 1
Odd
Return to index
13
Grace Marks
You are given the marks of a student as an integer input, the teacher gives 4 marks as grace marks
if the marks of the student is less than 33, otherwise no grace marks are given. Print the final
marks of the student.
Input Format
Output Format
Sample Input
20
Sample Output
24
Explanation
Since the marks of the student is below 33, we add 4 marks as the grace marks, so the final marks
is 24.
Return to index
14
Basic Calculator
You will be given two integers x,y in the integer data-type format and a character as an input in the
character data-type format,
1. If the entered character is ‘A’, then print the value of x + y in the integer format.
2. If the entered character is ‘S’, then print the value of x - y in the integer format.
3. If the entered character is ‘M’, then print the value of x * y in the integer format.
4. If the entered character is ‘D’, then print the value of x / y in the integer format.
5. If the entered character is ‘R’, then print the value of remainder when x is divided by y.
6. Else print the string “Enter again”.
Input Format
For each input, You will get the value of x as an integer data-type in the first line, You will get the
value of y as an integer data-type in the second line, You will get a character as a character
data-type in the third line.
Output Format
10 30
20
A
Explanation Entered character is 'A', we add the two integers and print the final sum as 30
50 -10
60
S
40 Enter again
30
k
Return to index
15
Print two-digit number
You will be given two digits x, y as character inputs, you need to form the two-digit number from it.
Print the final output based on the conditions given below,
1. If character x is zero, then print only the digit y in the integer data-type format.
2. Otherwise, print the number xy.
For example if x is ‘3’ and y is ‘8’, then print the number 38 in the integer data-type format.
Input Format
You will get the digit character x as a character data-type in the first line,
You will get the digit character y as a character data-type in the second line.
Constraints
Output Format
Sample Input 0
5
6
Sample Output 0
56
Sample Input 1
0
8
Sample Output 1
8
Sample Input 2
6
0
Sample Output 1
60
Return to index
16
Toggle and 2 jumps left
Take in a character as an input from the user,
A. Condition 1 : If the character is an alphabet then you need to toggle the character first,
For example,
● if the entered character is ‘a’, then convert it into ‘A’, and if the entered character is ‘A’ then
convert it into ‘a’, this simply means that if the entered character is a capital case then
convert it into a small case character and vice-versa.
● After toggling the character: a. if the resultant character is not ‘a’, ‘A’, ‘b’, ‘B’, then take two
jumps to the left and print the character, for eg. If the toggled character is ‘c’ then print ‘a’,
If the toggled character is ‘Z’, then print ‘X’. b. If the toggled character is ‘a’, ‘A’, ‘b’, ‘B’, then
print “Sorry”.
Input Format
Output Format
d B
Explanation Since the given character is 'd', its toggled character is 'D', after taking two jumps to
left, we print 'B'
Z x
a sorry
\ No alphabet
Return to index
17
Marks and Rank
Take in marks and rank of a student as integer inputs. Follow these conditions in a stepwise
manner: if the condition given before fails only then move on to the next condition, otherwise
don’t.
Input Format
Constraints
31
0 <= 𝑚𝑎𝑟𝑘𝑠, 𝑟𝑎𝑛𝑘 <= 2 −1
Output Format
15 Needs improvement
90
Explanation Since the marks of the student is below 20, so we print the string "Needs
improvement"
25 Concentrate
75
Explanation Here the second condition follows, so we print the string "Concentrate"
55 Needs to focus
50
Return to index
18
Adult or not
You will be given the age of a person as an integer input. You need to check if the age is greater
than or equal to 18. If it is, print "Adult", otherwise, print “Below age”.
Input Format
For each input, you will get the age of a person as an integer input.
Output Format
Sample Input 0
20
Sample Output 0
Adult
Explanation 0
Since the age is 18 which is greater than or equal to 18, and so we print "Adult"
Return to index
19
High Sum or Low Sum
You will get two integer inputs x and y. You need to check if the sum of x and y is greater than or
equal to 100. If it is, print “High Sum”, otherwise, print “Low Sum”.
Input Format
You will get the value of x in the first line, You will get the value of y in the second line.
Output Format
Sample Input
40
70
Sample Output
High Sum
Explanation
Value of x is 40, the value of y is 70. So the sum is 110, since sum is greater than or equal to 100 so
we print "High Sum"
Return to index
20
Grade the student
You are given marks of a student as an integer input. You need to print according to the following
rules:
Input Format
For each input, you will get marks of the student as an integer input.
Output Format
Sample Input
92
Sample Output
excellent
Explanation
Since the marks entered by the user is above 90, so we print "excellent"
Return to index
21
Shop Discount
A shop will give a discount of 10% on the total cost if the cost of the quantity purchased is more
than 1000. a. Ask the user for the number of units, b. Suppose, one unit will cost 100. c. Judge and
print total cost for the user in the integer format.
Input Format
For each input, You will be given the number of units in the integer format.
Output Format
Sample Input 0
15
Sample Output 0
1350
Explanation 0
The output is 1350, because the entered quantity is 15, so the cost of the units before the discount
is 1500, now 10% discount on 1500 is 150, so the final cost becomes 1500-150, which is equal to
1350.
10 1000
9 900
11 990
8 800
Return to index
22
Print Bonus
Ask users for their salary and year of service and print the net bonus amount. The bonus in a
company is given by Bonus = Salary * (5 / 100). A company decided to give a bonus of 5% to
employees if his/her years of service is more than 5 years. If the years of service is less than or
equal to 5, print 0, otherwise print Bonus calculated.
Input Format
For each input, you will be given Salary of the user in the first line as an integer input. Years of
service in the second line as an integer input.
Output Format
Sample Input
20000
6
Sample Output
1000
Explanation
Since the years of service are 6, we give the bonus. Hence the bonus is 1000.
Return to index
23
Print final salary
Take the age and salary of the person as integer inputs. And print his final income as an integer
data-type output. The government adds money into the account of a person based on his age: a. If
age is above 60, Rs. 1000 is added into the account. b. If age is above 40 and less than or equal to
60, Rs. 500 is added. c. No money is added if the age is less than or equal to 40.
Input Format
For each input, Age of the person in the first line, Salary of the person in the second line.
Output Format
Sample Input 0
65
2000
Sample Output 0
3000
Explanation 0
Since the age of the person is above 65, we add 1000 into his bank account. Since his salary is
2000, his final salary becomes 3000.
Return to index
24
Print z and x divisible by 3
Take integer inputs x, y, z from the user,
If the x is divisible by 3, then
1. If y is greater than or equal to 200, then add 10 to the value of z.
2. Else if y is greater than or equal to 100 but less than 200, then add 5 to the value of z
3. Else if y is greater than or equal to 50, but less than 100, then add 4 to the value of z
4. Else add 1 to z
Else if x is not divisible by 3, then
1. If y is greater than or equal to 200, then add 3 to the value of z.
2. Else if y is greater than or equal to 100 but less than 200, then add 2 to the value of z
3. Else add 1 to z
In the end add 10 to the value of z and print the final value of z.
Input Format
For each input, You get the value of x in the first line as an integer input
You get the value of y in the second line as an integer input
You get the value of z in the third line as an integer input
Output Format
Print the final value of z as an integer output
30 25
300
5
Explanation : Since x is 30 which is divisible by 3, and y is 300 which is greater than or equal to
200, so we add 10 to the value of z, so the value of z becomes 15.
In the end we add 10 to the value of z again, so now the value of z becomes 25.
41 19
150
7
Explanation : Since the value of x is 41, which is not divisible by 3, and value of y is 150 which is
greater than or equal to 100, so we add 2 to the value of z, so z becomes 7+2=9,
In the end, we add 10 to the value of z, so the value of z becomes 9+10=19.
Return to index
25
Print the oldest among three
You will be given the ages of three friends A,B,C as an integer input. You have to print the name of
the oldest friend among them.
Input Format
For each input, you will be given Age of A in the first line as an integer input Age of B in the second
line as an integer input Age of C in the third line as an integer input
Output Format
Sample Input 0
10
20
30
Sample Output 0
Explanation 0
Sample Input 1
20
30
5
Sample Output 1
Explanation 1
Return to index
26
Rich Adult Young
Take the age and salary of a person as an integer input, If the age is above 40 then
1. If the salary is greater than or equal to 30,000 then print “You are rich and adult”
2. Else print “You are an adult”
1. If the salary is greater than or equal to 12,000, then print “You are rich and young”
2. Else print “You are young”
Input Format
For each input, You will get the age of the person in the first line as an integer input, You will get
the salary of the person in the second line as an integer input.
Output Format
Explanation: Since the age is above 40 and the salary is greater than or equal to 30000, so we
print "You are rich and adult"
Explanation: Since the age is 30 which is less than or equal to 40 and salary is 10000 which is
less than 12000, so we print "You are young"
Return to index
27
Print final z
Take input three numbers x, y, z as an integer input
Then if the value of x is greater than or equal to 20,
1. If the value of y is greater than or equal to 100 then add 100 to the value of z.
2. If the value of y is less than 100 and greater than or equal to 50, then add 50 to z.
3. Else add 10 to the value of z.
Else if the value of x is less than 20,
1. If the value of y is greater than or equal to 100 then add 3 to the value of z.
2. If the value of y is less than 100 and greater than or equal to 50, then add 2 to z.
3. Else add 1 to the value of z.
Print the final value of z as an integer output in the end.
Input Format
For each input, Value of x as an integer input will be given in the first line Value of y as an integer
input will be given in the second line Value of z as an integer input will be given in the third line
Output Format
Print the final value of z as an integer output
30 130
120
30
Explanation : Since the value of x is 30 which is greater than or equal to 20, and the value of y is
120 which is greater than or equal to 100, so we add 100 to the value of z.
22 90
55
40
Explanation : Since the value of x is greater than or equal to 20, and the value of y is 55 which is
greater than or equal to 50 and less than 100, so we print 50 to the value of z. We print 90.
Return to index
28
Tell about x y
Take in two inputs x and y from the user, and then
1. If the value of x is greater than or equal to 59 and y is greater than or equal to 10, then
print “X is greater than or equal to 59 and y is greater than or equal to 10”
2. If the value of x is greater than or equal to 50, and y is less than 10, then print “X is greater
than or equal to 50 and y is less than 10”
3. Else print “None of the condition matches”
Input Format
For each input, you will get Value of x as an integer input in the first line, Value of y as an integer
input in the second line.
Output Format
Sample Input 0
60
12
Sample Output 0
X is greater than or equal to 59 and y is greater than or equal to 10
Explanation 0
Since the value of x is 60 which is greater than or equal to 59, and the value of y is 12 which is
greater than or equal to 10, so we print the string "X is greater than or equal to 59 and y is greater
than or equal to 10"
Sample Input 1
55
8
Sample Output 1
X is greater than or equal to 50 and y is less than 10
Explanation 1
Since the value of x is 55 which is greater than or equal to 50, and the value of y is 8 which is less
than 8,so we print the string
X is greater than or equal to 50 and y is less than 10
Return to index
29
Sample Input 2
25
12
Sample Output 2
None of the condition matches
Explanation 2
Since the value of x is 25 which doesn't follow the condition 1 where it is written that the value of x
should be greater than or equal to 59, also the condition 2 is not followed as it is written in it that
the value of x should be greater than or equal to 50, Since none of the above 2 conditions follow,
so we print the string "None of the condition matches"
Sample Input 3
55
10
Sample Output 3
None of the condition matches
Sample Input 5
59
10
Sample Output 5
X is greater than or equal to 59 and y is greater than or equal to 10
Return to index
30
Print the final incremented salary
Take in three inputs age, salary, experience, then
1. If age is greater than 60 and salary is greater than 20,000 and experience is greater than 20
years, then add 5000 to the salary.
2. If age is greater than 40 and salary is greater than 15,000 and experience is greater than 10
years, then add 2000 to the salary.
3. If age is greater than 30 and salary is greater than 10,000 and experience is greater than 5
years, then add 1000 to the salary.
4. Otherwise add 500 to the salary.
In the end Print the final salary.
Input Format
For each input, You will get age as an integer input in the first line, You will get salary as an integer
input in the second line, You will get experience as an integer input in the third line.
Output Format
Print the final salary as an integer output.
65 30000
25000
25
Explanation : Since the age is greater than 60 and salary is greater than 20,000 and experience is
greater than 20 years, so we add 5000 to the salary, hence we print 30000
30 15500
15000
7
Explanation : Since the age is 30, none of the conditions follow, so we add 500 to the salary.
Hence the final salary is 15500
40 15500 65 10500
15000 10000
5 25
Return to index
31
Top Management or not
Take in experience, salary and rank as integer inputs, then
1. If experience is greater than or equal to 10 years or the salary is greater than or equal to
50,000 or rank is greater than or equal to 10, then print “You are in top management”
2. Else print “You are not in top management”
Input Format
For each input, you will get Experience in the first line as an integer input, Salary in the second line
as an integer input, Rank in the third line as an integer input.
Output Format
You have to print the string "You are in top management" or "You are not in top management"
according to the conditions given in the problem statement.
Explanation: Since the experience of the person is 11 which is greater than or equal to 10 so the
first condition follows, hence the string "You are in top management" gets printed.
Explanation: Since the salary is 55000 which is greater than or equal to 50000 so the first
condition follows, hence we print the string "You are in top management".
Explanation Since the rank is 11 which is greater than or equal to 10, so the first condition
follows, hence we print the string "You are in top management"
Return to index
32
Print final z given xyz
Take in x, y, z as integer inputs from the user,
1. If x is greater than or equal to 20 and z is less than 100 then add 200 to the value of z.
2. If x is greater than or equal to 10, or y is less than 50 Then add 100 to the value of z.
In the end print the final value of z as an integer output.
Input Format
Output Format
25 280
30
80
Explanation Since the value of x is 25 which is greater than or equal to 20, and the value of z is
80 which is less than 100, hence the first condition follows, so we 200 to the value of z and
hence we print 80 + 200 = 280
25 290
45
190
Explanation Here the second condition follows so we add 100 to the value of z, so z becomes
190 + 100 = 290.
15 220
60
120
Return to index
33
Small Capital or Digit
Take in a character as an input and then
1. Print “Small case” if it is a small case character.
2. Print “Capital case” if it is a capital case character.
3. Print “Digit” if it is a digit.
4. Print “None” if none of the above conditions follow.
Input Format
For each input you will get a character as an input from the user.
Output Format
Sample Input 0
a
Sample Output 0
Small case
Explanation 0
Since the entered character is a small case character, so we print the string "Small case"
Sample Input 1
Z
Sample Output 1
Capital case
Sample Input 2
0
Sample Output 2
Digit
Sample Input 2
?
Sample Output 2
none
Return to index
34
Add if a digit
Take in a character as an input from the user
1. If the entered character is a digit, then add 100 to the value of the digit entered and print
the final answer. Convert the digit which is added as a character data-type into the integer
data-type using two ways
First: By using [Use the in-built function Character.getNumericValue]
Second using: By manipulating the digit character data-type into the integer data-type.
2. Else print “This is not a digit”
Input Format
Output Format
Sample Input 0
Sample Output 0
107
Explanation 0
Since the entered character is a digit, we add 100 to it and print the final answer.
Sample Input 2
Sample Output 2
Explanation 2
Since the entered character is not a digit so we print the string "This is not a digit"
Return to index
35
Toggle the character
Take in a character as an input from the user
a. If the entered character is a small-case character, then convert it into the corresponding
uppercase character and print it.
b. If the entered character is an upper-case character, then convert it into the corresponding
lowercase character and print it.
Input Format
Constraints
Output Format
Sample Input 0
a
Sample Output 0
A
Sample Input 1
K
Sample Output 1
k
Return to index
36
Print character at 3rd index
You will be given a string as an input, and
1. If the length of the string is greater than or equal to 4, then print the character at 3rd index.
2. Otherwise, print “Small string”
Input Format
Output Format
Sample Input 0
wxyza
Sample Output 0
z
Explanation 0
Since the length of the string is 5 which is greater than or equal to 4, we print the character at the
third index which is z.
Sample Input 1
abc
Sample Output 1
Small string
Sample Input 2
lmnopqr
Sample Output 2
o
Return to index
37
Concatenate_Two_Strings
Hello + Friends = HelloFriends
Take two strings as input by creating a Scanner object. Print the final string as output after
concatenation.
Input Format
Output Format
Sample Input 0
Hello
Friends
Sample Output 0
HelloFriends
Return to index
38
string concatenate 2
Given 2 strings, a and b, return a string of the form short+long+short, with the shorter string on
the outside and the longer string on the inside. The strings will not be the same length, but they
may be empty (length 0). comboString("Hello", "hi") → "hiHellohi" comboString("hi", "Hello") →
"hiHellohi" comboString("aaa", "b") → "baaab"
Input Format
Constraints
s1.length != s2.length
Output Format
Sample Input 0
hi
hello
Sample Output 0
hihellohi
Sample Input 1
hello
hi
Sample Output 1
hihellohi
Return to index