Java Program to print sum of even and odd digit of given number

Java Program to print sum of even and odd digit of given number


sum of even and odd digit of given number


When any integer value which ends in 0,2,4,6,8 is divided by two it is called as an even number

Example for even numbers – 34,-64,78,788

When any integer value which ends in 0,1,3,5,7,9 is not divided by two it is called as an odd number

Example for odd numbers – 33,-69,75,785

 

We can use a modular operator to find odd or even number in the given range.

if n%2==0,  n is an even number

if n%2==1,  n is an odd number

 

This program allows the user to enter a maximum number of digits and then, the program will find out even and odd digit of given number and  sum up odd and even numbers entered number using a 

Do While loop      



import java.util.Scanner;

class SumofEODigits

{

public static void main(String args[])

       {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number:");

int n=sc.nextInt();

int sume=0;

int sumo=0;

do{

int a=n%10;

if(a%2==0)

sume=sume+a;

else

sumo=sumo+a;

n=n/10;

}while(n!=0);

System.out.println("Sum of even Digits is: "+sume);

System.out.println("Sum of odd Digits is: "+sumo);

}

}


Example :

Number is: 12345

even number is 2,4 

sum is 10

odd number is:1, 3,5

sum is 9



Comments

Popular posts from this blog

Java Program for shop Bill to Calculate total amount of product with discount

RCM|Consequences of imbalanced Nutrition|WHO share daily need

Learn Data structures and algorithms concepts free online with certificate