Question

In: Computer Science

given an array, write code to scan the array for a particular purpose . use java

given an array, write code to scan the array for a particular purpose . use java

Solutions

Expert Solution

This code for sum of an array

here i am taking first input for size of an aaray using scanner.

and then i am taking all element in the array using nextInt()

after all i am doing sum of all element here.

code:

import java.util.Scanner;

Public class sum_of_array

{

        public static void main(String[] args)

          {

               int n,sum=0;

               Scanner s=new Scanner(System.in);

               System.out.println("Enter the size of an aaray-:");

               n=nextInt[n];

              int a[]=new int[n];

             System.out.println("Enter all the element in array-:");

              for(int i=0; i<n; i++)

                  {

                       a[i]=s.nextInt();

                       sum=sum+a[i];

                   }

             System.out.println("Sum of array:"+sum);

     }

}

Note:if there is any question in understanding kindly reach out to me in comment section


Related Solutions

Given an array, write code to scan the array for a particular purpose. Given a class,...
Given an array, write code to scan the array for a particular purpose. Given a class, write a getter and/or a setter. Given a class with a list, write code that manages the list.
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
Array of Hope! Write code for a Java class ArrayPair with the following fields and methods:...
Array of Hope! Write code for a Java class ArrayPair with the following fields and methods: ArrayPair - arr1 [] : int - arr2 [] : int Fields - length1 : int - length2 : int + ArrayPair (int arraySize) Methods + add (int arrNumber, int value) : void + remove (int arrNumber) : void + equal () : boolean + greater () : boolean + print () : void Thus, there are two arrays of integers in the class...
Language for this question is Java write the code for the given assignment Given an n...
Language for this question is Java write the code for the given assignment Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the matrix. Then the next line contains the n x n elements...
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new...
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new element to an array.​ Change an element with the new one.​ Search for a particular element in the array.​ ​The code must have four separate methods for each task stated above.​ Do not use any pre-defined Java functions.​ You are free to use int or String data-type for the array.​
Write a program in Java with a Scanner. Given an array and a number k where...
Write a program in Java with a Scanner. Given an array and a number k where k is smaller than the size of the array, write a program to find the k'th smallest element in the given array. It is given that all array elements are distinct. Example: Input: arr[] = {7,10,4,3,20,15} k = 3 Output: 7
write a code for given an array of integers where wachelement represents the maximum number...
write a code for given an array of integers where wach element represents the maximum number of jumps to reach the end of the array(starting from the first element) if an element O,then no jump can be made from that element if it is not possible to reach the end then output in c
1.Write the java code to create a two dimensional String array of sizes 12 by 8;...
1.Write the java code to create a two dimensional String array of sizes 12 by 8; Given the java array:       double[] test = new double[3]; 2.  Write the java code to put the numbers 1.0, 2.0, and 3.0 in to the array so that the 1.0 goes in the first cell, the 2.0 goes in the second cell and the 3.0 goes in the third cell. 3. Can you have different types of data is a three dimensional array? 4....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT