Computer Science, asked by Neharika5655, 6 months ago

write a program to enter an integer. print sum of even digits.


import java. util. Scanner;
public class compute
{
public static void main ()
{
Scanner sc= new Scanner (System.in)
int num =0, dig =0, sum=0;
System.out.println("Enter an integer:);
num = sc.nextInt();
while (num>0)
{
dig = num%10;
num =num/10;
if(dig%2==0);
sum =sum +dig;
}
System.out.println( "Print sum of even digits:"+ sum) ;
}
}

Answers

Answered by muskanchoudhuri
0

Answer:

Java Code:

import java.util.Scanner;

public class Main {

public static void main(String[] Strings) {

Scanner input = new Scanner(System.in);

System.out.print("Input an integer between 0 and 1000: ");

int num = input.nextInt();

int firstDigit = num % 10;

int remainingNumber = num / 10;

int SecondDigit = remainingNumber % 10;

remainingNumber = remainingNumber / 10;

int thirdDigit = remainingNumber % 10;

remainingNumber = remainingNumber / 10;

int fourthDigit = remainingNumber % 10;

int sum = thirdDigit + SecondDigit + firstDigit + fourthDigit;

System.out.println("The sum of all digits in " + num + " is " + sum);

}

}

Copy

Sample Output:

Input an integer between 0 and 1000: 565

The sum of all digits in 565 is 16

Flowchart:

Flowchart: Java Data Type Exercises - Adds all the digits in the integer between 0 and 1000

Java Code Editor:

Answered by anushar2k3
0

Yeah...

The program you have written is right..

You can carry on with it..

Similar questions