Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Write a function that uses a for loop to print five consecutive number (1).docx

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige

Hier ansehen

1 von 2 Anzeige

Write a function that uses a for loop to print five consecutive number (1).docx

Herunterladen, um offline zu lesen

Write a function that uses a for loop to print five consecutive numbers, and prints their sum. At the end. it would print the average. The output should have the following format:
Solution
ForTest.java
public class ForTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
displayValues();
}
public static void displayValues(){
int sum = 0;
System.out.println(\"Number\\tSum\");
for(int i=4; i<=8; i++){
sum = sum + i;
System.out.println(i+\"\\t\"+sum);
}
System.out.println(\"Average = \"+(sum/5));
}
}
Output:
Number   Sum
4   4
5   9
6   15
7   22
8   30
Average = 6
.

Write a function that uses a for loop to print five consecutive numbers, and prints their sum. At the end. it would print the average. The output should have the following format:
Solution
ForTest.java
public class ForTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
displayValues();
}
public static void displayValues(){
int sum = 0;
System.out.println(\"Number\\tSum\");
for(int i=4; i<=8; i++){
sum = sum + i;
System.out.println(i+\"\\t\"+sum);
}
System.out.println(\"Average = \"+(sum/5));
}
}
Output:
Number   Sum
4   4
5   9
6   15
7   22
8   30
Average = 6
.

Anzeige
Anzeige

Weitere Verwandte Inhalte

Weitere von lez31palka (20)

Aktuellste (20)

Anzeige

Write a function that uses a for loop to print five consecutive number (1).docx

  1. 1. Write a function that uses a for loop to print five consecutive numbers, and prints their sum. At the end. it would print the average. The output should have the following format: Solution ForTest.java public class ForTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub displayValues(); } public static void displayValues(){ int sum = 0; System.out.println("NumbertSum"); for(int i=4; i<=8; i++){ sum = sum + i; System.out.println(i+"t"+sum); } System.out.println("Average = "+(sum/5)); } } Output: Number  Sum 4  4 5  9 6  15 7  22
  2. 2. 8Â Â 30 Average = 6

×