7 Replies - 857 Views - Last Post: 10 March 2020 - 03:12 PM Rate Topic: -----

#1 supNub   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 25-December 19

HOW TO FINDING INDEX OF A NUMBER

Posted 10 March 2020 - 10:05 AM

I have project which i need to enter 5 digit number then be able to zero in on all/any of the numbers and check what they are divisible by example below , but i cant zero in on what syntax is should be using

EXAMPLE:
the leftmost digit is divisible by 1 and
the leftmost two digits are divisible by 2 and
the leftmost 3 digits are divisible by 3 and
the leftmost 4 digits are divisible by 4 and
leftmost 5 digits (the five-digit number itself) is divisible by 5.







import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
		int	numbSize;
		int numb;
		int	numbLarge=-1;	
				
		
		
		Scanner myScan = new Scanner(System.in);
		 
		System.out.print("Enter  5 digit number: ");
		numb = myScan.nextInt();
		numbSize = String.valueOf(numb).length();
		if (numbSize != 5) { 
			System.out.println("This is not a 5 digit number");

}

	}
}
:code:

This post has been edited by modi123_1: 10 March 2020 - 10:50 AM
Reason for edit:: In the future please use the [code] tags in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: HOW TO FINDING INDEX OF A NUMBER

#2 NeoTifa   User is online

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4655
  • View blog
  • Posts: 19,487
  • Joined: 24-September 08

Re: HOW TO FINDING INDEX OF A NUMBER

Posted 10 March 2020 - 10:15 AM

:code:

Try using %. That should give you pretty much all you need.
Was This Post Helpful? 1
  • +
  • -

#3 supNub   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 25-December 19

Re: HOW TO FINDING INDEX OF A NUMBER

Posted 10 March 2020 - 10:46 AM

sorry but i am really new to this so im going to need a bit more info can you provide an example of (%) is setup or any more info
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15568
  • View blog
  • Posts: 62,341
  • Joined: 12-June 08

Re: HOW TO FINDING INDEX OF A NUMBER

Posted 10 March 2020 - 10:53 AM

% is typically the 'modulus operator'. You should check into that.

ex
https://nullbeans.co...mples-and-uses/
Was This Post Helpful? 1
  • +
  • -

#5 NeoTifa   User is online

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4655
  • View blog
  • Posts: 19,487
  • Joined: 24-September 08

Re: HOW TO FINDING INDEX OF A NUMBER

Posted 10 March 2020 - 11:12 AM

My apologies, I assumed you knew the math operators already, given you knew about Scanners, as those are usually day 1 lessons. X % Y gives the remainder when divided. If the % returns 0, it's perfectly divisible. Ex: 4%2=0, 5%2=1.
Was This Post Helpful? 3
  • +
  • -

#6 supNub   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 25-December 19

Re: HOW TO FINDING INDEX OF A NUMBER

Posted 10 March 2020 - 12:06 PM

yes scanner is fun i learned it really quick but i remember covering this but ill have to review it again my next problem is once i place the if statement with (%)operater how do i target the specific number i want

example 12345
how would i check if lets say the 3rd number is divisable
Was This Post Helpful? 0
  • +
  • -

#7 NeoTifa   User is online

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4655
  • View blog
  • Posts: 19,487
  • Joined: 24-September 08

Re: HOW TO FINDING INDEX OF A NUMBER

Posted 10 March 2020 - 02:36 PM

Well, your number is being brought in as a String, which is basically an array of character objects. There is a method called Integer.toInt(String s) that returns the string as a number if you want to go the substring or base 10 division route. Quite a number of ways you could do it. Either way, I'd imagine a for loop would suit your needs. I'm not gonna give you the full logic, as it would defeat the purpose of the exercise, but I think I gave more than enough hints. :)Good luck!

Speaking of base 10 division, remember that dividing an integer by another integer will return an integer, which will drop any remainder if I recall correctly, which could be handy if you don't want to loop. :)

This post has been edited by NeoTifa: 10 March 2020 - 02:39 PM

Was This Post Helpful? 1
  • +
  • -

#8 supNub   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 25-December 19

Re: HOW TO FINDING INDEX OF A NUMBER

Posted 10 March 2020 - 03:12 PM

GOT IT THANKS
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1