0% found this document useful (0 votes)
2 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.

Uploaded by

yatakonakiran2
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 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.

Uploaded by

yatakonakiran2
Copyright
© © All Rights Reserved
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();

// Perform various operations


System.out.println("Sum of digits: " + sumOfDigits(number));
System.out.println("Reversed number: " + reverseNumber(number));
System.out.println("Is palindrome: " + isPalindrome(number));

System.out.print("Enter a number: ");


int fact_number = scanner.nextInt();
System.out.println("Factorial: " + factorial(fact_number));

// Input an array of numbers


System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
int[] array = new int[size];

System.out.println("Enter the elements of the array:");


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

// Find prime numbers in the array


findPrimesInArray(array);
scanner.close();
}
}

You might also like