Computer Science, asked by AreetraSircar, 29 days ago

write a program in java that will take a number from user and print the sum of even digit present in that number using while loop structure.​

Answers

Answered by Navvya1207
3

Answer:

import java.util.Scanner;

public class abc

{

public static void main (String args[ ])

{

Scanner scan=new Scanner (System.in);

System.out println("Enter number");

int n=scan.nextInt();

itn sum=0;

while(n!=0)

{

int d= n%10;

if (d%2==0)

sum+=d;

n=n/10;

}

System.out.println(sum);

Explanation:

  • (Line 1) Importing scanner class for input
  • (Line 2 and 3) Creating class
  • Line 4) Creating object of scanner class
  • (line 5) Inputting number by user
  • (line 6) delcaring a variable to allow the program to store the value of no. in it
  • (line 7) defining sum variable with 0 before hand to avoid further errors
  • (line 8) starting with while loop with the testing condition for extracting the digits of the no.
  • (line 9) extracting digits in the variable d
  • (line 10) checking if the digit is even
  • (line 11) if it is even adding it to sum variable
  • (line 12) update condition of the loop
  • (line 13) out of the loop, printing the sum value
Similar questions