Computer Science, asked by rifatdeshpande, 3 months ago

Write a java program to print the sum of even and odd digits of a number​

Answers

Answered by kamalrajatjoshi94
0

Answer:

Program:-

import java.util.*;

public class Sum_EvenDigits

{

public static void main(String args[ ])

{

Scanner in=new Scanner(System.in);

int n,a=0,s=0;

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

n=in.nextInt();

while(n!=0)

{

a=n%10;

n=n/10;

if(a%2==0)

s=s+a;

}

System.out.println("The sum of even digits="+s);

}

}

  • The first photo is the program
  • The second photo is the input
  • The third photo is the output

Attachments:
Answered by BrainlyProgrammer
1

Answer:

//Java program to print the sum of even and odd digits

//Easiest way

import java.util.*;

class abc{

public static void main (String ar []){

Scanner sc=new Scanner (System.in);

int n=sc.nextInt(); //Entera a number

int k=0,s=0;

while (n!=0){

s+=(n%2==0)? n%10:0;

//Here I had used ternary operator..

k+=(n%2!=0)?n%10:0;

n/=10;

}

System.out.println("Sum of Even digits"+s+"\nSum of odd digits"+k);

}

}

Variable Description:-

  • k= sum variable for odd numbers
  • s= sum variable for odd numbers
  • n= input variable

Attachments:
Similar questions