0% found this document useful (0 votes)
7 views14 pages

2b.hands-On - Control Flow Statements - Questions

The document outlines a hands-on training session for SDE readiness, focusing on control flow statements. It includes a series of programming problems with varying levels of difficulty, each requiring the implementation of specific logic in Java. Sample inputs and expected outputs are provided for each problem to guide learners in their coding exercises.

Uploaded by

2k22cse081
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)
7 views14 pages

2b.hands-On - Control Flow Statements - Questions

The document outlines a hands-on training session for SDE readiness, focusing on control flow statements. It includes a series of programming problems with varying levels of difficulty, each requiring the implementation of specific logic in Java. Sample inputs and expected outputs are provided for each problem to guide learners in their coding exercises.

Uploaded by

2k22cse081
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
You are on page 1/ 14

HANDS-ON

SDE Readiness Training

Hands-on No. : 2b

Topic : Control Flow Statements

Date : 13.02.2025

Solve the following problems

Question
Question Detail Level
No.

1 Write a program to find the mobile chosen is within the budget Easy
or not. To find the budget mobiles is based on the below-
mentioned criteria,
a) If the cost of the mobile chosen is less than or equal to
15000 then display it as "Mobile chosen is within the
budget"
b) If the cost of the mobile chosen is greater than 15000
then display it as “Mobile chosen is beyond the
budget”

Sample Input: 12000


Sample Output: Mobile chosen is within the budget
Sample Input: 22000
Sample Output: Mobile chosen is beyond the budget

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int budget = sc.nextInt();
if(budget<=15000) {
System.out.println("Mobile chosen is within the
budget");
}
else {
System.out.println("Mobile chosen is beyond the
budget");
}

OUTPUT:
14000
Mobile chosen is within the budget

17000
Mobile chosen is beyond the budget

It is going to be hard but, hard does not mean


impossible.
1
HANDS-ON
SDE Readiness Training

2 Write a program to check error in marks entry while user enters Easy
the marks in the system. Consider Error as marks entered less
than 0 and more than 100.

Sample Input: 83

Sample Output: Valid entry

Sample Input: 101

Sample Output: Invalid entry

Sample Input: -6

Sample Output: Invalid entry

public static void main(String[] args) {


Scanner marks = new Scanner(System.in);
int a = marks.nextInt();
if((a>=0)&&(a<=100)) {
System.out.println("Valid entry");
}
else {
System.out.println("inValid entry");
}
}

OUTPUT:
84
Valid entry

105
inValid entry

3 Kittu the ‘Giant Ant’ has 100 rooms in its colony. All the ants in Easy
the colony can crawl from 1 room to another, from there to
another and so on. But our ‘Kittu’ has a special power that
makes him to jump from one room to next 10th room directly.

That is room no 1 to room no 11 or 3 to 13… But poor ‘Kittu’


don’t know to calculate/find which room that he can jump next.
Help him saying all the room numbers to climb up. His starting
room number can be the input.

Sample Input: 2
Sample output: 2 12 22 32 … 92
Sample Input: 5
Sample Output: 5 15 25 35 … 95

It is going to be hard but, hard does not mean


impossible.
2
HANDS-ON
SDE Readiness Training
public static void main(String[] args) {
Scanner rooms = new Scanner(System.in);
int ant = rooms.nextInt();
for(int i=ant;i<=100;i+=10) {
System.out.print(i+" ");
}
}

OUTPUT:
2
2 12 22 32 42 52 62 72 82 92

5
5 15 25 35 45 55 65 75 85 95

4 Once a baby lion lost his way in the jungle. An old deer took pity Easy
on him and planned to take him to his place. But the other deer
and his other friends — rabbits, squirrels, and birds are scared
so they accompanied. In the morning they counted themselves
to see if the baby lion done any mischief. Help them to find it.
Total number of animals, count of each (rabbit, deer, birds, and
squirrels) in the morning are the inputs.

Sample Input: 240, 27,48,124,38 Sample


Output: Baby lion is mischievous

Sample Input: 250, 42,46,115,47


Sample Output: Baby lion is well
behaved
Sample Input: 120, 45,38,30, 27
Sample Output: Counted wrongly

public static void main(String[] args) {


int sum =0;
Scanner animals = new Scanner(System.in);
System.out.print("Enter total: ");
int total = animals.nextInt();
System.out.print("Enter their counts: ");
for(int i=1;i<=4;i++) {
int r = animals.nextInt();
sum = sum + r;
}
if(sum==total) {
System.out.print("Baby lion is well behaved");
}
else if(sum<total) {
System.out.println("Baby lion is mischevous");
}
else {
System.out.println("Counted wrongly");
It is going to be hard but, hard does not mean
impossible.
3
HANDS-ON
SDE Readiness Training
}
}

OUTPUT:
Enter total: 270
Enter their counts: 34
65
87
24
Baby lion is mischevous

Enter total: 250


Enter their counts: 25
50
27
148
Baby lion is well behaved

5 A store charges $12 per item if you buy less than 10 items. If you Easy
buy between 10 and 99 items, the cost is $10 per item. If you
buy 100 or more items, the cost is $7 per item. Write the logic
that asks customer name, how many items they are buying and
prints the customer’s name and total cost.

Sample Input: Smith, 40 Sample


Output: Smith 400 Sample
Input: Eve, 111
Sample Output: Eve 777

public static void main(String[] args) {


Scanner customer = new Scanner(System.in);
String name = customer.next();
int items = customer.nextInt();
int cost = 0;
if(items<10) {
cost = items *12;
}
else if((items>=10)&&(items<=99)) {
cost = items * 10;
}
else {
cost = items * 7;
}
System.out.println(name +" " + cost);
}

OUTPUT:

Madhi
50
Madhi 500

It is going to be hard but, hard does not mean


impossible.
4
HANDS-ON
SDE Readiness Training
Madhu
112
Madhu 784

6 Write a program to read a Coordinate Point in a XY Coordinate Easy


System and Determine its Quadrant.

Sample Input: 2 2
Sample Output: 1
public static void main(String[] args) {
Scanner coordinate = new Scanner(System.in);
int x = coordinate.nextInt();
int y = coordinate.nextInt();
if(x<0) {
if(y<0) {
System.out.println("3");
}
else {
System.out.println("2");
}
}
else if(x>0) {
if(y>0) {
System.out.println("1");
}
else {
System.out.println("4");
}
}
else {
System.out.println("Origin");
}
}

OUTPUT:

3
-5
4

-2
8
2

It is going to be hard but, hard does not mean


impossible.
5
HANDS-ON
SDE Readiness Training
7 A student will not be allowed to sit in exam if his/her attendance is less Easy
than 75%. Number of classes held, and the Number of classes attended
are the inputs. Display the attendance percentage and the eligibility of
the student for the exam. Allow the student to sit if he/she has medical
cause. Ask user if he/she has medical cause or not ( 'Y' or 'N' ) only when
the attendance is lacking and print accordingly.

Sample Input: 100, 80 Sample Output:


80% Allowed Sample Input: 100, 60,
‘N’
Sample Output: 60% Not allowed Sample
Input: 100, 70, ‘Y’ Sample Output: 70%
Allowed

public static void main(String[] args) {


Scanner student = new Scanner(System.in);
int total = student.nextInt();
int attended = student.nextInt();
if(attended>=75) {
System.out.println(attended+"% Allowed");
}
else {
char med = student.next().charAt(0);
if(med =='N') {
System.out.println(attended+"% not Allowed");
}
else {
System.out.println(attended+"% Allowed");
}

}
}

OUTPUT:
100
50
Y
50% Allowed

It is going to be hard but, hard does not mean


impossible.
6
HANDS-ON
SDE Readiness Training
100
70
N
70% not Allowed

8 A Strong Number is a number in which the sum of the factorial of each of Easy
its digits is equal to the number itself. For example, 145 is a strong
number because: 1!+4!+5!=1+24+120=145

Write a Java program that checks whether a given number is a Strong


Number or not.

Sample Input :145


Sample Output :Strong Number Sample
Input :141
Sample Output : Not a Strong Number

public static void main(String[] args) {


Scanner number = new Scanner(System.in);
int n = number.nextInt();
int original = n;
int fact =1;
int total =0;
while(n!=0) {
int rem = n % 10;
n=n/10;
for(int i=1;i<=rem;i++) {
fact = fact * i;
//System.out.println(fact);
}
total = total + fact;
fact = 1;
}
if(total == original) {
System.out.println("Strong Number");
}
else {
System.out.println("Not a Strong Number");
}
}

OUTPUT:
145
Strong Number

178
Not a Strong Number

It is going to be hard but, hard does not mean


impossible.
7
HANDS-ON
SDE Readiness Training
9 You are given an integer N (1 ≤ N ≤ 9). Your task is to print a number Easy
pattern in the form of a pyramid as shown , where the number of rows
corresponds to N, and each row has a symmetrical sequence of numbers.

232

34543

4567654

567898765

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
scanner.close();

for (int i = 1; i <= N; i++) {


for (int s = 0; s < N - i; s++) {
System.out.print(" ");
}
for (int j = i; j < 2 * i; j++) {
System.out.print(j + " ");
}
for (int j = 2 * i - 2; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}

OUTPUT:
5
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5

It is going to be hard but, hard does not mean


impossible.
8
HANDS-ON
SDE Readiness Training

10 Write a program to generate and print Pascal’s Triangle for a Easy


given integer N, where N specifies the number of rows in the
triangle.

1
11
121
1331
14641

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
scanner.close();

for (int i = 0; i < N; i++) {


for (int s = 0; s < N - i - 1; s++) {
System.out.print(" ");
}
int num = 1;
for (int j = 0; j <= i; j++) {
System.out.print(num + " ");
num = num * (i - j) / (j + 1);
}
System.out.println();
}
}

OUTPUT:
4
1
1 1
1 2 1
1 3 3 1

It is going to be hard but, hard does not mean


impossible.
9
HANDS-ON
SDE Readiness Training
11 You are required to process three English words inputted from Easy
standard input (STDIN), each on a separate line.
1. First Word: Replace all vowels (a, e, i, o, u) with the
symbol %.
2. Second Word: Replace all consonants with the symbol
#.
3. Third Word: Convert all characters to uppercase.
Finally, concatenate the three modified words and print the
result. No additional output should be printed apart from
the concatenated result.
Sample Input :
how
are
you
Sample Output :
h%wa#eYOU

public static void main(String[] args) {


Scanner square = new Scanner(System.in);
String a = square.next();
String b = square.next();
String c = square.next();
System.out.println(a.replaceAll("[aeiouAEIOU]", "%"));
System.out.println(b.replaceAll("[!aeiouAEIOU]", "#"));
System.out.println(c.toUpperCase());
}

OUTPUT:
Welcome
home
girl
W%lc%m%
h#m#
GIRL

12 A data company needs to store N files on a server. Each file has a Easy
specific size, and the server organizes these files into "buckets"
based on the sum of the digits of their file sizes.
For each file, you need to calculate its "bucket ID" based on the
sum of the digits of the file size. The bucket ID is determined by
summing all the digits in the file size.
Sample Input:
4
43 345 20 987

It is going to be hard but, hard does not mean


impossible.
10
HANDS-ON
SDE Readiness Training
Sample Output
7 12 2 24

public static void main(String[] args) {


Scanner number = new Scanner(System.in);
int count = number.nextInt();
int sum=0;
int[] buckets = new int[count];
for(int i=0;i<count;i++) {
buckets[i] = number.nextInt();
}
for(int i=0;i<count;i++) {
while(buckets[i]!=0) {
int rem = buckets[i] % 10;
buckets[i]=buckets[i]/10;
sum = sum + rem;
}
System.out.println(sum);
sum = 0;
}
}

OUTPUT:
4
43
345
20
987
7 12 2 24

13 “ClimateToday” is a media company that provides weather forecasting Easy


services. The company has recorded the temperatures for a location over
N days and has already generated a report for temperatures that fall
within a specific range from the list.
Now, the company needs to identify and extract the temperatures that
fall outside this range. Specifically, it needs to find the temperatures that
are not covered by the given minimum (minRange) and maximum
(maxRange) values.

SampleInput N:7
minRange:3
maxRange :6
Values: 2 5 1 8 6 9 4 Sample Output
2189

public static void main(String[] args) {


Scanner climate = new Scanner(System.in);
System.out.println("N: ");

It is going to be hard but, hard does not mean


impossible.
11
HANDS-ON
SDE Readiness Training
int n = climate.nextInt();
System.out.print("Enter minrange: ");
int minrange = climate.nextInt();
System.out.println("Enter maxrange: ");
int maxrange = climate.nextInt();
int[] value = new int[n];
for(int i=0;i<n;i++) {
value[i] = climate.nextInt();
}
for(int i=0;i<n;i++) {
if((value[i]>=3)&&(value[i]<=6)){
continue;
}
else {
System.out.print(value[i]+" ");
}
}
}

OUTPUT:
N:
6
Enter minrange: 2
Enter maxrange: 9
3
5
1
3
9
15
1 9 15

14 Apparel, a garments company, wishes to open outlets in various Easy


locations. The company has shortlisted several plots and aims to select
only those that are square-shaped for its outlets. A plot is square-shaped
if its area is a perfect square (i.e., the square root of the area is an
integer).
Task:Write a java program to help Apparel determine the number of
square-shaped plots from the list of shortlisted plots. Input:
1. Get an integer numOfPlots, representing the total number of plots
shortlisted by the company.
2. Get the numOfPlots integers, where each integer
represents the area of a plot.
Output:
Print an integer representing the number of square-shaped plots that will
be selected for the outlets.
SampleInput 8
79 77 54 81 48 34 25 16
SampleOutput 3

It is going to be hard but, hard does not mean


impossible.
12
HANDS-ON
SDE Readiness Training

public static void main(String[] args) {


Scanner square = new Scanner(System.in);
int numofplots = square.nextInt();
int[] area = new int[numofplots];
int count = 0;
for(int i=0;i<numofplots;i++) {
area[i]=square.nextInt();
}
for(int i=0;i<numofplots;i++) {
for(int j=1;j<area[i]/2;j++) {
if(area[i]==j*j) {
count++;
}
}
}
System.out.println(count);
}

OUTPUT:
81
48
34
25
16

15 You are a teacher creating an engaging math activity for your Easy
students by writing N numbers on the classroom whiteboard. You
use a green pen for odd numbers and a red pen for even
numbers.
Your task is to find and return an integer value representing the
number of times you need to switch from the green pen to the red
pen while writing these numbers.
SampleInput:

It is going to be hard but, hard does not mean


impossible.
13
HANDS-ON
SDE Readiness Training
6
1 2 1 6 10 9
SampleOutput:
2
SampleInput:
6
70 23 13 26 72 19
SampleOutput:
1

public static void main(String[] args) {


Scanner classroom = new Scanner(System.in);
int hello = classroom.nextInt();
int count =0;
int[] pens = new int[hello];
int odd = 0;
for(int i=0;i<hello;i++) {
pens[i]=classroom.nextInt();
}
for(int j=0;j<hello;j++) {
if((pens[j])%2!=0) {
odd=j;
if(odd!=(hello-1)){
if((pens[odd+1]%2)==0) {
count++;
}
}
}
}
System.out.println(count);
}

OUTPUT:
5
3
6
4
1
8

It is going to be hard but, hard does not mean


impossible.
14

You might also like