0% found this document useful (0 votes)
7 views8 pages

Java Task1

The document contains Java programs that demonstrate various number classifications such as Harshad, Duck, Spy, and Buzz numbers, along with utility programs for checking even/odd status, leap years, LCM and GCD calculations, removing duplicate characters, counting words, character frequency, moving zeros in an array, and finding a missing number in a sequence. Each program includes a brief definition and example, followed by the corresponding Java code. The document serves as a collection of algorithms and coding examples for basic number theory and string manipulation tasks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Java Task1

The document contains Java programs that demonstrate various number classifications such as Harshad, Duck, Spy, and Buzz numbers, along with utility programs for checking even/odd status, leap years, LCM and GCD calculations, removing duplicate characters, counting words, character frequency, moving zeros in an array, and finding a missing number in a sequence. Each program includes a brief definition and example, followed by the corresponding Java code. The document serves as a collection of algorithms and coding examples for basic number theory and string manipulation tasks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

TASK

1. Harshad Number (Niven Number)


Definition: Number divisible by sum of its digits.

Example:
18 → 1 + 8 = 9
18 % 9 == 0

public class HarshadNumber {

public static void main(String[] args) {

int num = 18;

int n = num;

int sum = 0;

while (n > 0) {

sum += n % 10;

n /= 10;

if (num % sum == 0)

[Link](num + " is a Harshad Number");

else

[Link](num + " is NOT a Harshad Number");

}
}

2. Duck Number
Definition: Number contains zero but must not start with zero.

Example:
705 → Duck Number
1205 → Duck Number
035 → Not Duck Number

public class DuckNumber {

public static void main(String[] args) {

String num = "705";


if ([Link](0) == '0') {

[Link]("Not a Duck Number");

return;

if ([Link]("0"))

[Link]("Duck Number");

else

[Link]("Not a Duck Number");

}
}

3. Spy Number
Definition: Sum of digits = Product of digits

Example:
112
Digits → 1, 1, 2
Sum = 1+1+2 = 4
Product = 1×1×2 = 2

digits sum = digits product

public class SpyNumber {

public static void main(String[] args) {

int num = 112;

int sum = 0, prod = 1, n = num;

while (n > 0) {

int digit = n % 10;

sum += digit;

prod *= digit;

n /= 10;

if (sum == prod)

[Link](num + " is a Spy Number");

else

[Link](num + " is NOT a Spy Number");


}
}

4. Buzz Number
Definition: Ends with 7 OR divisible by 7.

public class BuzzNumber {

public static void main(String[] args) {

int num = 147;

if (num % 7 == 0 || num % 10 == 7)

[Link](num + " is a Buzz Number");

else

[Link](num + " is NOT a Buzz Number");

}
}
5.

Even/Odd Without Using % Operator


public class EvenOdd {

public static void main(String[] args) {

int num = 11;

if ((num & 1) == 0)

[Link]("Even");

else

[Link]("Odd");

6. Leap Year Check


public class LeapYear {

public static void main(String[] args) {

int year = 2024;

if (year % 400 == 0)

[Link]("Leap Year");
else if (year % 100 == 0)

[Link]("Not a Leap Year");

else if (year % 4 == 0)

[Link]("Leap Year");

else

[Link]("Not a Leap Year");

7. LCM & GCD Program


public class LcmGcd {
public static void main(String[] args) {
int a = 12, b = 18;
int x = a, y = b;

// GCD
while (y != 0) {
int temp = y;
y = x % y;
x = temp;
}
int gcd = x;

// LCM Formula
int lcm = (a * b) / gcd;

[Link]("GCD = " + gcd);


[Link]("LCM = " + lcm);
}
}

[Link] Duplicate Characters


public class RemoveDuplicateChar {

public static void main(String[] args) {

String s = "banana";

String result = "";

for (char ch : [Link]()) {

if ([Link](ch) == -1)

result += ch;

[Link](result);

}
}

9. Count Words in String

public class WordCount {

public static void main(String[] args) {

String s = "Java is easy";

String[] words = [Link]().split("\\s+");

[Link]("Word Count = " +


[Link]);

}
}

10.
Character Frequency Count
Example:

Input → "aaabbc"
Output → a=3, b=2, c=1
public class CharFrequency {

public static void main(String[] args) {

String s = "aaabbc";

int[] count = new int[256];

for (char ch : [Link]()) count[ch]++;

for (int i = 0; i < 256; i++) {

if (count[i] > 0)

[Link]((char)i + " = " + count[i]);

11.

Move All Zeros to the End

Input:

[1,0,3,0,5]

Output:

[1,3,5,0,0]

public class MoveZeros {

public static void main(String[] args) {

int[] arr = {1, 0, 3, 0, 5};

int index = 0;

// Move non-zero elements forward

for (int num : arr) {

if (num != 0) {

arr[index++] = num;

}
}

// Fill remaining positions with zero

while (index < [Link]) {

arr[index++] = 0;

// Print output

for (int n : arr) {

[Link](n + " ");

12.

Find Missing Number in Array


Condition:
Array contains numbers from 1 to n, but one number is missing.

Example:

Input → [1, 2, 4, 5]
Missing number = 3

public class MissingNumber {

public static void main(String[] args) {

int[] arr = {1, 2, 4, 5};

int n = [Link] + 1; // Since one number is missing

int totalSum = n * (n + 1) / 2;

int arraySum = 0;

for (int num : arr) {


arraySum += num;

int missing = totalSum - arraySum;

[Link]("Missing Number = " + missing);

You might also like