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.
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 ratings0% found this document useful (0 votes)
11 views
Java Number Based Simple Programs
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.
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
You are on page 1/ 2
import java.util.
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) { System.out.print("Prime numbers in the array: "); for (int number : array) { if (isPrime(number)) { System.out.print(number + " "); } } System.out.println(); }
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input a number from the user
System.out.print("Enter a number: "); int number = scanner.nextInt();