dm6801

20171109 - 03 - classEx_06

Nov 11th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. package myApp;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class class03_ex06 {
  6.  
  7.     public static void main(String[] args) {
  8.         //init vars
  9.         int n = 0;
  10.        
  11.         //usage msg
  12.         System.out.println("Integer: ");
  13.        
  14.         //open in_Stream, parse & close
  15.         Scanner s = new Scanner(System.in);
  16.         n = s.nextInt();
  17.         s.close();
  18.        
  19.         //loop n times
  20.         for (int i=1; i<=n; i+=1) {
  21.             int sum = 0; //hold sum of digits
  22.            
  23.             //calculate sum of digits using recursion
  24.             for (int j=i; j>0; ) {
  25.                 sum += j%10; //add right digit to sum
  26.                 j /= 10; //remove right digit from the current iteretaion's number
  27.             }
  28.            
  29.             //check myself
  30.             //System.out.printf("sum %d: %d%n", i, sum);
  31.            
  32.             //output only numbers that match the pattern
  33.             if (i%3==0 && sum<5) {
  34.                 System.out.println(i);
  35.             }
  36.            
  37.         }
  38.     }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment