-1

Have some loops homework to do, and need some help! Here are the 3 questions:

  1. Us the method below to take two integers and only output numbers divisible by ten. Make the list start with the largest of the numbers.

    public static void divisibleByTen( int start, int end )

The above method is the example on the HW sheet. I have no idea how to implement it. I also don't know how to start with the largest number. Right now, I don't know how to take user input into the loop, so I made an example with 10 and 100:

public class QuestionOne {

    public static void main(String [] args) {

        for (int i = 10; i <= 100; i += 10){

               System.out.println(i + "");
        }
   }

}


  1. Use the method below to output the triangle below. Assume the positive number is between 3 and 9.

    public static void printLeftUpper( int num)

Desired output is this number triangle:

1 2 3 4 5

1 2 3 4

1 2 3 

1 2 

1

Here's my code so far:

public class QuestionTwo {

    public static void main(String [] args) {

        for(int i = 5; i >= 1; i--) {

            for(int j = 1; j <= i; ++j) {

                System.out.print(j + " ");
            }

        System.out.println();
        }
    }

}


The third question I have ZERO idea how to start.

3.    public static void sumEvens( int begin, int end )

Use the method above to take in two numbers, called begin and end, inclusive, checks if the numbers between them are even. Include the numbers in the sum if they are even as well, and print out the sum of all such numbers.

Example: sumEven(16, 11) uses 16+14+12 = 42, and outputs "For numbers between 16 and 11, the sum of all even numbers is 42."

The help is GREATLY appreciated. Thanks so much!!

  • 2
    1) please limit your posts to one question 2) even if you don't know how to start, provide your thoughts about the problem. Please [edit] your question accordingly – OneCricketeer Nov 25 '19 at 00:54
  • 1
    3) You've been given method definitions, but you're not using them. You should not only write code in the main method – OneCricketeer Nov 25 '19 at 00:56
  • Hint: look at the "Remainder Operator" described in [the Java Tutorials](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html) – D.B. Nov 25 '19 at 01:01
  • Even means "divisible by two" – Thorbjørn Ravn Andersen Nov 25 '19 at 01:12
  • 1
    The first issue with your solution to problem 1 is there is no dvisibleByTen method. Please create one or at least try to create one, and try to call it. Then make a separate posting just for problem 1. I am happy to help people with their homework. I don't like doing their homwork for them. – AndyMan Nov 25 '19 at 01:38

2 Answers2

-2

Who likes homework? I've taken the liberty of doing Question 3, hope that helps!

import java.util.List;
import java.util.ArrayList;

public class OddEven {

public static void main(String[] args) {
    sumEvens(0,1000);
}

// Gets sum of odd and even numbers between a given range:
public static void sumEvens(int begin, int end)
{
    List<Integer> evenNumbers = new ArrayList<Integer>();
    List<Integer> oddNumbers = new ArrayList<Integer>();

    if (begin < end)
    {
        for (int i = begin; i <= end; i++)
        {   
            // Number is even:
            if (i % 2 == 0)
            {
                evenNumbers.add(i);
            }

            // Number is odd:
            else
            {
                oddNumbers.add(i);
            }
        }
    }

    else
    {
        for (int i = begin; i >= end; i--)
        {   
            // Number is even:
            if (i % 2 == 0)
            {
                evenNumbers.add(i);
            }

            // Number is odd:
            else
            {
                oddNumbers.add(i);
            }
        }
    }

    // Calculate even values:
    int evenSum = 0;
    for (int i: evenNumbers) {
        evenSum += i;
    }

    System.out.println("The sum of all even numbers is: " + evenSum);

    // Calculate odd values:
    int oddSum = 0;
    for (int i: oddNumbers) {
        oddSum += i;
    }

    System.out.println("The sum of all odd numbers is: " + oddSum);
}
}
Kez
  • 7
  • 2
-2
public class QuestionOne {

    public static void main(String [] args) {

        divisibleByTen( 1, 100 );
    }
    public static void divisibleByTen( int start, int end ){
        // reversal big to small (100 to 1)
        for(int i = end; i >=  start; i--){
            // use mod (%) to check if i is divisible by 10
            if( i%10 == 0 ){
                // it is then output
                System.out.println(i);
            }
        }
    }
}

Question 2 is correct.

Question 3

public static void main(String [] args) {

    sumEvens( 16, 10);
}
public static void sumEvens( int begin, int end ){
    int start = 0;
    int last = end;
    int sum = 0;
    if(begin > end){
        start = end;
        end = begin;
    }else{
        start = begin;
    }

    for(int i = start; i <= end; i++){
        // get even integers

        if(i%2 == 0){
            sum += i; 
        }
    }
    System.out.println("For numbers between " +begin+ " and " +last+ ", the sum of all even numbers is " +sum);
  }
Safi
  • 11
  • 1