0% found this document useful (0 votes)
4 views

Functions Solutions

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)
4 views

Functions Solutions

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
You are on page 1/ 3

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;
}
}

You might also like