Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
C++ Program to Print Prime Numbers from 1 to n
Next article icon

C++ Program to Print the Largest Possible Prime Number From a Given Number

Last Updated : 07 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an integer, the task is to find the largest prime number that can be made out of that. If we consider the integer as a string of digits, then the prime number can be a substring of any length. The examples given below will clarify the idea of the problem.

Example:

Input: 12691
Output: 691

Approach:

  • Create a string of the given number
  • Compute all the substrings of the string
  • Check whether any substring is prime or not
  • If a substring is prime, maximize its value by comparing it with the other prime substrings
  • Return the maximum value of the prime substring

C++




// C++ program to Print the Largest
// Possible Prime Number From a
// Given Number
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
 
// function to check whether the
// substring is prime or not
bool isprime(string f)
{
    int n = stoi(f);
   
    // corner case check
    if (n <= 1)
        return false;
 
    // Check from 2 to n-1
    for (int i = 2; i < n; i++) {
        if (n % i == 0)
            return false;
    }
    return true;
}
int longp(int a)
{
    // convert the number to a string
    string d = to_string(a);
    int n = d.length();
    int c = INT_MIN;
    for (int i = 0; i < n; i++) {
        for (int len = 1; len <= n - i; len++) {
           
            // calculate the substrings of the string
            string p = d.substr(i, len);
           
            // pass the substring to the prime check
            // function
            if (isprime(p)) {
                int l = stoi(p);
               
                // store the maximum value of the prime
                // substring
                c = max(c, l);
            }
        }
    }
    return c;
}
// Driver Code
int main()
{
 
    long long int n = 12691;
    int k = longp(n);
    cout << k;
    return 0;
}
 
 
Output
691

Time Complexity: O(N3)
Auxiliary Space: O(1)



Next Article
C++ Program to Print Prime Numbers from 1 to n
author
pushkar_s
Improve
Article Tags :
  • C++
  • C++ Programs
  • C Basic Programs
Practice Tags :
  • CPP

Similar Reads

  • C++ Program to Print Prime Numbers from 1 to n
    A prime number is a natural number that has only two divisors, which are 1 and itself. In this article, we will learn how to print all the prime numbers from 1 to n in C++. Examples Input: n = 10Output: 2, 3, 5, 7Explanation: As 2, 3, 5, 7 are the only prime numbers between 1 to 10. Input: n = 5Outp
    5 min read
  • C++ Program To Check If a Prime Number Can Be Expressed as Sum of Two Prime Numbers
    Given a prime number [Tex]N  [/Tex]. The task is to check if it is possible to express [Tex]N  [/Tex]as sum of two separate prime numbers.Note: The range of N is less than 108. Examples:  Input: N = 13 Output: Yes Explanation: The number 13 can be written as 11 + 2, here 11 and 2 are both prime. Inp
    2 min read
  • C++ Program To Find Prime Numbers Between Given Interval
    A prime number is defined as a natural number greater than 1 and is divisible by only 1 and itself. In other words, the prime number is a positive integer greater than 1 that has exactly two factors, 1 and the number itself. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 . . . Note:
    7 min read
  • Find all possible ways to Split the given string into Primes
    Given string str that represents a number. The task is to find all possible ways to split the given string such that each segment is a prime number in the range of 1 to 106.Examples: Input: str = "3175" Output: [317, 5] [31, 7, 5] [3, 17, 5]Explanation: There can be 8 possible ways to split: [3175]
    10 min read
  • C Program To Find Prime Numbers Between Given Range
    A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, and 11 are prime numbers. In this article, we will learn how to find all the prime numbers between the given range. Example Input: l = 10, r = 30Output: 11 13 17 19Explan
    6 min read
  • C++ Program To Check Whether a Number is Prime or not
    Given a positive integer N. The task is to write a C++ program to check if the number is prime or not.  Definition:  A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are {2, 3, 5, 7, 11, ....} The idea to solve this
    3 min read
  • Segregate Prime and Non-Prime Numbers in an array
    Given an array arr[] of size N, the task is to rearrange the array elements such that all the Prime numbers are placed before the Non-prime numbers. Examples: Input: arr[] = {1, 8, 2, 3, 4, 5, 7, 20}Output: 7 5 2 3 4 8 1 20Explanation:The output consists of all the prime numbers 7 5 2 3, followed by
    15+ min read
  • TCS Coding Practice Question | Prime Numbers upto N
    Given a number N, the task is to find the Prime Numbers from 1 to N using Command Line Arguments. Examples: Input: N = 7Output: 2, 3, 5, 7 Input: N = 13Output: 2, 3, 5, 7, 11, 13 Approach: Since the number is entered as Command line Argument, there is no need for a dedicated input lineExtract the in
    4 min read
  • Find the largest composite number that divides N but is strictly lesser than N
    Given a composite number N, the task is to find the largest composite number that divides N and is strictly lesser than N. If there is no such number exist print -1.Examples: Input: N = 16 Output: 8 Explanation: All numbers that divide 16 are { 1, 2, 4, 8, 16 } out of which 8 is largest composite nu
    6 min read
  • C++ Program to Check Prime Number
    A Prime Number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples: Input: n = 29Output: 29 is primeExplanation: 29 has no divisors other than 1 and 29 itself. Hence, it is a prime number. Input: n = 15Output: 15 is NOT primeExplanation: 15 has divisors
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences