Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package class170622;
- import java.util.Scanner;
- public class For06 {
- public static void main(String[] args) {
- /*
- * כתוב תוכנית הקולטת מספר n. עבור כל המספרים מ- 1 ועד n הצג רק את
- * המספרים שהם כפולה של 3 וסכום ספרותיהם קטן מ- 5. למשל עבור המספר 22
- * יוצגו המספרים: 3, 12, ו- 21
- */
- // define final variables
- final int MULIPLICATOR = 3;
- final int MAX_SUM = 5;
- // create a scanner to get input data
- Scanner s = new Scanner(System.in);
- // ask the user for a positive integer number
- System.out.println("Enter integer value: ");
- // get and save input data
- int num = s.nextInt(); // input : number given by the user
- // close scanner
- s.close();
- // for each multiplication of the number MULIPLICATOR
- for (int i = MULIPLICATOR; i <= num; i += MULIPLICATOR) {
- int tmp = i; // calculated: current number
- int sum = 0; // calculated: sum of digits for the current number
- // add each digit in the current number to the sum
- while (tmp > 0) {
- sum += tmp % 10;
- tmp /= 10;
- }
- // if sum is not greater than MAX_SUM
- if (sum <= MAX_SUM) {
- // print the number: i
- System.out.printf("%d ", i);
- }
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement