0% found this document useful (0 votes)
75 views32 pages

Number Crunching (Digit Manipulation) and Number Based Problems

The document discusses digit manipulation techniques in Java, focusing on extracting and processing individual digits for various operations. It covers methods for separating digits, reconstructing numbers, and performing basic operations such as reversing a number, summing digits, and checking for palindromes. Additionally, it includes examples of number-based problems like checking for prime numbers, calculating factorials, and finding the GCD of two numbers.

Uploaded by

bharathkandhan96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views32 pages

Number Crunching (Digit Manipulation) and Number Based Problems

The document discusses digit manipulation techniques in Java, focusing on extracting and processing individual digits for various operations. It covers methods for separating digits, reconstructing numbers, and performing basic operations such as reversing a number, summing digits, and checking for palindromes. Additionally, it includes examples of number-based problems like checking for prime numbers, calculating factorials, and finding the GCD of two numbers.

Uploaded by

bharathkandhan96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Number Crunching &

Number Based Problems


Digit manipulation
Number Crunching Using Digit Manipulation
 Digit manipulation is an essential aspect of number crunching, where individual digits of a
number are extracted, processed, and used to perform specific operations.

 Below are different scenarios and their implementations in Java for number crunching using digit
manipulation:

Why is Digit Manipulation Useful?

Digit manipulation is a foundational concept for:


 Solving puzzles and problems: Checking if a number is a palindrome, Armstrong number, etc.
 Building algorithms: Performing encryption, checksum verification, or custom transformations.
 Improving logical thinking: Understanding the structure of numbers.
Digit manipulation
Separation
 Separation refers to extracting digits of a number, either starting from the rightmost digit or the leftmost
digit. Below are detailed explanations of separating digits right-to-left and left-to-right, using both hard
and soft methods.

Separation (Right to Left)


Hard Method
The hard method uses modulus (%) and division (/) to extract the digits.
public class SeparationRightToLeftHard {
public static void main(String[] args) {
int num = 12345;
while (num != 0) {
int digit = num % 10; // Extract the last digit
System.out.print(digit + " ");
num /= 10; // Remove the last digit
}
}
}
Output:
54321
Digit manipulation
Separation
Soft Method
 The soft method calculates each digit by dividing the number by a place value (pv) and applying
modulus.
public class SeparationRightToLeftSoft
{
public static void main(String[] args)
{
int num = 12345;
int pv = 1; // Place value starts at 1
while (pv <= num)
{
int digit = (num / pv) % 10; // Extract digit based on place
value
System.out.print(digit + " ");
pv *= 10; // Move to the next place value
}
}
}
Output:
Digit manipulation
Separation
Separation (Left to Right)
Hard Method
 In the hard method, we first calculate the highest place value (pv) and then use it to extract digits.
public class SeparationLeftToRightHard {
public static void main(String[] args) {
int num = 12345;
int pv = 1; // Start with 1
while (num / pv >= 10) { // Determine the highest place value
pv *= 10;}
while (pv > 0) {
int digit = num / pv; // Extract the leftmost digit
System.out.print(digit + " ");
num %= pv; // Remove the extracted digit
pv /= 10; // Move to the next place value
}
}
}
Output:
12345
Digit manipulation
Separation
Soft Method
 The soft method uses a similar approach but directly divides the number by increasing place values
(pv) to extract digits.
public class SeparationLeftToRightSoft {
public static void main(String[] args) {
int num = 12345;
int pv = 1; // Start with 1
while (num / pv >= 10) {// Determine the highest place value
pv *= 10;}
System.out.println("Digits (Left to Right) - Soft Method:");
while (pv > 0) {
int digit = num / pv; // Extract the leftmost digit
System.out.print(digit + " ");
num %= pv; // Remove the extracted digit
pv /= 10; // Move to the next place value
}
}
}
Output:
12345
Digit manipulation
Formation:
Left-Side and Right-Side Formation
 The concepts of left-side formation and right-side formation relate to how a number is reconstructed
by iterating through its digits.

1. Left-Side Formation
 In left-side formation, the number is reconstructed starting from the most significant digit (leftmost
digit) to the least significant digit. Each new digit is added to the left of the current result.

Algorithm for Left-Side Formation


 Extract digits from left to right.
 Use a base variable initialized to 1.
 Multiply the extracted digit by the base and add it to the result.
 Update the base by multiplying it by 10 for the next digit
Digit manipulation
import java.util.Scanner;
public class LeftSideFormation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
int result = 0;
int base = 1; // Initialize base as 1
int pv = 1;
// Calculate place value (pv) for the most significant digit
while (num / pv >= 10) {
pv *= 10;}
while (pv > 0) {
int digit = num / pv; // Extract the most significant digit
result = digit * base + result; // Add digit * base to result
base *= 10; // Increase base by 10
num %= pv; // Remove the most significant digit
pv /= 10; // Reduce place value
}
System.out.println("The number formed (left-side): " + result);
}
}
Digit manipulation
Formation:
Right-Side Formation
 In right-side formation, the number is reconstructed starting from the least significant digit (rightmost
digit) to the most significant digit. Each new digit is added to the right of the current result.

Algorithm for Right-Side Formation

 Extract digits from right to left.


 Use a result variable initialized to 0.
 Multiply the result by 10 and add the extracted digit to it.
Digit manipulation
import java.util.Scanner;
public class RightSideFormation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the number
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int result = 0;
// Right-side formation
while (num > 0) {
int digit = num % 10; // Extract the least significant digit
result = result * 10 + digit; // Form the number by adding digits to the right
num /= 10; // Remove the extracted digit
}
// Output the formed number
System.out.println("The number formed (right-side): " + result);
}
}
Digit manipulation
Basic Operations in Digit Manipulation
Below are the basic operations commonly used in digit manipulation:

 Reverse a Number
 Sum of Digits
 Count Digits
 Check For Palindrome
 Multiplication of Digits
 Find Largest Digit
 Find Smallest Digit
 Check If All Digits Are Even
Digit manipulation
Reverse a Number
Reverse the digits of a number.

int num = 12345;


int reversed = 0;
while (num != 0)
{
int digit = num % 10; // Extract last digit
reversed = reversed * 10 + digit; // Append to result
num /= 10; // Remove last digit
}
System.out.println(reversed);

Output:
54321
Digit manipulation
Sum of Digits
Add all digits of a number.

int num = 12345;


int sum = 0;
while (num != 0)
{
sum += num % 10; // Add last digit
num /= 10; // Remove last digit
}
System.out.println(sum);

Output:
15
Digit manipulation
Count Digits
Count the number of digits in a number.

int num = 12345;


int count = 0;
while (num != 0)
{
count++;
num /= 10; // Remove last digit
}
System.out.println(count);

Output:
5
Digit manipulation

Check for Palindrome


Determine if a number reads the same forward and backward.

int num = 12321;


int original = num;
int reversed = 0;
while (num != 0)
{
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println(original == reversed ? "Palindrome" : "Not a Palindrome");

Output:
Palindrome
Digit manipulation
Multiplication of Digits
Find the product of all digits.

int num = 1234;


int product = 1;
while (num != 0)
{
product *= num % 10; // Multiply by last digit
num /= 10; // Remove last digit
}
System.out.println(product);

Output:
24
Digit manipulation
Find Largest Digit
Determine the largest digit in a number.

int num = 3927;


int maxDigit = 0;
while (num != 0)
{
int digit = num % 10;
maxDigit = Math.max(maxDigit, digit);
num /= 10;
}
System.out.println(maxDigit);

Output:
9
Digit manipulation
Find Smallest Digit
Determine the smallest digit in a number.

int num = 3927;


int minDigit = 9; // Start with the largest possible digit
while (num != 0)
{
int digit = num % 10;
minDigit = Math.min(minDigit, digit);
num /= 10;
}
System.out.println(minDigit);

Output:
2
Digit manipulation
Check if All Digits Are Even
Verify if all digits in the number are even.

int num = 2486;


boolean allEven = true;
while (num != 0)
{
if ((num % 10) % 2 != 0)
{
allEven = false;
break;
}
num /= 10;
}
System.out.println(allEven ? "All digits are even" : "Not all digits are even");

Output:
All digits are even
Number Based Problems
Check if a Number is Prime
A prime number is only divisible by 1 and itself.
public class PrimeCheck {
public static void main(String[] args) {
int num = 29; // Change to test different numbers
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
System.out.println(num + (isPrime ? " is a prime number." : " is not a prime."));
}
}
Number Based Problems
Find the Factorial of a Number
The factorial of n is the product of all positive integers up to n.

public class Factorial


{
public static void main(String[] args)
{
int num = 5; // Change to test different numbers
long factorial = 1;
for (int i = 1; i <= num; i++)
{
factorial *= i;
}
System.out.println("Factorial of " + num + " is " + factorial);
}
}
Number Based Problems
Find the GCD of Two Numbers
The greatest common divisor (GCD) is the largest number that divides both
numbers.

public class GCD


{
public static void main(String[] args)
{
int num1 = 56, num2 = 98;
while (num2 != 0)
{
int temp = num2;
num2 = num1 % num2;
num1 = temp;
}
System.out.println("GCD: " + num1);
}
}
Number Based Problems
Check if a Number is Perfect
A perfect number is equal to the sum of its proper divisors.

public class PerfectNumber {


public static void main(String[] args)
{
int num = 28; // Change to test different numbers
int sum = 0;
for (int i = 1; i < num; i++)
{
if (num % i == 0) {
sum += i;
}
}
System.out.println(num + (sum == num ? " is a perfect number." : " is not a perfect
number."));
}
}
Number Based Problems

Java NC L0,1.Right to Left


Java NC L0 2 count The number of digits
Java NC L0.3 Product
Java NC L0 4 Number of occurrences
Java NC L0 5.HARSHAD NUMBER
Java NC L0 6 product of digit at even place divisible by the sum of a digit in odd places
Java NC L0 7 find the greatest digit in the given number
Java NC L0 8 Prime Digit
Java NC L0.9 FindLeftmostDigit
Java NC L0 10.Digit odd or even
Number Based Problems

Java NC L1 1 Sum Of Digits between left and right


Java NC L1 2 Frequency OF flights
Java NC L1 3 Anagram
Java NC L1 4 strong Number
Java NC L1 5 Armstrong Number
Java NC L1 6 Reverse Number
Java NC L1 7 Palindrom
Java NC L1 8 Magic Number
Java NC L1 9 Adam Number
Java NC.L1 10 reverse k digits
Number Based Problems

Java_NC_L2_1 counts the number of 1’s in its binary form


Java_NC_L2_2.decimal to binary
Java_NC_L2_3.dec to HEXADECIMAL VALUE
Java_NC_L2_4.Binary to decimal
Java_NC_L2_5.Convert to a given base
Java_NC_L2_6.Armstrong Number in a range
Java_NC_L2_7.Adam number generation
Java_NC_L2_8.next largest palindrome
Java_NC_L2_9.RIGHT ROTATION
Java_NC_L2_10.LEFT ROTATION
Number Based Problems

Java NC L3 1 Kaprekar Number


Java NC L3 2 Happy Number
Java NC L3.3 Alternate digit swapping
Java NC L3 4 removes the duplicate occurrences
Java NC L3 5 Digit swapping
Java NC L3 6 odd even segregate
Java NC L3 7 IsRotated
Java NC L3 8 Increasing or Decreasing or Not
Java NC L3 9 pandigital number
Java NC L3 10 The next largest using some digits
Number Based Problems

java_NB_L0_1 factors
java_NB_L0_2 Factor count
java_NB_L0_3 sum of factors
java_NB_L0_4 Perfect number
java_NB_L0_5 ABUNDANT,PERFECT,DEFICIENT NUMBER
java_NB_L0_6 amicable pair
java_NB_L0_7 BETROTHED NUMBER
java_NB_L0_8 Print factors in pairs
java_NB_L0_9 GCD of 2 nos
java_NB_L0_1O LCM
Number Based Problems

java_NB_L1_1 Prime number


java_NB_L1_2 prime factors
java_NB_L1_3 Prime Factorization
java_NB_L1_4 Greatest Prime Factor
java_NB_L1_5 Find the Least Prime Factor
java_NB_L1_6 Unique prime factor count
java_NB_L1_7 Factors of LCM in descending order
java_NB_L1_8 Kth largest factor
java_NB_L1_9 Triperfect number
java_NB_L1_10 Perfect Number -GEN
Number Based Problems

java NB L2 1 Highly Composite number


java NB L2 4 BETROTHED NUMBER -GEN
java NB L2 5 PRIME NUMBER IN RANGE
java NB L2 6 Square free number
java NB L2 7 Count Pairs
java NB L2 8 n-Smooth number
java NB L2 9 UGLY NUMBER
java NB L2 10 UGLY NUMBER GEN
THANK YOU

You might also like