while length=1 wenakan substring kadila ewage values ekatu wenna lyanna
the input is: 9087236781
so the result must be in 1 digit like this
9+0+8+7+2+3+6+7+8+1=51>>>>5+1= 6
give me progaming logic about that????
while (x > 9)
{
str = (string_cast) x;
len = length(x); // the length of the number
total = 0;
for (i = 0; i < len; i++)
{
total += (integer_cast)str[i];
}
x = total;
}
import java.util.Scanner;
class logic{
public static void main(String[] args){
int x=0,y=0;
String num=null;
Scanner in = new Scanner(System.in);
num = in.nextLine();
while(num.length() > 1){
y=0;
for (x=0;x<num.length();x++)
{
y += Integer.parseInt(num.substring(x,x+1));
}
num = Integer.toString;
}
System.out.println;
}
}

Yep bro!I also noticed that,that's the only difference,I can't remember much C now to do this,that's why i used Java
Good solution bro. Although I can hardly remember any java now I can make out the logic in ur code. You have done the same thing as I have, but instead u have used the length of the final solution (which should be 1) as the controller for the loop, where as I used the condition that value must be less than or equal to 9 as the condition to continue the loop.
Good stuff.

Yep bro!I also noticed that,that's the only difference,I can't remember much C now to do this,that's why i used Java![]()
I don't think there is such thing as 'string_cast' in C, is there? think u can directly assign an integer to a string without casting in C. can't remember much of C either![]()
anyway, so i jst posted the logic. almost any program should be programmable using any language once u get the logic right.




import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class NumberProcessor {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input;
try {
input = reader.readLine();
} catch (IOException ex) {
input = "";
}
int number;
do {
number = processString(input);
input = String.valueOf(number);
} while (input.length() > 1);
System.out.println("summed result is : " + number);
}
private static int processString(String input) {
int value = 0;
for (int i = 0; i < input.length(); i++) {
value += Integer.parseInt(String.valueOf(input.charAt(i)));
}
return value;
}
}
nope there is nothing as string_cast on C,and there is no String on C either,it's just an array of char,yep you're absolutely correct,once we thought algorithm,the programmer can code it using any language,btw,it seems thread starter is missing,![]()

Code:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class NumberProcessor { public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String input; try { input = reader.readLine(); } catch (IOException ex) { input = ""; } int number; do { number = processString(input); input = String.valueOf(number); } while (input.length() > 1); System.out.println("summed result is : " + number); } private static int processString(String input) { int value = 0; for (int i = 0; i < input.length(); i++) { value += Integer.parseInt(String.valueOf(input.charAt(i))); } return value; } }
hope this help..
Code:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class NumberProcessor { public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String input; try { input = reader.readLine(); } catch (IOException ex) { input = ""; } int number; do { number = processString(input); input = String.valueOf(number); } while (input.length() > 1); System.out.println("summed result is : " + number); } private static int processString(String input) { int value = 0; for (int i = 0; i < input.length(); i++) { value += Integer.parseInt(String.valueOf(input.charAt(i))); } return value; } }
hope this help..
hey man. simple conversion ekakne thiyennne. sahena dewal samanai oya dekema.mata meka C# walin liyala dennakooo??
meka wadada ban???
hey man. simple conversion ekakne thiyennne. sahena dewal samanai oya dekema.
yep. I have tested it. I used recursion.