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

Functions Solutions

Uploaded by

someshgondal2
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)
3 views3 pages

Functions Solutions

Uploaded by

someshgondal2
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

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

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) {

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

https://t.me/+QGUyTripaP4zNTcx
https://telegram.me/+QGUyTripaP4zNTcx

You might also like