Knowee
Questions
Features
Study Tools

import java.util.Scanner;public class Test { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number of elements in the array"); int n = sc.nextInt(); //Declare the array int arr[ ]=new int[n]; //Get the elements from user System.out.println("Enter the elements of the array"); for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } //Code to find the sum of odd elements in the array int sum=findSumOddElements();   // invoke the method System.out.println("The sum is "+);        } public int findSumOddElements() { int sum=0; for(int i=0;i<;i++) { if(()!=0) { sum=sum+arr[i]; } }   ;  //return statement }}

Question

import java.util.Scanner;public class Test { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number of elements in the array"); int n = sc.nextInt(); //Declare the array int arr[ ]=new int[n]; //Get the elements from user System.out.println("Enter the elements of the array"); for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } //Code to find the sum of odd elements in the array int sum=findSumOddElements();   // invoke the method System.out.println("The sum is "+);        } public int findSumOddElements() { int sum=0; for(int i=0;i<;i++) { if(()!=0) { sum=sum+arr[i]; } }   ;  //return statement }}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

It seems like you're trying to create a Java program that calculates the sum of odd numbers in an array. However, your code is incomplete and has some errors. Here's the corrected version:

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of elements in the array");
        int n = sc.nextInt();

        //Declare the array
        int arr[] = new int[n];

        //Get the elements from user
        System.out.println("Enter the elements of the array");
        for(int i = 0; i < n; i++){
            arr[i] = sc.nextInt();
        }

        //Code to find the sum of odd elements in the array
        int sum = findSumOddElements(arr, n);  // invoke the method

        System.out.println("The sum is " + sum);
    }

    public static int findSumOddElements(int[] arr, int n) {
        int sum = 0;
        for(int i = 0; i < n; i++) {
            if(arr[i] % 2 != 0) {
                sum = sum + arr[i];
            }
        }
        return sum;  //return statement
    }
}

Here are the changes I made:

  1. I added the parameters arr and n to the findSumOddElements method. This is because you need to pass the array and its length to the method in order to calculate the sum of the odd elements.

  2. I changed the findSumOddElements method to be static. This is because you're calling it from a static context (the main method), so it also needs to be static.

  3. I fixed the System.out.println statement that prints the sum. It was missing the variable to print, so I added sum.

  4. I added the modulus operation (arr[i] % 2) to the if statement in the findSumOddElements method. This operation checks if the number is odd.

  5. I added a return statement to the findSumOddElements method to return the sum of the odd elements.

This problem has been solved

Similar Questions

public class Main  {     public static void main(String arg[]) {       Scanner sc=new Scanner(System.in);      //Get the size of array, n, from user     int n=sc.nextInt();      //Declare an array of size 'n'    int arr[]=;   }}

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int K = sc.nextInt(); int[] A = new int[N]; for (int i = 0; i < N; i++) { A[i] = sc.nextInt(); } Arrays.sort(A); int happyCount = 0; int j = 0; for (int i = 0; i < N; i++) { while (j < N && A[j] - A[i] < K) { j++; } if (j > i + 1) { happyCount++; } } System.out.println(happyCount); } } i need output as 5

Arrange the below code in correct order :Scanner sc=new Scanner(System.in);import java.util.Scanner;int num1 = sc.nextInt();public class Main{}}System.out.println("The value is "+num1);public static void main(String args[]) {

for (int i = 0; i < arr.length-1; i++){ for (int j = i+1; j < arr.length; j++) { if( (arr[i].equals(arr[j])) && (i != j) ) { System.out.println(arr[i]); } }}

Fill the code to print the numbers in the array using for each loop public class Main{    public static void main(String args[])    {                int arr[]={50,70,80,90,100};                //fill code to print the array               for(Answer)                    System.out.println(num);    }}

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.