Question

Function name: triangular Parameters: integer Returns: a list Description: Write a function called triangular that accepts one integer parameter that returns a list with the same number of terms as the value of the parameter. If the parameter is zero or a negative number, you should return an empty list A triangular number is the sum of a positive integer and all the integers before it. The series is as follows: 1, 3, 6, 10, 15, 21,
media%2F84a%2F84af222c-69e3-461e-b9c8-21
0 0
Add a comment Improve this question Transcribed image text
Answer #1

please fidn the code 1

import java.util.ArrayList;
import java.util.Scanner;

public class Triangular {
   public static void main(String args[]){
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter the size of the list: ");
       int size = sc.nextInt();
      
       ArrayList<Integer> list = triangular(size);
       System.out.println(list.toString());
   }
   public static ArrayList<Integer> triangular(int n){
       int sum=0;
       ArrayList<Integer> list1 = new ArrayList<Integer>();
       if(n<=0)
           return list1;
       else{
           for(int i=0;i<n;i++){
               sum = sum+(i+1);
               list1.add(sum);
           }
       }  
       return list1;
   }
}

2) mthod for sum list

private static int sumList(ArrayList lis1){
       int sum=0;
       for(int i=0;i<lis1.size();i++){
           sum = sum + (Integer)lis1.get(i);
       }
       return sum;
   }

Add a comment
Know the answer?
Add Answer to:
Function name: triangular Parameters: integer Returns: a list Description: Write a function called triangular that accepts...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT