+ 3
How to add the odd placed digits of an integer??
If an integer is1234 then find out the 2+4= 6 and if integer is 12345 then also it is 2+4 =6??
2 odpowiedzi
+ 19
● first check if number of digits in 'n' is odd or even, if odd then take n directly, else use (n/10)
● use loop or recursion till n becomes 0, it will look like : (n%10) + method(n/100)
//in java
+ 3
You need to use Strings and character methods and a loop to go over the String
example:
int i = 0;
String s = "12345"; // or use a scanner for user input
for(int k = 1; k < s.length()-1 ; k + 2){
char c = s.charAt(k);
//get the character from the string at a specified position k
int j = Character.getNumericValue(c);
//converts the character to a usable integer
i = i + j; //adding the integers
}
System.out.print(i);