Computer Applications
Define a class to accept a number and check if the sum of the first digit and the last digit is an even number or an odd number.
Example:
N = 2396 N = 9316
First digit: 2 9
Last digit: 6 6
Sum: 8 15
Output: Sum is even Sum is odd
Answer
import java.util.Scanner;
public class FirstLastSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
int lastDigit = n % 10;
int firstDigit = n;
while (firstDigit >= 10) {
firstDigit /= 10;
}
int sum = firstDigit + lastDigit;
System.out.println("First digit: " + firstDigit);
System.out.println("Last digit: " + lastDigit);
System.out.println("Sum: " + sum);
if (sum % 2 == 0)
System.out.println("Sum is even");
else
System.out.println("Sum is odd");
}
}Output
Related Questions
Define a class to accept values into a 3x3 integer array and print the product of each row elements.
Example:
3 1 2 4 2 1 5 1 2 Output:
Row 0 – 6
Row 1 – 8
Row 2 – 10Define a class to overload the method transform as follows:
int transform(int n) – to return the sum of the digits of the given number
Example: n = 458
output : 17void transform(String s) – to convert the given String to upper case and print
Example: if S = “Blue”
Output : BLUEvoid transform (char ch) – to print the character ch in 3 rows and 3 columns using nested loops.
Example: if ch = ‘@’
Output :@@@ @@@ @@@Define a class to accept a string. Check if it is a Special String or not.
A String is Special if the number of vowels equals to the number of consonants.
Example: MINE
Number of vowels = 2
Number of Consonants = 2Define a class to accept 10 integers in an array, search for the given value using the Linear Search technique and print appropriate messages.