mmayoub

For-Exercise 03

Jun 27th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. package class170622;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class For06 {
  6.  
  7.     public static void main(String[] args) {
  8.         /*
  9.          * כתוב תוכנית הקולטת מספר n. עבור כל המספרים מ- 1 ועד n הצג רק את
  10.          * המספרים שהם כפולה של 3 וסכום ספרותיהם קטן מ- 5. למשל עבור המספר 22
  11.          * יוצגו המספרים: 3, 12, ו- 21
  12.          */
  13.  
  14.         // define final variables
  15.         final int MULIPLICATOR = 3;
  16.         final int MAX_SUM = 5;
  17.  
  18.         // create a scanner to get input data
  19.         Scanner s = new Scanner(System.in);
  20.  
  21.         // ask the user for a positive integer number
  22.         System.out.println("Enter integer value: ");
  23.         // get and save input data
  24.         int num = s.nextInt(); // input : number given by the user
  25.  
  26.         // close scanner
  27.         s.close();
  28.  
  29.         // for each multiplication of the number MULIPLICATOR
  30.         for (int i = MULIPLICATOR; i <= num; i += MULIPLICATOR) {
  31.  
  32.             int tmp = i; // calculated: current number
  33.             int sum = 0; // calculated: sum of digits for the current number
  34.  
  35.             // add each digit in the current number to the sum
  36.             while (tmp > 0) {
  37.                 sum += tmp % 10;
  38.                 tmp /= 10;
  39.             }
  40.  
  41.             // if sum is not greater than MAX_SUM
  42.             if (sum <= MAX_SUM) {
  43.                 // print the number: i
  44.                 System.out.printf("%d ", i);
  45.             }
  46.         }
  47.  
  48.         System.out.println();
  49.     }
  50.  
  51. }
Add Comment
Please, Sign In to add comment