Write a complete program to find the sum of the following se

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

Solution

//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


*/

 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site