Write a complete program to find the sum of the following series: 2, 4, 6,. 40 U
ID: 3782888 • Letter: W
Question
Write a complete program to find the sum of the following series: 2, 4, 6,. 40 Using for loop Using do-while loop Write a class called Lab1 which contains Main method Inside main method write two program segments: one is for loop and the other is do-while loop Compile and run your program. To receive full credit your program should compile and run with no errors. Upload the Lab1 .java file to blackboard. Write the following comments at the beginning of your program://Programmer: Your name//lab-1: Finding sum using repetition control structureExplanation / Answer
//Lab1.java
import java.util.Scanner;
public class Lab1
{
public static void main(String[] args)
{
int sum1 = 0;
int sum2 = 0;
for (int i = 2; i <= 40 ; i=i+2)
{
sum1 = sum1 + i;
}
System.out.println("Sum using for loop: " + sum1);
int j = 2;
do
{
sum2 = sum2 + j;
j = j+2;
}while(j <= 40);
System.out.println("Sum using do while loop: " + sum2);
}
}
/*
Output:
Sum using for loop: 420
Sum using do while loop: 420
*/
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.