
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Lead Number in java
A lead number is a number whose sum of even digits is equal to the sum of odd digits. In this article, we will learn about lead numbers and programs to check if a number is a lead number or not.
Lead number example
Let's take the number 615341. The sum of even digits is 6 + 4 = 10. The sum of odd digits is 1 + 5 + 3 + 1 = 10. Here, the sum of even digits = the sum of odd digits, so 615341 is a lead number
Arithmetic Approach
This approach extracts digits using arithmetic operations like modulus and division. The modulus operator is to find the last digit of the number, while the division operator is to remove the last digit from the number.
Steps for implementation
The following steps explain how to check for a lead number using arithmetic operations.
Step 1 ? Initialize oddS and evenS variables to store the sum of odd digits and the sum of even digits, respectively.
Step 2 ? Take the input number from the user
Step 3 ? Extract the last digit of the number using the modulus operator.
Step 4 ? Check if the digit is odd or even. If the digit is odd, add it to the oddS variable; otherwise, if it is even, add it to evenS.
Step 5 ? Now remove the last digit of the number using the division operation.
Step 6 ? If evenS equals oddS, print 'It is a Lead Number'; otherwise print 'It is not a Lead Number'.
Implementation code
Below is the implementation of the arithmetic approach to check for a lead number.
import java.util.*; public class leadnumber { public static void main(String args[]) { int oddS=0,evenS=0; Scanner sc=new Scanner(System.in); System.out.print("Enter a number: "); int num=sc.nextInt(); while(num>0) { int rem=num%10; if(rem%2==0){ evenS=evenS+rem; } else { oddS=oddS+rem; } num=num/10; } if(evenS==oddS){ System.out.println("It is a Lead Number"); } else { System.out.println("It is not a Lead Number"); } } }
Output
Enter a number: 2343 It is a Lead Number Enter a number: 384746 It is not a Lead Number
String-based approachM
In this approach, we process the number as a string and iterate through the characters to extract the digits. When we process numbers as strings, it will prevent overflow, specifically while using very large numbers.
Steps for implementation
These steps explain how to use a string-based approach to check for a lead number.
Step 1 ? Initialize the variables oddS and evenS to store the sum of odd digits and the sum of even digits respectively.
Step 2 ? Take the input number from the user as a string.
Step 3 ? Iterate through each character of the string by converting it to an array of characters.
Step 4 ? After extracting a character, convert it to an integer to implement a calculation for checking if the digit is odd or even.
Step 5 ? If the digit is odd, add it to the oddS variable; otherwise, if it is even, add it to the evenS.
Step 6 ? If evenS equals oddS, print 'It is a Lead Number'; otherwise print 'It is not a Lead Number'.
Implementation code
Below is the implementation using the string-based approach to find lead numbers.
import java.util.*; public class leadnumber { public static void main(String args[]) { int oddS = 0, evenS = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); String num = sc.next(); sc.close(); for (char ch : num.toCharArray()) { int rem = Character.getNumericValue(ch); if (rem % 2 == 0) { evenS += rem; } else { oddS += rem; } } if (evenS == oddS) { System.out.println("It is a Lead Number"); } else { System.out.println("It is not a Lead Number"); } } }
Output
Enter a number: 8767 It is a Lead Number Enter a number: 5736 It is not a Lead Number
Recursion-based approach
We can also use a recursive approach instead of a loop to find if a number is a lead number or not. In this approach we use a recursive function that extracts the last digit and checks if it is even or odd repeatedly till the base case, which is the number equals zero, is met.
Steps for implementation
The following steps explain how to use recursion to check for a lead number.
Step 1 ? Take the number as input from the user to check for the lead number and initialize the variables oddS and evenS.
Step 2 ? Define a recursive function named findsum.
Step 3 ? Inside the recursive function, we will extract the last digit of the number using 'number%10' and check if it is even or odd.
Step 4 ? If it is even, add the digit to evenS. Otherwise, add to oddS.
Step 5 ? Recursively call the function again with the number after removing the last digit by calculating 'number/10'.
Step 6: ? The recursion should stop and return the sum of odd and even digits when the base case, which is 'num==0' is met.
Step 7 ? Now check if the even sum is equal to the odd sum or not. If they are equal print "It is a lead number" otherwise, print "It is not a lead number".
Implementation code
Below is the implementation using recursion to check for a lead number.
import java.util.*; public class leadnumber { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int num = sc.nextInt(); sc.close(); int[] sums = findsums(num, 0, 0); if (sums[0] == sums[1]) { System.out.println("It is a Lead Number"); } else { System.out.println("It is not a Lead Number"); } } public static int[] findsums(int num, int evenS, int oddS) { if (num == 0) { return new int[]{evenS, oddS}; } int rem = num % 10; if (rem % 2 == 0) { evenS += rem; } else { oddS += rem; } return findsums(num / 10, evenS, oddS); } }
Output
Enter a number: 2321 It is a Lead Number Enter a number: 4421 It is not a Lead Number