Results 1 to 8 of 8

Thread: right or wrong

  1. #1
    jot321 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default right or wrong

    /*WRITE A PROGRAM TO ACCEPT A NUMBER AND FIND THE SUM AND PRODUCT OF DIGITS OF A NUMBER USING FUNCTION NAME DIGIT(int n ) WHICH RECEIVES THE VALUES FROM THE MAIN()*/

    class digitsp
    {
    void main(int a)
    {
    double a1=a;
    digitsp obj = new digitsp();
    int pro=obj.digit(a);
    System.out.println(pro);

    double sum=obj.digit(a1);
    System.out.println((int)sum);
    }


    int digit(int n)
    {
    int d,pro=1;
    do
    {
    d=n%10;
    n=n/10;
    pro=pro*d;
    }
    while(n!=0);
    return pro;

    }

    double digit(double n)
    {
    int d,sum=0;
    do
    {
    d=(int)n%10;
    n=(int)n/10;
    sum=sum+d;
    }
    while(n!=0);
    return sum;
    }
    }


    /*HAVE I DONE IT IN RIGHT MANNER*/

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default

    Part of the assignment woud be: Does it give you the correct results?
    Another part: Are descriptive variable names used? Score: D-
    ANother part: is the code documented so that anyone reading it can understand what the code is supposed to do.
    You'd get an F for that.

    Don't convert double input to int and then back to double. You'll lose precision.
    Classnames by convention start with uppercase letter
    Add "text" in println() to describe what output is.

  3. #3
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Yes,Norm is right,just also add public static void main(String[] args) to compile and run the application

  4. #4
    jot321 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    yes you are right that i must have used descriptive variable and proper printing statements

    results are coming accurate
    i want to know that is there any other of d doing this program(easier) or this is the easiest way

    function overloading is the only way yis there any other way

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default

    THE SUM AND PRODUCT OF DIGITS OF A NUMBER
    Can you describe what this means?
    What would be the answers for the number 5(a single digit number base 10)?
    What would be the answer for 22?

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First: When you post a question please use code tags. It's really helpful to others. If you don't know how to do it, read FAQ page.

    Second: Try to describe your question as much as possible. Seems to me other members have lots of doubts what you are trying to do.

    Actually I'm not clear what you are trying to do here too. Are you looking product/sum of a number? If so, the answer to the Norms' question,

    Number 22 - Sum = 4 and Product = 4
    Number 34 - Sum = 7 and Product = 12

    is it? But in that case what is the output for single digit number?

  7. #7
    jot321 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    in case of single digit the answer will be same as the number
    number-9
    sum-9
    product-9

    Number 22 - Sum = 4 and Product = 4
    Number 34 - Sum = 7 and Product = 12


    THIS IS CORRECT

    i want to know that is there any other way other than function overloading

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. What's wrong with my codes?
    By ayoood in forum New To Java
    Replies: 16
    Last Post: 09-01-2008, 04:57 AM
  2. Can someone tell me what I did wrong??
    By booter4429 in forum New To Java
    Replies: 7
    Last Post: 08-13-2008, 09:35 PM
  3. Help - something has gone wrong with SWT/Eclipse
    By int80 in forum SWT / JFace
    Replies: 3
    Last Post: 08-05-2008, 12:47 PM
  4. wrong values
    By mark-mlt in forum New To Java
    Replies: 8
    Last Post: 04-25-2008, 12:11 PM
  5. I am Doing Something Wrong But Don't Know What?
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 02:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •