Write a complete program to find the sum of the following series: 2, 4, 6 ..., 4
ID: 3782876 • 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 structure Date: Today's dateExplanation / Answer
a)
SumOfNosUsingFor.java
//Name: Provide Your name Here
//Finding the sum of the series using for loop
//Date : January 27,2017
public class SumOfNosUsingFor {
public static void main(String[] args) {
int sum=0;
//Sum of the series using for loop
for(int i=2;i<=40;i+=2)
sum+=i;
//Displaying the sum of the series
System.out.println("The Sum of the Series is "+sum);
}
}
_____________________
output:
The Sum of the Series is 420
____________________
b)
SumOfNosUsingDoWhile.java
//Name: Provide Your name Here
//Finding the sum of the series using do while loop
//Date : January 27,2017
public class SumOfNosUsingDoWhile {
public static void main(String[] args) {
int i=2,sum=0;
//Sum of the series using do while loop
do
{
sum+=i;
i+=2;
}while(i<=40);
//Displaying the sum of the series
System.out.println("The Sum of the Series is "+sum);
}
}
_______________________
Output:
The Sum of the Series is 420
_______Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.