1// Java program to check if a given credit
2// card is valid or not.
3import java.util.Scanner;
4
5public class CreditCard {
6 // Main Method
7 public static void main(String[] args)
8 {
9 long number = 5196081888500645L;
10
11 System.out.println(number + " is " +
12 (isValid(number) ? "valid" : "invalid"));
13 }
14
15 // Return true if the card number is valid
16 public static boolean isValid(long number)
17 {
18 return (getSize(number) >= 13 &&
19 getSize(number) <= 16) &&
20 (prefixMatched(number, 4) ||
21 prefixMatched(number, 5) ||
22 prefixMatched(number, 37) ||
23 prefixMatched(number, 6)) &&
24 ((sumOfDoubleEvenPlace(number) +
25 sumOfOddPlace(number)) % 10 == 0);
26 }
27
28 // Get the result from Step 2
29 public static int sumOfDoubleEvenPlace(long number)
30 {
31 int sum = 0;
32 String num = number + "";
33 for (int i = getSize(number) - 2; i >= 0; i -= 2)
34 sum += getDigit(Integer.parseInt(num.charAt(i) + "") * 2);
35
36 return sum;
37 }
38
39 // Return this number if it is a single digit, otherwise,
40 // return the sum of the two digits
41 public static int getDigit(int number)
42 {
43 if (number < 9)
44 return number;
45 return number / 10 + number % 10;
46 }
47
48 // Return sum of odd-place digits in number
49 public static int sumOfOddPlace(long number)
50 {
51 int sum = 0;
52 String num = number + "";
53 for (int i = getSize(number) - 1; i >= 0; i -= 2)
54 sum += Integer.parseInt(num.charAt(i) + "");
55 return sum;
56 }
57
58 // Return true if the digit d is a prefix for number
59 public static boolean prefixMatched(long number, int d)
60 {
61 return getPrefix(number, getSize(d)) == d;
62 }
63
64 // Return the number of digits in d
65 public static int getSize(long d)
66 {
67 String num = d + "";
68 return num.length();
69 }
70
71 // Return the first k number of digits from
72 // number. If the number of digits in number
73 // is less than k, return number.
74 public static long getPrefix(long number, int k)
75 {
76 if (getSize(number) > k) {
77 String num = number + "";
78 return Long.parseLong(num.substring(0, k));
79 }
80 return number;
81 }
82}
83
1import java.util.*;
2public class fungsiA {
3
4 public static void main(String args []) throws InterruptedException {
5 Scanner sc = new Scanner(System.in);
6 int n;
7 System.out.print("Masukkan N :");
8 n = sc.nextInt();
9 long starTime = System.nanoTime();
10 for (int i = 1; i <= n; i = i + 2) {
11 for (int j = 1; j <= n/2; j++) {
12 System.out.println(2);
13 }
14 }
15 long endTime = System.nanoTime();
16 long totalTime = (endTime - starTime) / 1000000;
17 System.out.println("Total Time : " + totalTime + " milliseconds or " + (totalTime / 1000) + " seconds");
18 }
19}
1public static void diceSum(){
2Random rand=new Random();
3Scanner console=new Scanner (System.in);
4System.out.print("Desired dice sum: ");
5int n=console.nextInt();
6int x, y,sum;
7do {
8x = rand.nextInt(6) +1;
9y = rand.nextInt(6) +1;
10sum = x + y;
11System.out.println(x + " and " + y + " = " + sum);
12} while (sum != n);
13
14}
1public class testint
2 {
3 public static void main(String[] args)
4 {
5 int a=1;
6 int i=2;
7 int m;
8 for(int k=0;k<=8;k++)
9 {
10 m=a+i;
11 System.out.println9"The value is : ", m);
12 a=i;
13 i=m;
14 }
15 }
16 }
1for (var i = 1; i < 2; i++) {
2for (var j = 1; j < 3; j++) {
3echo j;
4}
5var i = 0;
6}
1import java.util.*;
2public class fungsiA {
3
4 public static void main(String args []) throws InterruptedException {
5 Scanner sc = new Scanner(System.in);
6 int n;
7 System.out.print("Masukkan N :");
8 n = sc.nextInt();
9 long starTime = System.nanoTime();
10 for (int i = 1; i <= n; i = i + 2) {
11 for (int j = 1; j <= n/2; j++) {
12 System.out.println(2);
13 }
14 }
15 long endTime = System.nanoTime();
16 long totalTime = (endTime - starTime) / 1000000;
17 System.out.println("Total Time : " + totalTime + " milliseconds or " + (totalTime / 1000) + " seconds");
18 }
19}