top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Write a function to print the sum of the even and odd digit with given condition?

+3 votes
81 views

Rohit's teacher has asked him to write a function that takes as input parameters the first parameter will be an integer number representing. The number whose digitSum needs to be found the second parameter will be a char representing the
option which would either be 'e' or 'o' representing 'even' or 'odd' respectively.

Function signature public int digitSum(int n, char ch);

digitSum(9625, 'o')
Example 1: if given number is 9625 and the option is 'o' we must add only the odd digit i.e. 9+5=14
digitSum(2134, 'e')
Example 2: if given number is 2134 and the option  is 'e' we must add only the odd digit i.e. 2+4=6
posted Apr 22, 2017 by Azhar Keer

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

+1 vote
 
Best answer

Even OR Odd Digits' Sum: try this code this is very similar, just input parameter is in string

 public EvenOddDigitsSum(int input1, String input2){
    int rem,sum=0;
       while(input1>0){
           rem=input1%10;
           input1/=10;
           if(input2.equals("odd")){
           if(rem%2!=0)   sum+=rem;   
           }
           else if(input2.equals("even")){  
           if(rem%2==0)   sum+=rem;   
           }
      }
    return sum;
}
answer Dec 9, 2017 by Arindam Nath
Similar Questions
+3 votes

Write a program to print the sum of the element of the array with the given below condition?

If the array has 6 and 7 in succeeding orders, ignore 6 and 7 and the numbers between them for the calculation of sum.

Array Elements - 10, 3, 6, 1, 2, 7, 9

Output: 22
i.e 10+3+9

+3 votes

Rohit wants to add the last digits of two given numbers.
For example, If the given numbers are 267 and 154, the output should be 11.

Below is the explanation -
Last digit of the 267 is 7
Last digit of the 154 is 4
Sum of 7 and 4 = 11

Write a program to help Rohit achieve this for any given two numbers.

Note: The sign of the input numbers should be ignored. i.e.
if the input numbers are 267 and 154, the sum of last two digits should be 11
if the input numbers are 267 and -154, the sum of last two digits should be 11
if the input numbers are -267 and 154, the sum of last two digits should be 11
if the input numbers are -267 and -154, the sum of last two digits should be 11

0 votes

C program to find and print second largest digit in the given number? (without using arrays, functions and using only one loop).

+1 vote

Given a singly linked list of integers, write a function in java that returns true if the given list is palindrome, else returns false

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...