Question

In: Computer Science

Write a function called evenUp that returns the result of increasing the first even digit in...

Write a function called evenUp that returns the result of increasing the first even digit in a positive integer parameter by 1. (Your solution should use no more than 10 lines of code. Your function can return any convenient value of your choice if the parameter is not positive.) For example, a program that uses the function evenUp follows.

public class P5 {

public static void main(String args[]) {

System.out.println(evenUp(1232)); // prints 1332 only the first even 2 changes

System.out.println(evenUp(1332)); // prints 1333

System.out.println(evenUp(1333)); // prints 1333 no even digit to change

System.out.println(evenUp(22)); // prints 32 System.out.println(evenUp(2)); // prints 3

Solutions

Expert Solution

CODE:

public class P5
{   
//Definition for evenUp()
int evenUp(int a){
//converting int to string
String s = ""+a;
  
// iterating digits
for(int i=0;i<s.length();i++){
  
//if even digit found
if((s.charAt(i)-'0')%2==0){
  
// converting character into int and incrementing by 1
int n= s.charAt(i)-'0'+1;
  
//converting incremented digit into string
String ch = ""+n;
  
//updating string with updated digit
s = s.substring(0, i) + ch + s.substring(i + 1);
  
//terminating the loop
break;
}
}
  
//converting string to int
a = Integer.parseInt(s);
  
//returns result
return a;
}
   public static void main(String[] args) {
  
   //creating object of P5 Class
   P5 ob = new P5();
  
   //Calling evenUp() and printing results
   System.out.println(ob.evenUp(1232)); // prints 1332 only the first even 2 changes
System.out.println(ob.evenUp(1332)); // prints 1333
System.out.println(ob.evenUp(1333)); // prints 1333 no even digit to change
System.out.println(ob.evenUp(22)); //prints 32
   }
}
OUTPUT:


Related Solutions

a. Write a c++ function called IsPalindrome that receives a 3-digit number and returns whether it...
a. Write a c++ function called IsPalindrome that receives a 3-digit number and returns whether it is palindrome or not. [2.5 marks] b. Write a c++ function, called GCD that receives two parameters n, d, and prints their greatest common divisor [2.5 marks] Now after you created the two functions, you have to write a main function that can call the above functions according to user choice. Write a menu that allows the user to select from the following options...
JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
Write an evenTest(n) function that returns True if it is given an even integer and False...
Write an evenTest(n) function that returns True if it is given an even integer and False if it is given an odd number. The rest of your code (outside of the function) should get an integer number from the user, call evenTest(n) to test if the number is even, and then tell the user if the number was even or odd. Name the program even_or_odd.py. Part 2. Salary.py (5 pts) You are offered two options to receive salary: • Option...
write an algorithm function called balance() in javascript that takes in a string and returns a...
write an algorithm function called balance() in javascript that takes in a string and returns a strinf with balanced parantheses only. the string should be able to contain only parantheses, numbers, and letters. include comments of each portion of your code balance(“()()”) should return “()()” balance(“())”) should return “()” balance(“a(b)c”) should return “a(b)c” balance(“a(b)c())”) should return “a(b)c()”
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns...
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns the highest similarity score found in the genome as a double. Note: the term genome refers to the string that represents the complete set of genes in an organism, and sequence to refer to some substring or sub-sequence in the genome. Your function MUST be named findBestSimScore Your function should take two parameters in this order: a string parameter for the genome (complete set...
** * Write a recursive function that removes the first k even numbers * from the...
** * Write a recursive function that removes the first k even numbers * from the stack. If there are less than k even elements in the stack, * just remove all even elements. Do not use any loops or data structures * other than the stack passed in as a parameter. * @param stack * @param k * @return Returns the number of elements removed from the stack. */ public static int removeEvenNumbers(Stack<Integer> stack, int k) { return 0;...
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT