# Coding to find the Harshad numbers...

• 03-06-2011, 10:08 PM
nope
Coding to find the Harshad numbers...
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```
edit: Ignore the last question, I got it mixed up with the next lab due which is finding the palindrome...

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.
• 03-07-2011, 04:02 AM
nope
bump

apologies on first post :<
• 03-07-2011, 04:12 AM
Eranga
What's the input and relevant output you comes with?
• 03-07-2011, 04:15 AM
nope
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.

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, 04:32 AM
Eranga
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, 04:37 AM
nope
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, 04:43 AM
Eranga
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, 04:47 AM
nope
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```
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?

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.
• 03-07-2011, 04:52 AM
Eranga
Where you calculate the summation of the given number? I cannot see that in your code.
• 03-07-2011, 04:59 AM
nope
Code:

```            while (input > 0){                                         int sum=0,quotient=0;                                                         quotient = input/10;                         input = input%10;                         sum = sum+input;                         input = quotient;```
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?
• 03-07-2011, 05:06 AM
Eranga
Ok on that put a print statement and see the result is correct or not.
• 03-07-2011, 05:08 AM
juhiswt
import java.util.Scanner;

/**
* @param args
*/
public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in); // defines Scanner

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, 05:08 AM
nope
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.
Code:

`if ( input % sum != 0){`
should be the "not a harshad number" result, but no matter what I do it always outputs 0?
• 03-07-2011, 05:10 AM
Eranga
What happen when you enter the number I gave?
• 03-07-2011, 05:12 AM
nope
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 value
• 03-07-2011, 05:16 AM
juhiswt
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, 05:16 AM
Eranga
In while loop you've changed the value. SO it's not the same as user entered.