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 Check Whether a Number is Prime or not
Next article icon

C++ Program To Check If a Prime Number Can Be Expressed as Sum of Two Prime Numbers

Last Updated : 17 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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. Input: N = 11 Output: No

Simple Solution: A simple solution is to create a sieve to store all the prime numbers less than the number N. Then run a loop from 1 to N and check whether [Tex]i  [/Tex]and [Tex]n-i  [/Tex]are both prime or not. If yes then print Yes, else No.

Efficient solution: Apart from 2, all of the prime numbers are odd. So it is not possible to represent a prime number (which is odd) to be written as a sum of two odd prime numbers, so we are sure that one of the two prime number should be 2. So we have to check whether n-2 is prime or not. If it holds we print Yes else No.
For example, if the number is 19 then we have to check whether 19-2 = 17 is a prime number or not. If 17 is a prime number then print yes otherwise print no.

Below is the implementation of the above approach: 

C++

// C++ program to check if a prime number
// can be expressed as sum of
// two Prime Numbers
#include <bits/stdc++.h>
using namespace std;
  
// Function to check whether 
// a number is prime or not
bool isPrime(int n)
{
    if (n <= 1)
        return false;
  
    for (int i = 2; i <= sqrt(n); i++) 
    {
        if (n % i == 0)
            return false;
    }
  
    return true;
}
  
// Function to check if a prime number
// can be expressed as sum of
// two Prime Numbers
bool isPossible(int N)
{
    // if the number is prime,
    // and number-2 is also prime
    if (isPrime(N) && isPrime(N - 2))
        return true;
    else
        return false;
}
  
// Driver code
int main()
{
    int n = 13;
  
    if (isPossible(n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}
                      
                       
Output:
Yes


Next Article
C++ Program To Check Whether a Number is Prime or not
author
kartik
Improve
Article Tags :
  • C++
  • C++ Programs
  • C++ Basic Programs
Practice Tags :
  • CPP

Similar Reads

  • Check if a number can be represented as a sum of a Prime Number and a Perfect Square
    Given a positive integer N, the task is to check if N can be represented as a sum of a Prime Number and a Perfect Square or not. If it is possible to represent N in required form, then print "Yes". Otherwise, print "No". Examples: Input: N = 27Output: YesExplanation: 27 can be expressed as sum of 2
    15+ 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
  • C++ Program to Print the Largest Possible Prime Number From a Given Number
    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: 12691Output: 691 Appro
    2 min read
  • 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
  • Count all prime numbers in a given range whose sum of digits is also prime
    Given two integers L and R, the task is to find the count of total numbers of prime numbers in the range [L, R] whose sum of the digits is also a prime number. Examples: Input: L = 1, R = 10 Output: 4 Explanation: Prime numbers in the range L = 1 to R = 10 are {2, 3, 5, 7}. Their sum of digits is {2
    15 min read
  • C++ Program to find sum of even factors of a number
    Given a number n, the task is to find the even factor sum of a number. Examples: Input : 30 Output : 48 Even dividers sum 2 + 6 + 10 + 30 = 48 Input : 18 Output : 26 Even dividers sum 2 + 6 + 18 = 26 Let p1, p2, … pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respect
    3 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
  • 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
  • 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
  • Check if all Prime factors of number N are unique or not
    Given a number N. The task is to check whether the given number N has unique prime factors or not. If yes then print YES else print NO.Examples: Input: N = 30 Output: YES Explanation: N = 30 = 2*3*5 As all the prime factors of 30 are unique.Input: N = 100 Output: NO Explanation: N = 100 = 2*2*5*5 As
    8 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