Results 1 to 17 of 17
- 03-06-2011, 09:08 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
Coding to find the Harshad numbers...
edit: Ignore the last question, I got it mixed up with the next lab due which is finding the palindrome...Java Code:import java.util.Scanner; public class Harshad { /** * @param args */ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); // defines Scanner System.out.print("Please enter a positive integer (>=1): "); // asking for input int input = keyboard.nextInt(); // loop for wrong input while (input >= 1){ if (input <=0){ System.out.println("Sorry, the integer must be greater than or equal to 1."); System.out.print("Please enter a positive integer (>=1): "); } int sum=0; if (input > 0){ int p = input % 10; sum = sum + p; input = input / 10; int finalanswer = input % sum; // put in equation for Harshad number. if ( finalanswer == 0){ System.out.println(input + " is a Harshad number."); } else{ System.out.println(input + " is not a Harshad number."); } } // ends while2 } // ends else } // public static void main } // public class
anyways, could someone point what's wrong with this? the output isn't what I'm expecting, when I enter a number that's double digits and higher it comes out weird, like it shows if the first digit is a harshad # then 0 is a harshad # (since I defined the remainder as the input % sum I figure the code prints if the remainder is the harshad # (not true in this case, 0 doesn't count as one)? I've been fiddling around and completely lost, sorry I'm bumping this even though this is on first page since I have to turn this in 2 hours.Last edited by nope; 03-07-2011 at 03:02 AM.
- 03-07-2011, 03:02 AM #2
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
bump
apologies on first post :<
- 03-07-2011, 03:12 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What's the input and relevant output you comes with?
- 03-07-2011, 03:15 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
Please enter a positive integer (>=1): "57784"
5778 is not a Harshad number.
577 is not a Harshad number.
57 is not a Harshad number.
5 is not a Harshad number.
0 is a Harshad number.
this is what comes out, the number in quotes is what i input when prompted. I have no idea how this is printing out like this? made me lol so much.
- 03-07-2011, 03:32 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Since you've a while loop this could be happen. The point is that the logic inside the while loop is correct or not. Are you sure that you've convert the logic into the code?
- 03-07-2011, 03:37 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
to be honest i'm not sure, too many if-else statements and my brain is asploding atm lol, and it being due in an hour doesn't help at all either... i'm sure i messed up somewhere with the if statements, and i'm positive that while loop is written right but i can't be too sure.
- 03-07-2011, 03:43 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ok, just forget about the code for the moment.
How did you calculate the Harshad number. Take 1729 and say how you check it is Harshad or not?
- 03-07-2011, 03:47 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
been messing around and if I make the code print out the sum, then the output is one digit per line, I think that's what it makes the output put out multiple lines but I don't know what's causing this?Java Code:import java.util.Scanner; public class Harshad { /** * @param args */ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); // defines Scanner System.out.print("Please enter a positive integer (>=1): "); // asking for input int input = keyboard.nextInt(); // loop for wrong input if (input <=0){ System.out.println("Sorry, the integer must be greater than or equal to 1."); System.out.print("Please enter a positive integer (>=1): "); } else { while (input > 0){ int sum=0,quotient=0; quotient = input/10; input = input%10; sum = sum+input; input = quotient; if ( input % sum == 0){ System.out.println("The number " + input + " is a Harshad number."); } else{ System.out.println("The number " + input + " is not a Harshad number."); } } // ends while2 } // ends else } // public static void main } // public class
edit: adding all the digit in the input number and get the sum of that (if i input 12576, i'm supposed to get the sum of each digits, 1+2+5+7+6, then divide that sum by the input, in this case 12576, and if the remainder is 0 it's a hashard #, if not then no). the while loop there is supposed to calculate the sum of all the digits.Last edited by nope; 03-07-2011 at 03:49 AM. Reason: additional comment
- 03-07-2011, 03:52 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Where you calculate the summation of the given number? I cannot see that in your code.
- 03-07-2011, 03:59 AM #10
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
this was the calculation I Got from the lab teacher, I didn't figure it out but it was continuing to divide the input number then doing something with the reminders or something like that?Java Code:while (input > 0){ int sum=0,quotient=0; quotient = input/10; input = input%10; sum = sum+input; input = quotient;
- 03-07-2011, 04:06 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ok on that put a print statement and see the result is correct or not.
- 03-07-2011, 04:08 AM #12
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
import java.util.Scanner;
public class Harshad {
/**
* @param args
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in); // defines Scanner
System.out.print("Please enter a positive integer (>=1): "); // asking for input
int input = keyboard.nextInt();
int value=input;
int sum=0,quotient=0;
// loop for wrong input
if (input <=0){
System.out.println("Sorry, the integer must be greater than or equal to 1.");
System.out.print("Please enter a positive integer (>=1): ");
} else {
while (input > 0){
quotient = input/10;
input = input%10;
sum = sum+input;
input = quotient;
} // ends while2
}// ends else
if ( value % sum == 0){
System.out.println("The number " + value + " is a Harshad number.");
}
else{
System.out.println("The number " + value + " is not a Harshad number.");
}
} // public static void main
} // public class
- 03-07-2011, 04:08 AM #13
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
the result is correct, but the next line is supposed to determine if it is a harshad #, and it always prints "0" is a harshad number; btw in fact it isn't.
should be the "not a harshad number" result, but no matter what I do it always outputs 0?Java Code:if ( input % sum != 0){
- 03-07-2011, 04:10 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What happen when you enter the number I gave?
- 03-07-2011, 04:12 AM #15
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
uh, sorry, eranga what number did you give me? the printout of the sum always works, no matter what number i put in.
also juhiswt, isn't declaring input as value redundant? does using 'input' mess up the code or something?
yay it worked, thanks! when I left "input" on the println output code the input always returned '0' and was fixed when I replaced it with value after defining valueLast edited by nope; 03-07-2011 at 04:16 AM.
- 03-07-2011, 04:16 AM #16
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
Input is having the value of quotient so sum will devide the quotient not inputted number.So keep the vaue of input in another variable.
- 03-07-2011, 04:16 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
In while loop you've changed the value. SO it's not the same as user entered.
Similar Threads
-
Find the sum of two numbers that are from two different arrays.
By bMorgan in forum New To JavaReplies: 8Last Post: 02-08-2011, 07:27 AM -
Problem getting numbers from user and finding smallest two numbers
By radhi16 in forum New To JavaReplies: 11Last Post: 01-14-2011, 06:36 PM -
does gui coding in netbeans is just the same with coding without nebeans?
By maizuddin35 in forum NetBeansReplies: 4Last Post: 10-25-2010, 03:49 PM -
Help with my code to find MinOfAll numbers
By May.ver.rick in forum New To JavaReplies: 9Last Post: 04-20-2010, 04:46 AM -
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks