write a program to find the sum of first n even natural numbers
Answers
Answered by
0
Answer:
Explanation:
Given a number n. The problem is to find the sum of first n even numbers.
Examples:
Input : n = 4
Output : 20
Sum of first 4 even numbers
= (2 + 4 + 6 + 8) = 20
Input : n = 20
Output : 420
Answered by
0
Answer:
import java.util.Scanner;
class sumofnaturalevennum
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Enter n : ");
int n = input.nextInt();
int sum = 0;
int num = 1;
for(int count=1; count<=n; ){
if(num%2 == 0){
sum += num;
count++;
}
num++;
}
System.out.println("Sum: "+sum);
}
}
Explanation:
Similar questions
Math,
17 days ago
Math,
1 month ago
Math,
1 month ago
Environmental Sciences,
9 months ago
English,
9 months ago