Results 1 to 17 of 17
  1. #1
    nope is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default Coding to find the Harshad numbers...

    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
    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.
    Last edited by nope; 03-07-2011 at 03:02 AM.

  2. #2
    nope is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    bump

    apologies on first post :<

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

  4. #4
    nope is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    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.

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

    Default

    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?

  6. #6
    nope is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    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.

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

    Default

    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?

  8. #8
    nope is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    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
    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.
    Last edited by nope; 03-07-2011 at 03:49 AM. Reason: additional comment

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

  10. #10
    nope is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

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

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

  12. #12
    juhiswt is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    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

  13. #13
    nope is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    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.
    Java Code:
    if ( input % sum != 0){
    should be the "not a harshad number" result, but no matter what I do it always outputs 0?

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

  15. #15
    nope is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    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
    Last edited by nope; 03-07-2011 at 04:16 AM.

  16. #16
    juhiswt is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    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.

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

Similar Threads

  1. Replies: 8
    Last Post: 02-08-2011, 07:27 AM
  2. Replies: 11
    Last Post: 01-14-2011, 06:36 PM
  3. Replies: 4
    Last Post: 10-25-2010, 03:49 PM
  4. Help with my code to find MinOfAll numbers
    By May.ver.rick in forum New To Java
    Replies: 9
    Last Post: 04-20-2010, 04:46 AM
  5. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 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
  •