programme logic eka kiyanna menna me examlpe ekata??

viraj_slk

Active member
  • Oct 1, 2007
    505
    35
    28
    One possible solution to think of

    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????


    Good to see that u have asked for the logic rather than a code.

    Here's one way I thought of to do it:
    first assign ur number to a variable as integer :
    x = 9087236781;

    then convert the same number to a string value. This is so that I can loop through each character.
    str = (string_cast) x;

    now str = '9087236781';
    so..for example: str[0] is equal to 9;

    now use a loop like this to keep adding the numbers together UNTIL the total is less than or equal to 9.

    Code:
    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;
    }

    finally.. when the loop gets finished, 'x' will have a single digit value that is less than or equal to 9, and is also the summation of numbers in '9087236781'

    Let me know if u need a bit more help to clarify this one.

     
    Last edited:

    nagaya

    Member
    Mar 18, 2007
    12,671
    194
    0
    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(y);
    }
    System.out.println(y);
    }
    }
     

    viraj_slk

    Active member
  • Oct 1, 2007
    505
    35
    28
    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(y);
    }
    System.out.println(y);
    }
    }


    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. :)
     
    • Like
    Reactions: koondeGoda

    nagaya

    Member
    Mar 18, 2007
    12,671
    194
    0

    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 ;)
     

    viraj_slk

    Active member
  • Oct 1, 2007
    505
    35
    28
    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 :D

    anyway, so i jst posted the logic. almost any program should be programmable using any language once u get the logic right.
     

    nagaya

    Member
    Mar 18, 2007
    12,671
    194
    0
    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 :D

    anyway, so i jst posted the logic. almost any program should be programmable using any language once u get the logic right.

    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,:lol::lol:;):rolleyes:
     

    amilabanuka

    Well-known member
  • Sep 30, 2006
    7,292
    881
    113
    Thama math hoyanooo....
    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..
     
    • Like
    Reactions: koondeGoda

    viraj_slk

    Active member
  • Oct 1, 2007
    505
    35
    28
    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,:lol::lol:;):rolleyes:

    oh yes, string == char[] in C. thanks for reminding :)
     

    amila.ela

    Well-known member
  • Mar 24, 2010
    8,645
    626
    113
    මිහිමත
    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..


    mata meka C# walin liyala dennakooo??
     

    amila.ela

    Well-known member
  • Mar 24, 2010
    8,645
    626
    113
    මිහිමත
    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..


    meka wadada ban???