1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Pie
3 years ago
5

Write a method that returns the sum of all the elements of an array of ints that have an odd index.

Computers and Technology
1 answer:
wolverine [178]3 years ago
5 0

Answer:

import java.io.*;

import java.util.Scanner;

class Odd {

public static void main (String[] args) {

    Scanner ele=new Scanner(System.in);//creating a scanner object.

      int n,sum=0;//declaring n and sum and initializing sum with 0.

    n=ele.nextInt();//taking input of the n.

    int [] arr =new int[n];//declaring an array arr.

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

    {

        arr[i]=ele.nextInt();//taking input of the array.

    }

    for(int i=0;i<arr.length;i++)

    {

        if(i%2!=0)//adding to the if index is odd.

        {

            sum+=arr[i];

        }

    }

    System.out.println(sum);

}

}

Input:-

6

1 2 3 4 5 6

Output:-

12

Explanation:

First I have created an a scanner object ele. I have taken two integers n and sum, n for number of array elements and sum for calculating the sum of odd indexed elements hence initializing it with 0.Then I have created an array named arr.Then taking the input of the n and array elements.Then looping over the array and calculating the sum.

You might be interested in
Press _________ to toggle the selected range as bold​
Lunna [17]

Answer:

press the b that i in a dark shade that is bold

3 0
3 years ago
What kinds of circumstances would lead you to writing a function versus using a loop? please explain in simple terms
GREYUIT [131]

Answer:

1. You want to use parameters

2. You don't want your program to run multiple times

3. You want to call that snippet of code throughout your program

hope this helped :D

5 0
3 years ago
My assignment asks me to write a python program using if, elif, and else that takes a user's salary and calculates the tax based
Goryan [66]

Answer:

user_salary = int(input("Please Enter Your Salary : "))

if user_salary in range(10000, 20000):

   print("Tax = ",int(user_salary/100*1))

elif user_salary in range(20000, 30000):

   print("Tax = ",int(user_salary/100*2))

elif user_salary in range(30000, 40000):

   print("Tax = ",int(user_salary/100*3))

else:

   print("No Tax!")

Explanation:

8 0
2 years ago
With which type of test question should you leave yourself extra time to answer?
lapo4ka [179]

Answer:

c

Explanation:

5 0
3 years ago
Read 2 more answers
Which answer best describes an unsubsidized federal loan?
soldi70 [24.7K]

The answer is you are responsible for paying all the interest that accumulates on your loan. <span>For unsubsidized loans, the interest accumulates while you are in school, and 6 months after you graduate (or drop out) you will start paying on your loans. </span>

6 0
3 years ago
Other questions:
  • Create a file with a 20 lines of text and name it "lines.txt". Write a program to read this a file "lines.txt" and write the tex
    12·1 answer
  • A(n) _____ might be written by a programmer or it might be created through a DBMS utility program.
    6·1 answer
  • &gt;&gt;&gt; from hog import *
    15·1 answer
  • To use a macro, a user must first <br> ____ a macro and then <br> ____the macro.
    8·1 answer
  • What Temperature does mainframe need
    15·2 answers
  • What are the characteristics of 1st generation computers​
    9·2 answers
  • 3<br> Select the correct answer.<br> What is the output of the following HTML code?<br>Please
    8·2 answers
  • Which are the correct commands to create and run an ReactJS project?
    10·1 answer
  • The batteries on electric vehicles are recharged using electricity from either a wall socket or dedicated charging unit.
    11·1 answer
  • All of the following are personal security safeguards except ________. Select one: A. send no valuable data via email B. Use htt
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!