Question

In: Computer Science

Write a program in java which store 10 numbers and find the sum of odd and...

  1. Write a program in java which store 10 numbers and find the sum of odd and even numbers.
  2. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers.
  3. Write a program in java which has an array of 10 numbers, search whether the number 20 present in that array. If present replace it with 40.
  4. Write a program in java which has an array of 10 numbers; insert the number 100 in the index position 3.
  5. Write a program in java which has an array of 10 numbers; delete the number from the index position 5.
  6. Write a program in java which has two arrays of size 4 and 5; merge them and sort.

Solutions

Expert Solution

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:-


Related Solutions

Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers.
write in c plus plus Write a program that computes the sum of the odd numbers...
write in c plus plus Write a program that computes the sum of the odd numbers and the sum of the even numbers between two values a and b. For example a=1 and b=10
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Write an assembly program In MASM assembler to calculate the sum of odd numbers only stored...
Write an assembly program In MASM assembler to calculate the sum of odd numbers only stored in a 16-bit integers array. The size of the array is provided in a separate variable. Use the following design: Sum = 0 FOR i = 0 TO SizeOfArray - 1     IF Arr[i] % 2 = 1           Sum = Sum + Arr[i]     END IF END FOR
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1...
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1 ..99 and print out the answer.
Write a program in C++ to read 10 numbers from keyboard and find their sum, average,...
Write a program in C++ to read 10 numbers from keyboard and find their sum, average, maximum and minimum
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen. Run the program. Capture the console output. Put the program code and console output at the end of your text file,
Write a Java program to find the sum of all integers between 200 to 250 which...
Write a Java program to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
write a program to find the maximum possible sum such that no two chosen numbers are...
write a program to find the maximum possible sum such that no two chosen numbers are adjacent either vertically, horizontally, or diagonally. code in java
The Sum and The Average In C++, Write a program that reads in 10 integer numbers....
The Sum and The Average In C++, Write a program that reads in 10 integer numbers. Your program should do the following things: Use a Do statement Determine the number positive or negative Count the numbers of positive numbers, and negative Outputs the sum of: all the numbers greater than zero all the numbers less than zero (which will be a negative number or zero) all the numbers Calculate the average of all the numbers. The user enters the ten...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT