Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Write a program that will print out all perfect numbers from 1 to n. To deter

ID: 3625870 • Letter: 1

Question

1) Write a program that will print out all perfect numbers from 1 to n. To determine if a number is perfect add up all the factors of the number that are less than the number . If the sum is equal to the number. it is perfect. For example, the factors of 6 are 1,2,3. Since the sum is 6,6 is a perfect number. The factor of 12 are 1,2,3,4,6. Since the sum is 16, 12 is not a perfect number.

2) Write a program that asks the user to enter the size of a triangle(an integer from 1 to 50). Display the triangle by writing lines of asterisks. The first line will have one asterisk, the next two, and so on, with each line having one more asterisk than the previous line, up to the number entered by the user. On the next line write one fewer asterisk and continue by decreasing the number of asterisks by 1 for each successive line until only one asterisk is displayed. Use nested for loops : the outside loop controls the number of lines to write, and the inside loop controls the number of asterisks to displays on a line. For example, if the user enters 3, the output would be
*
**
***
**
*

Explanation / Answer

public class Perfect { public static void main(String args[]){ perfect(1000); } public static void perfect(int n){ int i = 1; int j = 1; int sum = 0; while (i