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 }}
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:
-
I added the parameters
arrandnto thefindSumOddElementsmethod. 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. -
I changed the
findSumOddElementsmethod to bestatic. This is because you're calling it from a static context (themainmethod), so it also needs to be static. -
I fixed the
System.out.printlnstatement that prints the sum. It was missing the variable to print, so I addedsum. -
I added the modulus operation (
arr[i] % 2) to theifstatement in thefindSumOddElementsmethod. This operation checks if the number is odd. -
I added a return statement to the
findSumOddElementsmethod to return the sum of the odd elements.
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); }}
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.