0% found this document useful (0 votes)
29 views3 pages

Simple Java Number Programs

The document contains Java programs that perform various number-related checks and operations. It includes functionalities to determine if a number is even or odd, check for primality, reverse a number, check if a number is a palindrome, and calculate the sum of its digits. Each program prompts the user for input and outputs the result based on the specified operation.

Uploaded by

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

Simple Java Number Programs

The document contains Java programs that perform various number-related checks and operations. It includes functionalities to determine if a number is even or odd, check for primality, reverse a number, check if a number is a palindrome, and calculate the sum of its digits. Each program prompts the user for input and outputs the result based on the specified operation.

Uploaded by

binduann
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

✅ Check if a number is Even or Odd

import [Link];

public class EvenOdd {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();

if (num % 2 == 0)
[Link]("Even Number");
else
[Link]("Odd Number");

[Link]();
}
}

✅ Check whether the number is prime number or not


import [Link];

public class SimplePrimeCheck {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int count = 0;

for (int i = 1; i <= num; i++) {


if (num % i == 0)
count++;
}

if (count == 2)
[Link]("Prime Number");
else
[Link]("Not a Prime Number");

[Link]();
}
}

✅ Reverse of a number
import [Link];
public class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int rev = 0;

while (num != 0) {
int digit = num % 10;
rev = rev * 10 + digit;
num = num / 10;
}

[Link]("Reversed Number: " + rev);


[Link]();
}
}

✅ Check whether the number is palindrome or not


import [Link];

public class PalindromeCheck {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int original = num, rev = 0;

while (num != 0) {
int digit = num % 10;
rev = rev * 10 + digit;
num = num / 10;
}

if (original == rev)
[Link]("It is a Palindrome Number.");
else
[Link]("It is NOT a Palindrome Number.");

[Link]();
}
}

✅ Sum of digits
import [Link];
public class SumOfDigits {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();

int sum = 0;

while (num > 0) {


int digit = num % 10; // Get the last digit
sum += digit; // Add it to sum
num = num / 10; // Remove the last digit
}

[Link]("Sum of digits = " + sum);


[Link]();
}
}

Common questions

Powered by AI

The SimplePrimeCheck class determines if a number is prime by counting the number of divisors the number has. It iterates from 1 to the number, increasing a count each time the number is divisible with no remainder. If the count equals 2, the number is prime; otherwise, it is not.

The method used in SimplePrimeCheck checks divisibility by all numbers up to the given number, making it inefficient for large numbers as it operates with a time complexity of O(n). More efficient algorithms, such as testing divisibility only up to the square root of a number or using the Sieve of Eratosthenes, could reduce computational complexity significantly.

The EvenOdd class determines whether a number is even or odd by using the modulus operator (%) to check the remainder when the number is divided by 2. If the remainder is 0, the number is even; otherwise, it is odd.

The ReverseNumber class reverses a number by continuously taking the last digit of the number using the modulus operator (%), then adding this digit to the reversed number after multiplying the current reversed number by 10 to shift digits left. The original number is then divided by 10 to remove the last digit. This process repeats until the number becomes 0.

The PalindromeCheck class verifies that a number is a palindrome by first reversing the original number using a process similar to the ReverseNumber class, then comparing the reversed number with the original. If they are equal, the number is a palindrome; otherwise, it is not.

The ReverseNumber program uses integer division and modulus operations to manipulate digits efficiently. The modulus operation extracts the least significant digit, and multiplying the reversed number by 10 helps in placing each extracted digit in the correct position. Integer division is used to discard the last digit from the original number. This avoids complex data structures and makes the algorithm simple and fast.

The SumOfDigits class computes the sum of digits by repeatedly extracting the last digit using the modulus operator (%), adding this digit to a sum, then removing the last digit by dividing the number by 10. This continues until the number becomes 0.

The SimplePrimeCheck can be modified to check divisibility only up to the square root of the number. Since a larger factor of the number must be paired with a smaller factor and the largest possible factor is the square root, iterating only up to the square root decreases the number of divisibility checks significantly, improving efficiency. Using this method reduces the time complexity from O(n) to O(√n)

Processing very large numbers in the PalindromeCheck class may result in integer overflow if the number exceeds the storage capacity of an integer. This would cause erroneous checks since the reversed number might not match the original number due to truncation or wrap-around effects. Adjustments such as using data types with larger storage (e.g., BigInteger) or developing custom algorithms to handle digit strings could mitigate these issues.

In SumOfDigits, performance could degrade for larger input sizes since each operation to extract and add digits involves multiple arithmetic calculations. Optimization might include directly parsing the number into a string and iterating over each character to convert and sum its numeric value, which could reduce overhead if string manipulation is more efficient. For extremely large numbers, considering concurrent processing of parts could also enhance performance.

You might also like