In: Computer Science
Complete code of fincidng sum of even numbers and odd numbers:-
import java.util.Scanner;
// Main class
class EvenOdd {
// Main method
public static void main(String ... args) {
// Creating object of Scanner
class to take user input.
Scanner sc = new
Scanner(System.in);
// 'arr[]' will store 10 numbers
entered by user.
int arr[] = new int [10];
// 'even' will store the sum of
even numbers.
int even = 0;
// 'odd' will store the sum of
odd numbers.
int odd = 0;
System.out.println("Enter 10
numbers");
// Taking user input and
calculating sum.
for(int i = 0; i < 10; i++)
{
arr[i] =
sc.nextInt();
if(arr[i]%2 ==
0) {
even += arr[i];
}
else {
odd += arr[i];
}
}
// Printing output.
System.out.println("Sum of all even
numbers : "+even);
System.out.println("Sum of all odd
numbers : "+odd);
}
}
Screenshot of output:-
Complete code for squaring a 2D array in java:-
import java.util.Random;
// Main class
class Square {
// This method will print the elements present in
matrix.
public static void ShowNumbers(int mat[][], int r, int
c) {
for(int i = 0; i < r; i++)
{
for(int j = 0; j
< c; j++) {
System.out.print(mat[i][j]+"\t");
}
System.out.println();
}
}
// This method will do the square of each element
in matrix.
public static void Square(int mat[][], int r, int c)
{
for(int i = 0; i < r; i++)
{
for(int j = 0; j
< c; j++) {
mat[i][j] = mat[i][j]*mat[i][j];
}
}
}
public static void main(String ... args) {
// Number of rows and columns in
matrix.
int rows = 5;
int cols = 6;
// Creating object of Random
class to generate random numbers.
Random rand = new Random();
// Creating a matrix.
int mat[][] = new int
[rows][cols];
for(int i = 0; i < rows; i++)
{
for(int j = 0; j
< cols; j++) {
// Inserting elements into matrix.
mat[i][j] = rand.nextInt(100);
}
}
// Printing original
matrix.
System.out.println("Original matrix
: ");
ShowNumbers(mat, rows, cols);
// Squaring the matrix
elements.
Square(mat, rows, cols);
// Again printing squared
matrix.
System.out.println("\nSquared
matrix : ");
ShowNumbers(mat, rows, cols);
}
}
Screenshot of output:-
Complete code of Searching 20 in java:-
import java.util.Scanner;
import java.util.Random;
// Main class
class SearchReplace {
// Print function this will print the content of
array.
public static void print(int arr[], int n) {
for(int i = 0; i < n; i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
}
// Main method
public static void main(String ... args) {
// Creating object of Scanner
class to take user input.
Scanner sc = new
Scanner(System.in);
// Creating object of Random
class to take user input.
Random rand = new Random();
// 'arr[]' will store 10 numbers
generated randomly.
int arr[] = new int [10];
// This boolean variable become
true if 20 present in the array.
boolean present = false;
// Generating random
numbers.
for(int i = 0; i < 10; i++)
{
arr[i] =
rand.nextInt(100);
}
System.out.println("Original array
: ");
print(arr, 10);
for(int i = 0; i < 10; i++)
{
if(arr[i] == 20)
{
present = true;
arr[i] = 40;
}
}
if(present) {
System.out.println("Modified array : ");
print(arr,
10);
}
else {
System.out.println("20 is not present");
}
}
}
Screenshot of output:-
Complete code for inserting 100 at index position 3 in java:-
import java.util.Scanner;
import java.util.Random;
// Main class
class Insert100 {
// Print function this will print the content of
array.
public static void print(int arr[], int n) {
for(int i = 0; i < n; i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
}
// Main method
public static void main(String ... args) {
// Creating object of Scanner
class to take user input.
Scanner sc = new
Scanner(System.in);
// Creating object of Random
class to take user input.
Random rand = new Random();
// 'arr[]' will store 10 numbers
generated randomly.
int arr[] = new int [10];
// Generating random
numbers.
for(int i = 0; i < 10; i++)
{
arr[i] =
rand.nextInt(100);
}
System.out.println("Original array
: ");
print(arr, 10);
// Inserting 100 at index
position 3.
arr[3] = 100;
print(arr, 10);
}
}
Screenshot of output:-