Skip to content
Related Articles

Related Articles

Check if the first and last digit of number N is prime and their sum is less than K
  • Last Updated : 30 Oct, 2019

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check
// if the first and last digit of number N
// is prime and their sum is less than K
#include <bits/stdc++.h>
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 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

Yes
No



My Personal Notes arrow_drop_up
Recommended Articles
Page :