A number is given as input. Find the odd digits in the number, add them and find if the sum is odd or not.if even return -1, if odd return 1 input:52315 logic:5+3+1 +5=14(even) output:-1 input1112 logic: 1+1+1=3(odd) output: 1
Answers
Answer:
guilty you you too oo uu uu uzgulflucuocucykckxkycoykyco6diti5jdk7fl6cu ll ui
Explanation:
ghnjjgi52u4
Answer:
A number is given as input. Find the odd digits in the number, add them and find if the sum is odd or not. If even return -1, if odd return 1
input:52315
logic:5+3+1+5=14(even)
output:-1
input:1112
logic:1+1+1=3(odd)
output:1
Explanation:
Given:
- First, calculate the reverse of the given number.
- To the reverse number we apply modulus operator and extract its last digit which is actually the first digit of a number so it is odd positioned digit.
- The next digit will be even positioned digit, and we can take the sum in alternating turns.
Find: Java program for the above scenario of the problem
Program:
// Java implementation of the approach
import java.util.*;
class SORE {
// Function to return the reverse of a number
static int reverse(int n)
{
int rev = 0;
while (n != 0) {
rev = (rev * 10) + (n % 10);
n /= 10;
}
return rev;
}
// Function to find the sum of the odd
// and even positioned digits in a number
static void getSum(int n)
{
n = reverse(n);
int sumOdd = 0, sumEven = 0, c = 1;
while (n != 0) {
// If c is even number then it means
// digit extracted is at even place
if (c % 2 == 0)
sumEven += n % 10;
else
sumOdd += n % 10;
n /= 10;
c++;
}
System.out.println("Sum odd = " + sumOdd);
System.out.println("Sum even = " + sumEven);
}
// Driver code
public static void main(String args[])
{
int n = 457892;
getSum(n);
}
}