Results 1 to 8 of 8
Thread: right or wrong
- 09-24-2008, 03:36 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
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*/
- 09-24-2008, 03:56 PM #2
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.
- 09-24-2008, 04:59 PM #3
Yes,Norm is right,just also add public static void main(String[] args) to compile and run the application
- 09-24-2008, 05:16 PM #4
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
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
- 09-24-2008, 05:30 PM #5THE SUM AND PRODUCT OF DIGITS OF A NUMBER
What would be the answers for the number 5(a single digit number base 10)?
What would be the answer for 22?
- 09-25-2008, 05:38 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
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?
- 09-25-2008, 11:30 AM #7
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
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
- 09-25-2008, 12:45 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
Actually you no need to overload. And also for int number you never can't get double value. Simply get each digit and process.
Similar Threads
-
What's wrong with my codes?
By ayoood in forum New To JavaReplies: 16Last Post: 09-01-2008, 04:57 AM -
Can someone tell me what I did wrong??
By booter4429 in forum New To JavaReplies: 7Last Post: 08-13-2008, 09:35 PM -
Help - something has gone wrong with SWT/Eclipse
By int80 in forum SWT / JFaceReplies: 3Last Post: 08-05-2008, 12:47 PM -
wrong values
By mark-mlt in forum New To JavaReplies: 8Last Post: 04-25-2008, 12:11 PM -
I am Doing Something Wrong But Don't Know What?
By BHCluster in forum New To JavaReplies: 3Last Post: 04-16-2008, 02:16 PM
Bookmarks