Write a program in Java to input a number and check whether it is a Lead number or not.
A number is said to be a Lead number if the sum of even digits is equal to the sum of odd
digits of the number. For example: 6374 (6 + 4 = 3 + 7).
Answers
import java.util.Scanner;
public class LeadNumber {
static boolean isLead(int number) {
int oddSum = 0, evenSum = 0;
for (int i = number; i != 0; i /= 10) {
int currentDigit = i % 10;
if (currentDigit % 2 = = 0)
evenSum += currentDigit;
else oddSum += currentDigit;
}
return evenSum = = oddSum;
}
public static void main(String[ ] args) {
System.out.print("Enter a number - ");
int number = new Scanner(System.in).nextInt( );
System.out.printf("It’s%sa Lead number", isLead(number) ? " " : " not ");
}
}
Question:-
Write a program to input a number and check whether it is a Lead number or not. A number is said to be a lead number if the sum of the even digits is equal to the sum of the odd digit.
Code:-
import java.util.*;
class Lead
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n=sc.nextInt();
int a=0;
while(n!=0)
{
int d=n%10;
if(d%2==0)
a+=d;
else
a-=d;
n/=10;
}
if(a==0)
System.out.println("Given number is a lead number.");
else
System.out.println("Given number is not a lead number.");
}
}