wap to print numbers which are divisible by 2 and 3 and also print its sum using loop java
Answers
Answered by
0
Required Answer:-
Question:
- Write a program in Java to print numbers which are divisible by 2 as well as 3 and display their sum.
Solution:
Here is the code.
public class JavaBrainly {
public static void main(String[] args) {
int s=0;
System.out.println("Numbers divisible by 2 as well as 3 are as follows..");
for(int i=1;i<=100;i++)
{
if(i%3==0 && i%2==0)
{
System.out.print(i+" ");
s+=i;
}
}
System.out.println("\n\nSum of the numbers: "+s);
}
}
Explanation:
- We will create a loop that iterates 100 times(i = 1 to 100). Inside the loop, we will check if a number between 1 to 100 is divisible by both 3 and 2 or not. If it is divisible, then we will display the number and add it's value to the variable s. At last, we will display the sum.
Output is attached for verification.
Attachments:
Similar questions
Physics,
1 month ago
Computer Science,
1 month ago
Computer Science,
3 months ago
Science,
9 months ago
Biology,
9 months ago
Accountancy,
9 months ago