1.
Return even array from an array in java
import java.util.*;
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(System.in);
System.out.println("Enter array elements");
int n=5;
int[] array= new int[n];
for(int i=0;i<n;i++){
array[i]=sc.nextInt();
}
int[] ans=Main(array,n);
for(int a:ans){
System.out.println(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 java.util.*;
public class Main
{
static int[] sortarray(int[] arr,int n){
ArrayList<Integer> l1= new ArrayList<>();
for(int a:arr){
if(a%2==0){
l1.add(a);
}
}
for(int a:arr){
if(a%2!=0){
l1.add(a);
}
}
int[] array=new int[l1.size()];
for(int i=0;i<l1.size();i++){
array[i]=l1.get(i);
}
return array;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of array");
int size= sc.nextInt();
System.out.println("Enter array elements");
int [] arr = new int[size];
for(int i=0;i<size;i++){
arr[i]=sc.nextInt();
}
int[] ans= sortarray(arr,size);
System.out.println(Arrays.toString(ans));
}
}
//or,
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = {3, 8, 5, 12, 7, 10};
int[] sortedArr = sortEvenOdd(arr);
System.out.println(Arrays.toString(sortedArr));
}
public static int[] sortEvenOdd(int[] arr) {
int n = arr.length;
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 java.util.*;
class HelloWorld {
static String encription(String data,int key ){
StringBuilder sb = new StringBuilder();
for(int i=0;i<data.length();i++){
int index=(i+key)%data.length();
sb.append(data.charAt(index));
}
return sb.toString();
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter a String");
String data=sc.nextLine(); //25143
System.out.println("Enter the key value");
int key=sc.nextInt(); //3
String ans=encription(data,key);
System.out.println(ans); //43251
}
}
4. sum of largest and second largest element in the array
import java.util.*;
public class Main
{
static int function(int[] arr,int n){
Arrays.sort(arr);
int size=arr.length;
return arr[size-1]+arr[size-2];
}
public static void main(String[] args) {
System.out.println("Enter the size of array");
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
System.out.println("Enter array elements");
int[] arr=new int[size];
for(int i=0;i<size;i++){
arr[i]=sc.nextInt();
}
int ans=function(arr,size);
System.out.println(ans);
}
}
5. WAP to sort a string in alphabetical order
import java.util.*;
public class Main {
public static void main(String[] args) {
String str = "hello";
// Convert the string to a char array
char[] ch = str.toCharArray();
// Sort the char array
Arrays.sort(ch);
// Convert the sorted char array back to a string
String sortedString = new String(ch);
// Print the result
System.out.println("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) Math.sqrt(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);
System.out.println("Sum of positive perfect square elements: " + result);
}
}
7. WAP to Replace all 0’s with 1 in a given integer in java
import java.util.Scanner;
public class ReplaceZerosWithOnes {
public static void main(String[] args) {
// Create a scanner object to read input
Scanner scanner = new Scanner(System.in);
// Prompt user to enter an integer
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
// Convert the integer to a string
String numberStr = Integer.toString(number);
// Replace all occurrences of '0' with '1'
String replacedStr = numberStr.replace('0', '1');
// Convert the string back to an integer
int replacedNumber = Integer.parseInt(replacedStr);
// Output the result
System.out.println("The integer after replacing 0's with 1's is: " +
replacedNumber);
// Close the scanner
scanner.close();
}
}
8. return fibonacci series as an array from java function using arraylist
import java.util.ArrayList;
import java.util.Scanner;
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
fibonacciList.add(0);
if (terms == 1) {
return new int[]{0}; // Return array with one element
}
// Add the second Fibonacci number
fibonacciList.add(1);
// Calculate the remaining Fibonacci numbers and add them to the list
for (int i = 2; i < terms; i++) {
int nextNumber = fibonacciList.get(i - 1) + fibonacciList.get(i - 2);
fibonacciList.add(nextNumber);
}
// Convert ArrayList to array
int[] fibonacciArray = new int[fibonacciList.size()];
for (int i = 0; i < fibonacciList.size(); i++) {
fibonacciArray[i] = fibonacciList.get(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(System.in);
// Prompt user to enter the number of terms
System.out.print("Enter the number of terms in the Fibonacci series: ");
int terms = scanner.nextInt();
// Get the Fibonacci series from the function
int[] fibonacciSeries = getFibonacciSeries(terms);
// Output the Fibonacci series
System.out.println("Fibonacci Series:");
for (int number : fibonacciSeries) {
System.out.print(number + " ");
}
// Close the scanner
scanner.close();
}
}
9. Binary to decimal conversion in java
import java.util.*;
public class BinaryToDecimalConversionUsingParseInt {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a binary integer: ");
int binary = sc.nextInt();
//Integer to string
String binaryString=Integer.toString(binary);
// Convert binary to decimal using Integer.parseInt()
int decimal = Integer.parseInt(binaryString, 2);
// Output the result
System.out.println("The decimal equivalent is: " + decimal);
}
}
10. Decimal to binary conversion
import java.util.*;
public class DecimalToBinaryUsingToBinaryString {
public static void main(String[] args) {
// Create a scanner object to read input
Scanner scanner = new Scanner(System.in);
// Prompt user to enter a decimal integer
System.out.print("Enter a decimal integer: ");
int decimal = scanner.nextInt();
// Convert decimal to binary using Integer.toBinaryString()
String binary = Integer.toBinaryString(decimal);
//String to number conversion
int result=Integer.parseInt(binary);
System.out.println(result);
}
}