If the sum of even digits is equal to the sum of odd digits, then such numbers are called lead numbers.
Example: 3669,3544 etc
Say for example, if you have 3669 then sum of even numbers in the given number
6+6 = 12
Sum of odd numbers in the given number
3+9 = 12
As it can be seen that sum of even numbers and sum of odd numbers are equal. Hence, it is a Lead number.
Let's see the Java program to determine if a number is LEAD Number or not.
import java.util.Scanner;
class LEAD
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n, sum1 = 0,sum2 = 0;
System.out.println("Enter a number");
n = sc.nextInt();
while(n!=0)
{
int d = n%10;
if(d%2==0)
{
sum1 = sum1+d;
}
else
{
sum2 = sum2+d;
}
n = n/10;
}
if(sum1 == sum2)
{
System.out.println("Given number is a lead number");
}
else
{
System.out.println("Given number is not a lead number");
}
}
}
class LEAD
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n, sum1 = 0,sum2 = 0;
System.out.println("Enter a number");
n = sc.nextInt();
while(n!=0)
{
int d = n%10;
if(d%2==0)
{
sum1 = sum1+d;
}
else
{
sum2 = sum2+d;
}
n = n/10;
}
if(sum1 == sum2)
{
System.out.println("Given number is a lead number");
}
else
{
System.out.println("Given number is not a lead number");
}
}
}
Demonstration:
2. A class namely LEAD have been defined.
3. A main method have been defined.
4. Object of Scanner class have been created.
5. A Number has been demanded from user by giving a message.
6. To accept and store the number, a variable namely n has been defined of int data type
7. Then, two different variables namely
a) sum1 - This variable will store the sum of even digits. First, it is initialized with 0.
b) sum2 - This variable will store the sum of odd digits. First, it is initialized with 0.
Now, let us see the working:
while(n!=0)
This loop iteration will continue till the index position doesn't becomes 0.
Refer to the below diagram for clear understanding.
As it can be seen that loop will start its operation from index position 3 and the element at index 3 is 9. It will continue till index position 0 reaches.
For example, in first loop iteration, first element 9 will be extracted, then with the help of if else statement, it will be examines if 9 is divisible by 2 or not. If it is divisible by 2, then the number will be added to the sum1 variable or else it will be added to the sum2 variable. In this case, 9 is not divisible by 2 and hence, it will be added to the sum2 variable.
Like this, it will keep on checking the individual numbers until the index position does not reaches 0.
n = n/10; will ensure that the number is deleted permanently deleted.
In previous operation, we have seen that the first extracted number using n%10 was 9. Immediately, after the if...else operation, n = n/10; will be executed which means 3669 is now going to be divided by 10 resulting to 366.9. This operation will permanently delete this 9 from n variable. so that in next iteration, 9 doesn't gets counted during execution of the while loop. Like this, it will keep on deleting the digits from n variable.
Here is the final outcome of the program:
As 3669 is taken as the example, hence
sum1 is carrying = 12 (i.e. 6+6)
sum2 is carrying = 12 (i.e. 3+9)
Now, with the help of if...else conditional statement outside the while loop, it is getting identified if the numbers contained within sum1 and sum2 variables are equal or not. If they are equal, it is a Lead number or else, it is not a lead number.
Now, write this program and test it with various numbers and increase your knowledge.
Happy Coding.......