Incremental number skipping

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Van Dugall
    New Member
    • Mar 2009
    • 6

    Incremental number skipping

    Hi,

    I want to write a program to print all numbers from 1 to 100 except those that end in 7 or are a multiple of 7. So the program should skip printing the numbers 7, 14, 17, 21, 27, etc. This might be easy but I want to know an efficient way to do this any ideas??

    Thanks,
    Van Dugall
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Have you written any code for this? Any errors you are getting?

    Regards
    Dheeraj Joshi

    Comment

    • Van Dugall
      New Member
      • Mar 2009
      • 6

      #3
      Not yet... I am thinking about how to do this. I was thinking about doing a while from 1 to 100 and using if statements (mod 7 == 0) to handle the multples of 7 but how should I handle the numbers that end with 7? Also, what is the best way to skip a number when you are incrementing??

      Regards,,
      V. Dugall

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Hint: numbers that end in 0
        n mod 10 = 0

        Are you using a for loop?

        If so, you could have the print statement at the end. If the number matches a condition, use continue, so that the number skips over the print statement.

        Comment

        • Van Dugall
          New Member
          • Mar 2009
          • 6

          #5
          this is what I have so far.. but I'm having trouble excluding numbers that end in seven...:(

          Code:
          public class Seven {
          	public static void main(String[] args){
          		int num;
          		for(num = 1; num < 101; num++){
          			
          			if(num != 0 && num%7 == 0){
          				continue;
          			}
          			System.out.println(""+num);
          		}
          	  }
          }
          Regards,
          Van Dugall

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            num != 0
            This should always be true and is redundant. Take it out.

            Hint2: for numbers that end in 1
            num mod 10 = 1

            Comment

            Working...