To calculate sum of all these numbers divisible by 3
import java.util.Scanner;
public class Exercise {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int sum=0;
for(int i=0;i<=2*n;i=i+2)
{
if(i%3==0)
sum=sum+i;
}
System.out.print(sum);
}
}
Sample Input and Output
n = 5
sum 6
import java.util.Scanner;
public class Exercise {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int sum=0;
for(int i=0;i<=2*n;i=i+2)
{
if(i%3==0)
sum=sum+i;
}
System.out.print(sum);
}
}
Sample Input and Output
n = 5
sum 6
No comments:
Post a Comment