FUNCTIONS SOLUTIONS
[email protected]
Solution 1:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input the first number: ");
double x = sc.nextDouble();
System.out.print("Input the second number: ");
double y = sc.nextDouble();
System.out.print("Input the third number: ");
double z = sc.nextDouble();
System.out.print("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 java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num;
System.out.print("Enter an integer: ");
num = sc.nextInt();
if(isEven(num)) {
System.out.println("Number is even");
} else {
System.out.println("Number is odd");
}
}
[email protected]
public static boolean isEven(int number) {
if(number % 2 == 0) {
return true;
}
else {
return false;
}
}
}
Solution 3:
import java.util.Scanner;
public class Solution {
public static void main(String args[]) {
System.out.println("Please Enter a number : ");
Scanner sc = new Scanner(System.in);
int palindrome = sc.nextInt();
if(isPalindrome(palindrome)) {
System.out.println("Number : " + palindrome + " is a palindrome");
} else {
System.out.println("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) {
[email protected]
return true;
}
return false;
}
}
Solution 4: This is a DIY question & should be solved on your own.
Solution 5:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input an integer: ");
int digits = sc.nextInt();
System.out.println("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;
}
}