Check if the first and last digit of number N is prime and their sum is less than K

Last Updated : 13 Mar, 2023

Given a number N, the task is to check if its first and last digit is a prime number and their sum is less than K, or not. If it is, then print Yes, else print No.
Examples:

Input: N = 322223, K = 10
Output: Yes

Input: N = 62531561, K = 15
Output: No

Approach:

• Check if the first and last digit of N is prime or not.
• If they are prime, then check if they are less than K or not
• If they are less than K as well, print Yes.
• In any other case, print No.

Below is the implementation of the above approach:

C++

 `// C++ program to check ` `// if the first and last digit of number N ` `// is prime and their sum is less than K ` `#include ` `using` `namespace` `std;  ` ` `  `// Get the First digit of the number ` `int` `first(``int` `n) ` `{ ` `    ``int` `a = n; ` `    ``int` `c = 1; ` `    ``while` `(a != 0) ` `    ``{ ` `        ``a /= 10; ` `        ``c = c * 10; ` `    ``} ` `    ``c = c / 10; ` `    ``int` `fi = n / c; ` `    ``return` `fi; ` `} ` ` `  `// Check if the digit is prime or not ` `bool` `prime(``int` `n) ` `{ ` `    ``switch` `(n)  ` `    ``{ ` `        ``case` `2: ` `            ``return` `true``; ` `     `  `        ``case` `3: ` `            ``return` `true``; ` `     `  `        ``case` `5: ` `            ``return` `true``; ` `     `  `        ``case` `7: ` `            ``return` `true``; ` `     `  `        ``default``: ` `            ``return` `false``; ` `    ``} ` `} ` ` `  `// Function to Check if the first ` `// and last digit of number N is prime ` `// and their sum is less than K ` `void` `check(``int` `n, ``int` `k) ` `{ ` ` `  `    ``// Last digit of the number ` `    ``int` `l = n % 10; ` ` `  `    ``// First digit of number ` `    ``int` `f = first(n); ` ` `  `    ``// Check if these are prime ` `    ``bool` `lp = prime(l); ` `    ``bool` `fp = prime(f); ` ` `  `    ``// If they are prime ` `    ``if` `(lp && fp) ` `    ``{ ` ` `  `        ``// Check if they are less than k or not ` `        ``if` `(l + f < k) ` `            ``cout << ``"Yes\n"``; ` `        ``else` `            ``cout << ``"No\n"``; ` `    ``} ` `    ``else`  `    ``{ ` `        ``cout << ``"No\n"``; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main()  ` `{  ` `    ``// Test case 1 ` `    ``int` `n = 322223; ` `    ``int` `k = 10; ` `     `  `    ``check(n, k); ` `     `  `    ``// Test case 2 ` `    ``n = 62531561; ` `    ``k = 15; ` `     `  `    ``check(n, k); ` `     `  `    ``return` `0;  ` `}  ` ` `  `// This code is contributed by PrinciRaj1992  `

Java

 `// Java program to check ` `// if the first and last digit of number N ` `// is prime and their sum is less than K ` ` `  `import` `java.lang.*; ` ` `  `public` `class` `crazy_number { ` ` `  `    ``// Get the First digit of the number ` `    ``public` `static` `int` `first(``int` `n) ` `    ``{ ` `        ``int` `a = n; ` `        ``int` `c = ``1``; ` `        ``while` `(a != ``0``) { ` `            ``a /= ``10``; ` `            ``c = c * ``10``; ` `        ``} ` `        ``c = c / ``10``; ` `        ``int` `fi = n / c; ` `        ``return` `fi; ` `    ``} ` ` `  `    ``// Check if the digit is prime or not ` `    ``public` `static` `boolean` `prime(``int` `n) ` `    ``{ ` ` `  `        ``switch` `(n) { ` `        ``case` `2``: ` `            ``return` `true``; ` ` `  `        ``case` `3``: ` `            ``return` `true``; ` ` `  `        ``case` `5``: ` `            ``return` `true``; ` ` `  `        ``case` `7``: ` `            ``return` `true``; ` ` `  `        ``default``: ` `            ``return` `false``; ` `        ``} ` `    ``} ` ` `  `    ``// Function to Check if the first ` `    ``// and last digit of number N is prime ` `    ``// and their sum is less than K ` `    ``public` `static` `void` `check(``int` `n, ``int` `k) ` `    ``{ ` ` `  `        ``// Last digit of the number ` `        ``int` `l = n % ``10``; ` ` `  `        ``// First digit of number ` `        ``int` `f = first(n); ` ` `  `        ``// Check if these are prime ` `        ``boolean` `lp = prime(l); ` `        ``boolean` `fp = prime(f); ` ` `  `        ``// If they are prime ` `        ``if` `(lp && fp) { ` ` `  `            ``// Check if they are less than k or not ` `            ``if` `(l + f < k) ` `                ``System.out.println(``"Yes"``); ` `            ``else` `                ``System.out.println(``"No"``); ` `        ``} ` `        ``else` `{ ` `            ``System.out.println(``"No"``); ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` ` `  `        ``// Test case 1 ` `        ``int` `n = ``322223``; ` `        ``int` `k = ``10``; ` ` `  `        ``check(n, k); ` ` `  `        ``// Test case 2 ` `        ``n = ``62531561``; ` `        ``k = ``15``; ` ` `  `        ``check(n, k); ` `    ``} ` `} `

Python3

 `# Python3 program to check if  ` `# the first and last digit of number N ` `# is prime and their sum is less than K ` ` `  `# Get the First digit of the number ` `def` `first(n): ` `    ``a ``=` `n ` `    ``c ``=` `1` `    ``while` `(a !``=` `0``): ` `        ``a ``/``/``=` `10` `        ``c ``=` `c ``*` `10` ` `  `    ``c ``=` `c ``/``/` `10` `    ``fi ``=` `n ``/``/` `c ` `    ``return` `fi ` ` `  `# Check if the digit is prime or not ` `def` `prime(n): ` `    ``if` `n ``in` `[``2``, ``3``, ``5``, ``7``]: ` `        ``return` `True` `    ``else``: ` `        ``return` `False` ` `  `# Function to Check if the first ` `# and last digit of number N is prime ` `# and their sum is less than K ` `def` `check(n, k): ` ` `  `    ``# Last digit of the number ` `    ``l ``=` `n ``%` `10` ` `  `    ``# First digit of number ` `    ``f ``=` `first(n) ` ` `  `    ``# Check if these are prime ` `    ``lp ``=` `prime(l) ` `    ``fp ``=` `prime(f) ` ` `  `    ``# If they are prime ` `    ``if` `(lp ``and` `fp): ` ` `  `        ``# Check if they are less than k or not ` `        ``if` `(l ``+` `f < k): ` `            ``print``(``"Yes"``) ` `        ``else``: ` `            ``print``(``"No"``) ` `    ``else``: ` `        ``print``(``"No"``) ` ` `  `# Driver code ` ` `  `# Test case 1 ` `n ``=` `322223` `k ``=` `10` ` `  `check(n, k) ` ` `  `# Test case 2 ` `n ``=` `62531561` `k ``=` `15` ` `  `check(n, k) ` ` `  `# This code is contributed by Mohit kumar `

C#

 `// C# program to check ` `// if the first and last digit of number N ` `// is prime and their sum is less than K ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `    ``// Get the First digit of the number ` `    ``public` `static` `int` `first(``int` `n) ` `    ``{ ` `        ``int` `a = n; ` `        ``int` `c = 1; ` `        ``while` `(a != 0)  ` `        ``{ ` `            ``a /= 10; ` `            ``c = c * 10; ` `        ``} ` `        ``c = c / 10; ` `        ``int` `fi = n / c; ` `        ``return` `fi; ` `    ``} ` ` `  `    ``// Check if the digit is prime or not ` `    ``public` `static` `Boolean prime(``int` `n) ` `    ``{ ` `        ``switch` `(n)  ` `        ``{ ` `            ``case` `2: ` `                ``return` `true``; ` `     `  `            ``case` `3: ` `                ``return` `true``; ` `     `  `            ``case` `5: ` `                ``return` `true``; ` `     `  `            ``case` `7: ` `                ``return` `true``; ` `     `  `            ``default``: ` `                ``return` `false``; ` `        ``} ` `    ``} ` ` `  `    ``// Function to Check if the first ` `    ``// and last digit of number N is prime ` `    ``// and their sum is less than K ` `    ``public` `static` `void` `check(``int` `n, ``int` `k) ` `    ``{ ` ` `  `        ``// Last digit of the number ` `        ``int` `l = n % 10; ` ` `  `        ``// First digit of number ` `        ``int` `f = first(n); ` ` `  `        ``// Check if these are prime ` `        ``Boolean lp = prime(l); ` `        ``Boolean fp = prime(f); ` ` `  `        ``// If they are prime ` `        ``if` `(lp && fp) ` `        ``{ ` ` `  `            ``// Check if they are less than k or not ` `            ``if` `(l + f < k) ` `                ``Console.WriteLine(``"Yes"``); ` `            ``else` `                ``Console.WriteLine(``"No"``); ` `        ``} ` `         `  `        ``else`  `        ``{ ` `            ``Console.WriteLine(``"No"``); ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String []args) ` `    ``{ ` ` `  `        ``// Test case 1 ` `        ``int` `n = 322223; ` `        ``int` `k = 10; ` ` `  `        ``check(n, k); ` ` `  `        ``// Test case 2 ` `        ``n = 62531561; ` `        ``k = 15; ` ` `  `        ``check(n, k); ` `    ``} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Javascript

 ``

Output:

```Yes
No```

Time Complexity: O(log N)
Auxiliary Space: O(1), As constant extra space is used

Previous
Next