Answers for "find sum of all odd numbers in a number java"

1

Java program to calculate sum of even and odd numbers in an array

// Java program to calculate sum of even and odd numbers in an array
import java.util.Scanner;
public class FindSumOf
{
   public static void main(String[] args)
   {
      int number, sumEven = 0, sumOdd = 0;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter number of elements in an array: ");
      number = sc.nextInt();
      int[] arrNumbers = new int[number];
      System.out.println("Please enter elements of the array: ");
      for(int a = 0; a < number; a++)
      {
         arrNumbers[a] = sc.nextInt();
      }
      for(int a = 0; a < number; a++)
      {
         if(arrNumbers[a] % 2 == 0)
         {
            sumEven = sumEven + arrNumbers[a];
         }
         else
         {
            sumOdd = sumOdd + arrNumbers[a];
         }
      }
      System.out.println("Sum of even numbers in an array: " + sumEven);
      System.out.println("Sum of odd numbers in an array : " + sumOdd);
      sc.close();
   }
}
Posted by: Guest on February-22-2021
0

to get sum of even digits java

//To find the sum of even digits in a given Number

    public static int getEvenDigitSum(int number){
        if(number<0){
            return -1;
        }
        int finalNumber=0;
        while(number>0){
           if((number%10)%2==0){
               finalNumber+=number%10;

           }
           number=number/10; //takes out last digit to test the next digit


        }
        return finalNumber;
    }
//Output: ex:number=12323 -- Ans: 4
Posted by: Guest on September-16-2020

Code answers related to "find sum of all odd numbers in a number java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language