SlideShare a Scribd company logo
1 of 2
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
8Â Â 30
Average = 6

More Related Content

More from lez31palka

Create a shell script file called q4-sh.docx
Create a shell script file called q4-sh.docxCreate a shell script file called q4-sh.docx
Create a shell script file called q4-sh.docxlez31palka
 
Create a python zork file based on the following UML- Use command func.docx
Create a python zork file based on the following UML- Use command func.docxCreate a python zork file based on the following UML- Use command func.docx
Create a python zork file based on the following UML- Use command func.docxlez31palka
 
Create a new python program- sniff_spoof_icmp-py- The program shall sn.docx
Create a new python program- sniff_spoof_icmp-py- The program shall sn.docxCreate a new python program- sniff_spoof_icmp-py- The program shall sn.docx
Create a new python program- sniff_spoof_icmp-py- The program shall sn.docxlez31palka
 
Create a new webpage called -activity2-html-- This new page must inclu.docx
Create a new webpage called -activity2-html-- This new page must inclu.docxCreate a new webpage called -activity2-html-- This new page must inclu.docx
Create a new webpage called -activity2-html-- This new page must inclu.docxlez31palka
 
Create a multi-client-single server application to check whether the n.docx
Create a multi-client-single server application to check whether the n.docxCreate a multi-client-single server application to check whether the n.docx
Create a multi-client-single server application to check whether the n.docxlez31palka
 
Create a list containing decimal numbers- Write a program to find the.docx
Create a list containing decimal numbers- Write a program to find the.docxCreate a list containing decimal numbers- Write a program to find the.docx
Create a list containing decimal numbers- Write a program to find the.docxlez31palka
 
Create a JAVA class called Staff that has the following attributes- St.docx
Create a JAVA class called Staff that has the following attributes- St.docxCreate a JAVA class called Staff that has the following attributes- St.docx
Create a JAVA class called Staff that has the following attributes- St.docxlez31palka
 
Create a Java class with the below attributes- Create a class called.docx
Create a Java class with the below attributes-  Create a class called.docxCreate a Java class with the below attributes-  Create a class called.docx
Create a Java class with the below attributes- Create a class called.docxlez31palka
 
Create a JAVA class called Faculty that has the following attributes-.docx
Create a JAVA class called Faculty that has the following attributes-.docxCreate a JAVA class called Faculty that has the following attributes-.docx
Create a JAVA class called Faculty that has the following attributes-.docxlez31palka
 
Create a garden project - Submit a project scope document - Submit R.docx
Create a garden project   - Submit a project scope document - Submit R.docxCreate a garden project   - Submit a project scope document - Submit R.docx
Create a garden project - Submit a project scope document - Submit R.docxlez31palka
 
Create a file named insert-php and add the new records in to your rese.docx
Create a file named insert-php and add the new records in to your rese.docxCreate a file named insert-php and add the new records in to your rese.docx
Create a file named insert-php and add the new records in to your rese.docxlez31palka
 
Corporate Fund started the year with o net asset value of $30-80- By y.docx
Corporate Fund started the year with o net asset value of $30-80- By y.docxCorporate Fund started the year with o net asset value of $30-80- By y.docx
Corporate Fund started the year with o net asset value of $30-80- By y.docxlez31palka
 
Create a database design ERD diagram for a Hotel Business and to conf.docx
Create a database design ERD diagram  for a Hotel Business and to conf.docxCreate a database design ERD diagram  for a Hotel Business and to conf.docx
Create a database design ERD diagram for a Hotel Business and to conf.docxlez31palka
 
Create a class called IntegerArrayHelper that has the following attrib.docx
Create a class called IntegerArrayHelper that has the following attrib.docxCreate a class called IntegerArrayHelper that has the following attrib.docx
Create a class called IntegerArrayHelper that has the following attrib.docxlez31palka
 
Create a Birthday Card app that accepts a person's name and date of bi.docx
Create a Birthday Card app that accepts a person's name and date of bi.docxCreate a Birthday Card app that accepts a person's name and date of bi.docx
Create a Birthday Card app that accepts a person's name and date of bi.docxlez31palka
 
Create a chart of veins- list the name of the vein- drains into (major.docx
Create a chart of veins- list the name of the vein- drains into (major.docxCreate a chart of veins- list the name of the vein- drains into (major.docx
Create a chart of veins- list the name of the vein- drains into (major.docxlez31palka
 
Crane Corporation earned $379-000 during a period when it had an avera.docx
Crane Corporation earned $379-000 during a period when it had an avera.docxCrane Corporation earned $379-000 during a period when it had an avera.docx
Crane Corporation earned $379-000 during a period when it had an avera.docxlez31palka
 
Country Complete the statement that creates the table- CREATE TABLE Co.docx
Country Complete the statement that creates the table- CREATE TABLE Co.docxCountry Complete the statement that creates the table- CREATE TABLE Co.docx
Country Complete the statement that creates the table- CREATE TABLE Co.docxlez31palka
 
Convert the below-dictated lines into Preoperative History and Physica.docx
Convert the below-dictated lines into Preoperative History and Physica.docxConvert the below-dictated lines into Preoperative History and Physica.docx
Convert the below-dictated lines into Preoperative History and Physica.docxlez31palka
 
Could you explain why the correct answer is C and not B or D (I unders.docx
Could you explain why the correct answer is C and not B or D (I unders.docxCould you explain why the correct answer is C and not B or D (I unders.docx
Could you explain why the correct answer is C and not B or D (I unders.docxlez31palka
 

More from lez31palka (20)

Create a shell script file called q4-sh.docx
Create a shell script file called q4-sh.docxCreate a shell script file called q4-sh.docx
Create a shell script file called q4-sh.docx
 
Create a python zork file based on the following UML- Use command func.docx
Create a python zork file based on the following UML- Use command func.docxCreate a python zork file based on the following UML- Use command func.docx
Create a python zork file based on the following UML- Use command func.docx
 
Create a new python program- sniff_spoof_icmp-py- The program shall sn.docx
Create a new python program- sniff_spoof_icmp-py- The program shall sn.docxCreate a new python program- sniff_spoof_icmp-py- The program shall sn.docx
Create a new python program- sniff_spoof_icmp-py- The program shall sn.docx
 
Create a new webpage called -activity2-html-- This new page must inclu.docx
Create a new webpage called -activity2-html-- This new page must inclu.docxCreate a new webpage called -activity2-html-- This new page must inclu.docx
Create a new webpage called -activity2-html-- This new page must inclu.docx
 
Create a multi-client-single server application to check whether the n.docx
Create a multi-client-single server application to check whether the n.docxCreate a multi-client-single server application to check whether the n.docx
Create a multi-client-single server application to check whether the n.docx
 
Create a list containing decimal numbers- Write a program to find the.docx
Create a list containing decimal numbers- Write a program to find the.docxCreate a list containing decimal numbers- Write a program to find the.docx
Create a list containing decimal numbers- Write a program to find the.docx
 
Create a JAVA class called Staff that has the following attributes- St.docx
Create a JAVA class called Staff that has the following attributes- St.docxCreate a JAVA class called Staff that has the following attributes- St.docx
Create a JAVA class called Staff that has the following attributes- St.docx
 
Create a Java class with the below attributes- Create a class called.docx
Create a Java class with the below attributes-  Create a class called.docxCreate a Java class with the below attributes-  Create a class called.docx
Create a Java class with the below attributes- Create a class called.docx
 
Create a JAVA class called Faculty that has the following attributes-.docx
Create a JAVA class called Faculty that has the following attributes-.docxCreate a JAVA class called Faculty that has the following attributes-.docx
Create a JAVA class called Faculty that has the following attributes-.docx
 
Create a garden project - Submit a project scope document - Submit R.docx
Create a garden project   - Submit a project scope document - Submit R.docxCreate a garden project   - Submit a project scope document - Submit R.docx
Create a garden project - Submit a project scope document - Submit R.docx
 
Create a file named insert-php and add the new records in to your rese.docx
Create a file named insert-php and add the new records in to your rese.docxCreate a file named insert-php and add the new records in to your rese.docx
Create a file named insert-php and add the new records in to your rese.docx
 
Corporate Fund started the year with o net asset value of $30-80- By y.docx
Corporate Fund started the year with o net asset value of $30-80- By y.docxCorporate Fund started the year with o net asset value of $30-80- By y.docx
Corporate Fund started the year with o net asset value of $30-80- By y.docx
 
Create a database design ERD diagram for a Hotel Business and to conf.docx
Create a database design ERD diagram  for a Hotel Business and to conf.docxCreate a database design ERD diagram  for a Hotel Business and to conf.docx
Create a database design ERD diagram for a Hotel Business and to conf.docx
 
Create a class called IntegerArrayHelper that has the following attrib.docx
Create a class called IntegerArrayHelper that has the following attrib.docxCreate a class called IntegerArrayHelper that has the following attrib.docx
Create a class called IntegerArrayHelper that has the following attrib.docx
 
Create a Birthday Card app that accepts a person's name and date of bi.docx
Create a Birthday Card app that accepts a person's name and date of bi.docxCreate a Birthday Card app that accepts a person's name and date of bi.docx
Create a Birthday Card app that accepts a person's name and date of bi.docx
 
Create a chart of veins- list the name of the vein- drains into (major.docx
Create a chart of veins- list the name of the vein- drains into (major.docxCreate a chart of veins- list the name of the vein- drains into (major.docx
Create a chart of veins- list the name of the vein- drains into (major.docx
 
Crane Corporation earned $379-000 during a period when it had an avera.docx
Crane Corporation earned $379-000 during a period when it had an avera.docxCrane Corporation earned $379-000 during a period when it had an avera.docx
Crane Corporation earned $379-000 during a period when it had an avera.docx
 
Country Complete the statement that creates the table- CREATE TABLE Co.docx
Country Complete the statement that creates the table- CREATE TABLE Co.docxCountry Complete the statement that creates the table- CREATE TABLE Co.docx
Country Complete the statement that creates the table- CREATE TABLE Co.docx
 
Convert the below-dictated lines into Preoperative History and Physica.docx
Convert the below-dictated lines into Preoperative History and Physica.docxConvert the below-dictated lines into Preoperative History and Physica.docx
Convert the below-dictated lines into Preoperative History and Physica.docx
 
Could you explain why the correct answer is C and not B or D (I unders.docx
Could you explain why the correct answer is C and not B or D (I unders.docxCould you explain why the correct answer is C and not B or D (I unders.docx
Could you explain why the correct answer is C and not B or D (I unders.docx
 

Recently uploaded

Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptxAneriPatwari
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Celine George
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 

Recently uploaded (20)

Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptx
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 

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

  • 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