Computer Science, asked by Prathiksha1sweetie, 7 months ago

Write A Java Program, using while loop & for loop:-

4+8+12+........n

Best Answer Will Be Marked As Braniest! :)​

Answers

Answered by IamGenesis
1

Answer:

import java.util.Scanner;

class looping{

   public static void main(String[] args){

       Scanner sc = new Scanner(System.in);

       System.out.println("Enter the number of iterations needed");

       int n = sc.nextInt();

       int sum = 0;

       

       //using for loop

       for(int i=1;i<=n;i++){

           sum += 4*i;

       }

       

       //using while loop

       int i = 1;

       while(i<=n){

           sum += 4*i;

           i++;

       }

       

       System.out.println("The sum is: " + sum);

   }

}

Similar questions