NPTEL 2020: PROGRAMMING IN JAVA WEEK 1:Q3 ANSWER

Consider First n even numbers starting from zero(0).Complete the code segment to calculate sum of all the numbers divisible by 3 from 0 to n. Print the sum.

Example:

Input: n = 5


0 2 4 6 8
Even number divisible by 3:0 6
sum:6

int result=1;
int i=0;
while(result<=n)
{
if(i%2==0)
{
if(i%3==0)
{
sum=sum+i;
}
result=result+1;
}
i=i+1;
}
System.out.println(sum);

Leave a Reply

Your email address will not be published. Required fields are marked *