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

Write a program called SumAndAverage to produce the sum of 1, 2, 3, ellipsis, to

ID: 3837913 • Letter: W

Question

Write a program called SumAndAverage to produce the sum of 1, 2, 3, ellipsis, to N, where N is a positive integer number inputted by the user. The program should Use the While loop; Check the user's input, and ask the user re-input a valid value if N is not a positive integer number; Computer and display the average of 1, 2, 3, ellipsis to N; Display the output as ********* Please input a positive integer number N: 100 The sum from 1 to 100 is: 5050 The average is: 50.5 ********* modify the above program to complete the same tasks by using "do-while" loop and "for" loop.

Explanation / Answer

Question 1

SumAndAverage.java

import java.util.Scanner;


public class SumAndAverage {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print("Please input a positive number N: ");
       int n = scan.nextInt();
       while(n <= 0){
           System.out.print("Invalid input. Please input a positive number N: ");
           n = scan.nextInt();
       }
       int sum = 0;
       int i =1;
       while(i<=n){
           sum = sum + i;
           i++;
       }
       System.out.println("The sum from 1 to "+n+" is: "+sum);
       double average = sum/(double)n;
       System.out.println("The average is "+average);
   }

}

Output:

Please input a positive number N: 100
The sum from 1 to 100 is: 5050
The average is 50.5

Question 2

SumAndAverage.java

import java.util.Scanner;


public class SumAndAverage {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print("Please input a positive number N: ");
       int n = scan.nextInt();
       while(n <= 0){
           System.out.print("Invalid input. Please input a positive number N: ");
           n = scan.nextInt();
       }
       int sum = 0;
       int i =1;
       do{
           sum = sum + i;
           i++;
       }while(i<=n);
       System.out.println("The sum from 1 to "+n+" is: "+sum);
       double average = sum/(double)n;
       System.out.println("The average is "+average);
   }

}

Output:

Please input a positive number N: 100
The sum from 1 to 100 is: 5050
The average is 50.5

import java.util.Scanner;


public class SumAndAverage {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print("Please input a positive number N: ");
       int n = scan.nextInt();
       while(n <= 0){
           System.out.print("Invalid input. Please input a positive number N: ");
           n = scan.nextInt();
       }
       int sum = 0;
       for(int i=1;i<=n;i++){
           sum = sum + i;
       }
       System.out.println("The sum from 1 to "+n+" is: "+sum);
       double average = sum/(double)n;
       System.out.println("The average is "+average);
   }

}

Output:

Please input a positive number N: 100
The sum from 1 to 100 is: 5050
The average is 50.5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote