[Go to site: main page, start]

0% found this document useful (0 votes)
10 views9 pages

Java Array Manipulation Techniques

The document contains multiple Java programs that demonstrate various functionalities, including returning even numbers from an array, sorting an array with even numbers first, encrypting data, summing the largest elements, sorting strings, summing perfect squares, replacing digits in integers, generating Fibonacci series, and converting between binary and decimal. Each program includes user input handling and outputs the results accordingly. The examples illustrate fundamental programming concepts such as loops, conditionals, and array manipulations.

Uploaded by

Ananya Samanta
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)
10 views9 pages

Java Array Manipulation Techniques

The document contains multiple Java programs that demonstrate various functionalities, including returning even numbers from an array, sorting an array with even numbers first, encrypting data, summing the largest elements, sorting strings, summing perfect squares, replacing digits in integers, generating Fibonacci series, and converting between binary and decimal. Each program includes user input handling and outputs the results accordingly. The examples illustrate fundamental programming concepts such as loops, conditionals, and array manipulations.

Uploaded by

Ananya Samanta
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.

Return even array from an array in java


import [Link].*;
public class Main
{
static int[] Main(int[] array,int n){
int count=0;
for(int a:array){
if(a%2==0){
count++;
}
}
int[] newarray=new int[count] ;
int i=0;
for(int a:array){
if(a%2==0){
newarray[i]=a;
i++;
}
}
return newarray;
}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("Enter array elements");
int n=5;
int[] array= new int[n];
for(int i=0;i<n;i++){
array[i]=[Link]();
}
int[] ans=Main(array,n);
for(int a:ans){
[Link](a);
}
}
}
2. You are given an array of size 'n'. Write a function that sorts the array
such that all even numbers appear before the odd numbers.
Example: Input: arr = [3, 8, 5, 12, 7, 10]
Output: [8, 12, 10, 3, 5, 7]
import [Link].*;
public class Main
{
static int[] sortarray(int[] arr,int n){
ArrayList<Integer> l1= new ArrayList<>();
for(int a:arr){
if(a%2==0){
[Link](a);
}
}
for(int a:arr){
if(a%2!=0){
[Link](a);
}
}
int[] array=new int[[Link]()];
for(int i=0;i<[Link]();i++){
array[i]=[Link](i);
}
return array;
}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("Enter the size of array");
int size= [Link]();
[Link]("Enter array elements");
int [] arr = new int[size];
for(int i=0;i<size;i++){
arr[i]=[Link]();
}
int[] ans= sortarray(arr,size);
[Link]([Link](ans));

}
}
//or,
import [Link];

public class Main {


public static void main(String[] args) {
int[] arr = {3, 8, 5, 12, 7, 10};
int[] sortedArr = sortEvenOdd(arr);
[Link]([Link](sortedArr));
}

public static int[] sortEvenOdd(int[] arr) {


int n = [Link];
int[] result = new int[n];
int index = 0;

// First, add all even numbers


for (int num : arr) {
if (num % 2 == 0) {
result[index++] = num;
}
}

// Then, add all odd numbers


for (int num : arr) {
if (num % 2 != 0) {
result[index++] = num;
}
}

return result;
}
}

3. Encript the data adding character in index+3


Input: 25143 3
Output: 43251
import [Link].*;
class HelloWorld {
static String encription(String data,int key ){
StringBuilder sb = new StringBuilder();
for(int i=0;i<[Link]();i++){
int index=(i+key)%[Link]();
[Link]([Link](index));
}
return [Link]();
}

public static void main(String[] args) {


Scanner sc= new Scanner([Link]);
[Link]("Enter a String");
String data=[Link](); //25143
[Link]("Enter the key value");
int key=[Link](); //3
String ans=encription(data,key);
[Link](ans); //43251
}
}
4. sum of largest and second largest element in the array
import [Link].*;
public class Main
{
static int function(int[] arr,int n){
[Link](arr);
int size=[Link];
return arr[size-1]+arr[size-2];
}
public static void main(String[] args) {
[Link]("Enter the size of array");
Scanner sc=new Scanner([Link]);
int size=[Link]();
[Link]("Enter array elements");
int[] arr=new int[size];
for(int i=0;i<size;i++){
arr[i]=[Link]();
}
int ans=function(arr,size);
[Link](ans);
}
}

5. WAP to sort a string in alphabetical order

import [Link].*;

public class Main {


public static void main(String[] args) {
String str = "hello";

// Convert the string to a char array


char[] ch = [Link]();

// Sort the char array


[Link](ch);

// Convert the sorted char array back to a string


String sortedString = new String(ch);

// Print the result


[Link]("Sorted string: " + sortedString);
}
}
6. Sum of positive perfect square elements in an array
public class PerfectSquareSum {
// Method to return the sum of positive perfect square elements
public static int sumOfPerfectSquares(int[] arr) {
int sum = 0;
for (int num : arr) {
if (num > 0 ){
int sqrt = (int) [Link](num);
if (sqrt * sqrt == num){
sum += num;
}
}
}
return sum;
}

public static void main(String[] args) {


int[] arr = {-3, 4, -2, 5, 0, 16, 25, 6};
int result = sumOfPerfectSquares(arr);
[Link]("Sum of positive perfect square elements: " + result);
}
}
7. WAP to Replace all 0’s with 1 in a given integer in java
import [Link];
public class ReplaceZerosWithOnes {
public static void main(String[] args) {
// Create a scanner object to read input
Scanner scanner = new Scanner([Link]);

// Prompt user to enter an integer


[Link]("Enter an integer: ");
int number = [Link]();

// Convert the integer to a string


String numberStr = [Link](number);

// Replace all occurrences of '0' with '1'


String replacedStr = [Link]('0', '1');

// Convert the string back to an integer


int replacedNumber = [Link](replacedStr);

// Output the result


[Link]("The integer after replacing 0's with 1's is: " +
replacedNumber);

// Close the scanner


[Link]();
}
}
8. return fibonacci series as an array from java function using arraylist
import [Link];
import [Link];

public class FibonacciSeriesArray {

// Function to return Fibonacci series as an array using ArrayList


public static int[] getFibonacciSeries(int terms) {
// Create an ArrayList to store the Fibonacci series
ArrayList<Integer> fibonacciList = new ArrayList<>();

// Base cases for 0 and 1 terms


if (terms <= 0) {
return new int[0]; // Return empty array for 0 terms
}

// Add the first Fibonacci number


[Link](0);

if (terms == 1) {
return new int[]{0}; // Return array with one element
}

// Add the second Fibonacci number


[Link](1);

// Calculate the remaining Fibonacci numbers and add them to the list
for (int i = 2; i < terms; i++) {
int nextNumber = [Link](i - 1) + [Link](i - 2);
[Link](nextNumber);
}

// Convert ArrayList to array


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

return fibonacciArray; // Return the Fibonacci series as an array


}

public static void main(String[] args) {


// Create a scanner object to read input
Scanner scanner = new Scanner([Link]);

// Prompt user to enter the number of terms


[Link]("Enter the number of terms in the Fibonacci series: ");
int terms = [Link]();

// Get the Fibonacci series from the function


int[] fibonacciSeries = getFibonacciSeries(terms);

// Output the Fibonacci series


[Link]("Fibonacci Series:");
for (int number : fibonacciSeries) {
[Link](number + " ");
}

// Close the scanner


[Link]();
}
}
9. Binary to decimal conversion in java
import [Link].*;

public class BinaryToDecimalConversionUsingParseInt {

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
[Link]("Enter a binary integer: ");
int binary = [Link]();
//Integer to string
String binaryString=[Link](binary);
// Convert binary to decimal using [Link]()
int decimal = [Link](binaryString, 2);

// Output the result


[Link]("The decimal equivalent is: " + decimal);

}
}
10. Decimal to binary conversion
import [Link].*;

public class DecimalToBinaryUsingToBinaryString {

public static void main(String[] args) {


// Create a scanner object to read input
Scanner scanner = new Scanner([Link]);

// Prompt user to enter a decimal integer


[Link]("Enter a decimal integer: ");
int decimal = [Link]();

// Convert decimal to binary using [Link]()


String binary = [Link](decimal);

//String to number conversion


int result=[Link](binary);
[Link](result);

}
}

You might also like