Ask Question
6 August, 00:46

Write a java program that finds the sum of all even numbers between 1 and 55.

Also what modification would you do to the program if you wanted the sum of all odd numbers between 1 and 55

+4
Answers (1)
  1. 6 August, 03:05
    0
    For sum of even Number:

    public class even{

    public static void main (String []args) {

    int sum = 0;

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

    if (i%2==0) {

    sum = sum + i;

    }

    }

    System. out. println (sum);

    }

    }

    Modification for sum of odd:

    change the condition of if statement:

    i%2==1 instead of i%2==0

    Explanation:

    create the main function and declare the variable with zero.

    Then, take a for loop for traversing the number from 1 from 55.

    Inside the for loop, if statement check the condition for even number

    A number is even if number divide by 2 and give zero reminder.

    so, number%2==0 it is the condition for even number.

    then add the even number until condition TRUE.

    and finally print the result.

    Modification for sum of odd number:

    A number is odd, if number divide by 2 and give one reminder.

    it means, number%2==1 it is the condition for odd number.

    just change the if condition with above.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a java program that finds the sum of all even numbers between 1 and 55. Also what modification would you do to the program if you ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers