[Go to site: main page, start]

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

Java Number Functions and Operations

The document contains a Java program that defines various functions for number manipulation, including calculating the sum of digits, reversing a number, checking for palindromes, calculating factorials, and determining prime numbers. It also includes a method to find and print prime numbers from an input array. The main method allows user interaction to perform these operations on user-provided numbers and arrays.

Uploaded by

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

Java Number Functions and Operations

The document contains a Java program that defines various functions for number manipulation, including calculating the sum of digits, reversing a number, checking for palindromes, calculating factorials, and determining prime numbers. It also includes a method to find and print prime numbers from an input array. The main method allows user interaction to perform these operations on user-provided numbers and arrays.

Uploaded by

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

import [Link].

Scanner;

public class NumberFunctions


{

// Function to calculate sum of digits of a number


public static int sumOfDigits(int number)
{
int sum = 0;
while (number > 0)
{
sum += number % 10;
number /= 10;
}
return sum;
}

// Function to reverse a number


public static int reverseNumber(int number)
{
int reverse = 0;
while (number != 0)
{
reverse = reverse * 10 + number % 10;
number /= 10;
}
return reverse;
}

// Function to check if a number is a palindrome


public static boolean isPalindrome(int number)
{
return number == reverseNumber(number);
}

// Function to calculate factorial of a number using loops


public static long factorial(int number) {
long fact = 1;
for (int i = 1; i <= number; i++)
{
fact *= i;
}
return fact;
}

// Function to check if a number is prime


public static boolean isPrime(int number)
{
if (number <= 1) {
return false;
}
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0)
{
return false;
}
}
return true;
}
// Function to find the prime numbers in an array
public static void findPrimesInArray(int[] array) {
[Link]("Prime numbers in the array: ");
for (int number : array) {
if (isPrime(number)) {
[Link](number + " ");
}
}
[Link]();
}

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);

// Input a number from the user


[Link]("Enter a number: ");
int number = [Link]();

// Perform various operations


[Link]("Sum of digits: " + sumOfDigits(number));
[Link]("Reversed number: " + reverseNumber(number));
[Link]("Is palindrome: " + isPalindrome(number));

[Link]("Enter a number: ");


int fact_number = [Link]();
[Link]("Factorial: " + factorial(fact_number));

// Input an array of numbers


[Link]("Enter the size of the array: ");
int size = [Link]();
int[] array = new int[size];

[Link]("Enter the elements of the array:");


for (int i = 0; i < size; i++) {
array[i] = [Link]();
}

// Find prime numbers in the array


findPrimesInArray(array);
[Link]();
}
}

Common questions

Powered by AI

The 'sumOfDigits' function robustly handles non-negative integers, correctly summing digits until the number becomes zero. However, it doesn't address negative numbers or input validation within itself, limiting its robustness. Improvements could include handling negative integers by processing their absolute values and potentially throwing exceptions or returning error values for invalid inputs .

The 'findPrimesInArray' function iterates over each element in the given array, calling 'isPrime' to determine if the number is prime. Prime numbers detected are printed out. Its complexity depends on the array size and the complexity of 'isPrime', resulting in O(m * sqrt(n)) overall complexity, where m is the array size and n is each number's average value .

The 'reverseNumber' function reverses an integer by initializing a variable 'reverse' to 0 and iteratively extracting the last digit of the number (using modulus 10) to shift and add to 'reverse' after multiplying 'reverse' by 10. The number is reduced using integer division by 10 until it reaches 0. The computational complexity is O(n), where n is the number of digits in the integer, as each digit is processed once .

The 'sumOfDigits' function implements a basic looping algorithm to compute the sum of digits of a given integer. It initializes a variable 'sum' to 0, then iteratively adds the last digit of the number (found using modulus 10) to 'sum' and removes that digit from the number using integer division by 10. This process repeats until the number is reduced to 0, thus yielding the total sum of digits .

The 'factorial' function, due to using 'long' for return type and calculations, is limited by the maximum value 'long' can represent in Java, approximately up to 20!, after which it risks overflow. Handling larger numbers without overflow requires alternative approaches like using BigInteger, which can accommodate very large integers but incurs additional memory and processing overhead .

The 'isPrime' function checks if a number is prime by first handling edge cases (numbers less than or equal to 1) and then iteratively testing divisibility from 2 up to half the number. This approach, with time complexity approximately O(sqrt(n)), can be improved by checking up to the square root of the number, incorporating checks of divisibility only by 2 and numbers in the form of 6k ± 1, which further reduces the number of divisibility tests needed .

The 'factorial' function uses an iterative approach, initializing 'fact' to 1 and then multiplying it by each integer from 1 up to the given number. This loop-based implementation avoids the overhead of recursive function calls, making it more efficient in terms of time and space complexity compared to recursive solutions. Its time complexity is O(n), where n is the given integer .

The main method serves as the entry point for execution in Java programs. In 'NumberFunctions', it coordinates user input for numbers, invokes various utility functions for numerical operations, and manages the overall flow of operations run in sequence. It allows the program to execute step-by-step numerical analysis based on user input .

The 'isPalindrome' function checks if a number is a palindrome by using the 'reverseNumber' function to reverse the digits of the number and then comparing the reversed number with the original. If they are identical, the number is a palindrome. In number theory, palindromic numbers are symmetric and exhibit interesting properties, often used in problems related to symmetry and reflexivity .

The 'NumberFunctions' class closes the scanner at the end of its main function after all input operations are complete to free up system resources and prevent resource leaks. Closing the scanner is crucial to ensuring that input buffers are flushed, any associated streams are closed, and system resources are managed efficiently .

You might also like