0% found this document useful (0 votes)
36 views

Java Problems (Logical Programs) : Sorting

The document discusses various Java logical problems involving arrays, including sorting arrays in ascending and descending order, finding highest/lowest values in an array, and implementing mathematical and number series problems. It provides code examples for sorting an array in ascending order and finding the highest number in an array. A variety of other logical problems are listed, such as sorting characters, validating input, computing averages, and generating patterns.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Java Problems (Logical Programs) : Sorting

The document discusses various Java logical problems involving arrays, including sorting arrays in ascending and descending order, finding highest/lowest values in an array, and implementing mathematical and number series problems. It provides code examples for sorting an array in ascending order and finding the highest number in an array. A variety of other logical problems are listed, such as sorting characters, validating input, computing averages, and generating patterns.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Java Problems (Logical Programs)

Sorting
 Array Ascending order(Number), ex. {2,4,3,1,8,5,6} {1,2,3,4,5,6,8}
 Array Descending order(Number) , ex. {2,4,3,1,8,5,6}  {8,6,5,4,3,2,1}
 Flipping of Array , ex. {1,2,3,4,5,6,8}  {8,6,5,4,3,2,1}
 Character Sorting, ex. {f,g,a,c,d,b,e}  {a,b,c,d,e,f,g}

Finding Values

 Finding Array’s Highest Number, ex. {2,4,3,1,8,5,6}


 Finding Array’s Lowest Number, ex. {2,4,3,1,8,5,6}
 Finding Array’s Second Highest Number , ex. {2,4,3,1,8,5,6}
 Finding Array’s Second Lowest Number, ex. {2,4,3,1,8,5,6}
 Finding Duplicated Numbers, ex. {2,4,3,1,8,5,6,6,3,3}  {3,6}

Mathematical Problems
 Decimal to Binary ex. Input = 6  0110
 Binary to Decimal ex. Input = 0110  6
 Adding all numbers in a single Integer input
 Input Validation
 Average Computation
 Two Dimensional Array’s

Number Series
 Arithmetic, ex. 1 , 4 , 7 , 10, 13 , 16
 Cube, ex. 1 , 8 , 27 , 64 , 125
 Even, ex. 2 , 4 , 6 , 8 , 10
 Odd, ex 1 , 3 , 5 , 7 , 9
 Fibonacci, ex. 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34
 Geometric, ex. 5 , 10 , 20 , 40 , 80 , 160 , 320
 Prime, ex. 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23
 Square, ex. 1 , 4 , 9 , 16 , 25 , 36 , 49 , 64
 Triangular, ex. 1 , 3 , 6 , 10 , 15 , 21 , 28
Patterns

 Cross
 Diamond
 Pyramid
 Inverted Pyramid
Java Logical Problems Codes

 Sorting

 Array Ascending order(Number), ex. {2,4,3,1,8,5,6} {1,2,3,4,5,6,8}


int first_Array[] = {2,4,3,1,8,5,6};
int Increment = 0 , greaterNumber = first_Array[0] , Max = 0;

while(Increment != first_Array.length*first_Array.length)
{
for (int firstIndex = 0; firstIndex <first_Array.length-1; firstIndex++)
{
for (int Loop = firstIndex; Loop < firstIndex+1; Loop++)
{
if(first_Array[firstIndex] >= first_Array[firstIndex+1])
{
greaterNumber = first_Array[firstIndex];
first_Array[firstIndex] = first_Array[firstIndex+1];
first_Array[firstIndex+1] = greaterNumber;
}
}
}
//Increment by 1 until the value reaches the length of Array
Increment++;
}
//Print Array values in ascending order
System.out.print("Ascending order: ");
for (int arrayIndex = 0; arrayIndex < first_Array.length; arrayIndex++)
{
System.out.print(first_Array[arrayIndex]+" ");
}
 Array Descending order(Number) , ex. {2,4,3,1,8,5,6}  {8,6,5,4,3,2,1}

 Finding Array’s Highest Number, ex. {2,4,3,1,8,5,6}


int [] Array = {2,4,3,1,8,5,6};

int highestNumber = Array[0] , NumberOfLoop = 0;

for( NumberOfLoop = 0; NumberOfLoop < Array.length; NumberOfLoop++ )


{
if(Array[NumberOfLoop] >= highestNumber)
{
highestNumber = Array[NumberOfLoop];
}
}
System.out.print("Highest Number: "+highestNumber+"\n\n");

You might also like