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

Java Functions for Number Operations

The document contains multiple Java solutions for different programming tasks, including calculating the average of three numbers, checking if a number is even or odd, determining if a number is a palindrome, and summing the digits of an integer. Each solution includes the necessary code and prompts for user input. Additionally, one solution is marked as a DIY question for the reader to solve independently.

Uploaded by

Pakeeza
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)
6 views3 pages

Java Functions for Number Operations

The document contains multiple Java solutions for different programming tasks, including calculating the average of three numbers, checking if a number is even or odd, determining if a number is a palindrome, and summing the digits of an integer. Each solution includes the necessary code and prompts for user input. Additionally, one solution is marked as a DIY question for the reader to solve independently.

Uploaded by

Pakeeza
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

FUNCTIONS SOLUTIONS

mehtab0614@[Link]
Solution 1:
import [Link];
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Input the first number: ");
double x = [Link]();
[Link]("Input the second number: ");
double y = [Link]();
[Link]("Input the third number: ");
double z = [Link]();
[Link]("The average value is " + average(x, y, z)+"\n" );
}

public static double average(double x, double y, double z) {


return (x + y + z) / 3;
}
}

Solution 2:
import [Link].*;

public class Solution {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num;

[Link]("Enter an integer: ");


num = [Link]();

if(isEven(num)) {
[Link]("Number is even");
} else {
[Link]("Number is odd");
}
}

mehtab0614@[Link]
public static boolean isEven(int number) {
if(number % 2 == 0) {
return true;
}
else {
return false;
}
}
}

Solution 3:
import [Link];

public class Solution {


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

if(isPalindrome(palindrome)) {
[Link]("Number : " + palindrome + " is a palindrome");
} else {
[Link]("Number : " + palindrome + " is not a palindrome");
}

}
public static boolean isPalindrome(int number) {
int palindrome = number; // copied number into variable
int reverse = 0;

while (palindrome != 0) {
int remainder = palindrome % 10;
reverse = reverse * 10 + remainder;
palindrome = palindrome / 10;
}
// if original and the reverse of number is equal means
// number is palindrome in Java
if (number == reverse) {

mehtab0614@[Link]
return true;
}
return false;
}
}

Solution 4: This is a DIY question & should be solved on your own.

Solution 5:
import [Link];
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Input an integer: ");
int digits = [Link]();
[Link]("The sum is " + sumDigits(digits));
}

public static int sumDigits(int n) {


int sumOfDigits = 0;
while(n > 0) {
int lastDigit = n % 10;
sumOfDigits += lastDigit;
n /= 10;
}

return sumOfDigits;
}
}

You might also like