Write a java program called Question38 that does the followi

Write a java program called Question38 that does the following: Initialize a counter to 0. Get input for number of calculations to perform. Calculations are to be performed in a looping structure. Add 2 to the counter for each iteration inside the loop. Output the value of the counter after the loop completes. Be precise, import modules, include comments, prologue, etc. as needed.

Solution

Question38.java

import java.util.Scanner;


public class Question38 {

   public static void main(String[] args) {
       int counter = 0;
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter number of calculations to perform: \");
       int n= scan.nextInt();
       for(int i=0; i<n; i++){
           counter = counter + 2;
       }
       System.out.println(\"The value of the counter after the loop completes: \"+counter);
   }
  

}

Output:

Enter number of calculations to perform:
5
The value of the counter after the loop completes: 10

Write a java program called Question38 that does the following: Initialize a counter to 0. Get input for number of calculations to perform. Calculations are to

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site