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

Java Training Exercises and Solutions

Uploaded by

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

Java Training Exercises and Solutions

Uploaded by

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

JAVA TRAINING FEB

05/02/2023

import [Link].*;

class GoodMorning{

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);

int n = [Link]();

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

if(i%5 == 0 && i%3 == 0){


[Link]("Good Morning");
}

else if(i%5 == 0){


[Link]("Morning");
}

else if(i%3 == 0){


[Link]("Good");
}
else{
[Link](i);
}

}
}

---------------------------

import [Link].*;
import [Link];

class ArraysRotate{

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);

int arr[] = {1,2,3,4,5};

int n = [Link]();

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

int temp = arr[[Link]-1];

for(int j = [Link]-1 ; j > 0 ; j--){


arr[j]= arr[j-1];

}
arr[0] =temp;

for(int i = 0 ; i < [Link] ; i++){


[Link](arr[i]+" ");
}

}
}

// 3
// 3 4 5 1 2

// for(int i = 0; i < n ; i++){

// int temp = arr[0];

// for(int j = 0 ; j < [Link]-1 ; j++){


// arr[j]= arr[j+1];

// }
// arr[[Link]-1] =temp;

// }

// for(int i = 0 ; i < [Link] ; i++){


// [Link](arr[i]+" ");
// }

// }
// }

// 3
// 4 5 1 2 3

---------------

import [Link].*;
import [Link].*;

class RightTriangle {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();

for (int i = 0; i < n ; i++ , [Link]()){


for ( int j = 0 ; j <= i ; j++){
[Link]("*");
}

}
}
}
5
*
**
***
****
*****

------------------
Home work

get the number , count the iterations that the number's square

import [Link];

public class SquareCount {

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);

int n = [Link]();

int count = 0 , x = 0 ;

while(n > 0) {
for(int i = 1 ; i < n ; i++){
int s = i * i ;
if( s < n){
x = s ;

}
}
n = n - x;
count++;

}
[Link](count);
}

-----------------------------------------------------

06/02/2024

Common questions

Powered by AI

Loop control in the 'SquareCount' program is crucial for precisely decreasing the value of 'n' through efficient subdivisions by perfect squares, employing while and for loops. The strategy of iteratively updating 'x' to the highest perfect square less than 'n' ensures each subtraction is maximal, thus reducing the total count of operations needed. This reflects an effective greedy algorithm approach, optimal for minimizing iterations .

If the input 'n' is 0, the 'RightTriangle' program will not print any output. This is because the outer loop that prints the triangles runs from 0 to n-1. Therefore, with n=0, the loop will not execute any iterations, resulting in no printed triangles .

In the 'SquareCount' program, 'x' is determined within the nested for-loop that iterates from 1 to 'n'. It calculates the square 's' of each number 'i'. If 's' is less than 'n', 'x' is updated to 's'. This ensures that 'x' holds the largest perfect square less than 'n'. The significance of 'x' is that it represents the largest possible subdivision of 'n' that can be removed in each loop iteration, thus reducing 'n' steadily to zero .

The 'RightTriangle' program outputs a right-angled triangle composed of asterisks (*) with 'n' rows. For each row 'i', it prints 'i' plus one asterisks, starting from one asterisk in the first row. This is accomplished using nested loops: the outer loop iterates over the rows, while the inner loop handles the number of asterisks per row. The System.out.println() call in the outer loop ensures that each row is printed on a new line .

Numerical outputs without 'Good', 'Morning', or 'Good Morning' occur when numbers are neither divisible by 3 nor 5. This happens for any loop iteration where 'i' does not satisfy the conditions of i%5==0, i%3==0, or both. For instance, numbers such as 1, 2, 4, 7, 8, 11 etc., do not meet any of the conditional checks and will therefore print as numbers .

The 'SquareCount' program ends when the input number 'n' is reduced to zero or a negative value. It achieves its result by iteratively reducing 'n' by the largest perfect square less than 'n' in each iteration. The loop counts how many such iterations are necessary to reduce the number completely. It keeps track of the count of these reductions, which it prints at the end as the result .

The 'ArraysRotate' program may be inefficient for large arrays due to its repeated element-by-element shift operation, leading to a time complexity of O(n*m), where 'm' is the number of rotations. To improve efficiency, one could implement reversal algorithms or cyclic replacements, which allow rotation to be handled in O(n) time regardless of the number of rotations. This would enhance performance for larger arrays significantly .

The 'ArraysRotate' program shifts the elements of a predefined array to the right by 'n' positions. It stores the last element temporarily, shifts all other elements to the right by one position, and places the stored last element at the beginning. This process is repeated 'n' times to achieve the desired rotation. The current code's limitation is its inefficiency for large arrays and rotation values, as it uses a nested loop structure that can result in high time complexity. Additionally, the program lacks input validation and only adheres to rightward rotation .

The 'ArraysRotate' program assumes that the input 'n' is within valid bounds, but the code provided does not include error checking or management for incorrect or out-of-bound inputs. If the input exceeds the length of the array, the program will rotate the array more times than there are elements, which still effectively results in a valid output because of the cyclic nature of rotations in arrays. However, the program lacks robustness without explicit checks for input validation .

The 'GoodMorning' program uses conditional statements to determine which message to print based on divisibility criteria. For each integer from 1 to n, it first checks if the number is divisible by both 5 and 3, printing 'Good Morning' if true. Otherwise, if the number is divisible only by 5, it prints 'Morning'. If the number is divisible only by 3, it prints 'Good'. If none of these conditions are met, it prints the number itself .

You might also like