Site icon T4Tutorials.com

Sum of Two Prime Numbers – C++ program

Write a program in C++ to Check Whether a Number can be Express as a Sum of Two Prime Numbers.

Prime Number C++

Prime Number Program for Positive Numbers

This is the prime Number Program for Positive Numbers, if the user enter a negative number then the program asks the user, again and again, to enter the positive number, until the user enters the positive number.

Prime Number Program for Positive Numbers in C++

#include <iostream>
using namespace std;
int main(){
   	T4Tutorials:
    int num; 
	int i;
  	bool isPrime = true;
  cout << "Enter a positive integer: ";
  cin >> num;
 if (num>0)
//to ensure the length of number is at least 2
{
  for(i = 2; i <= num / 2; ++i)
  {
      if(num % i == 0)
      {
          isPrime = false;
          break;
      }
  }
  if (isPrime)
      cout << "This is a prime number";
  else
      cout << "This is not a prime number";
}
else
{
//to ensure that if the length of number is less than 2, then goto T4Tutorials albel (just
//below the main function) and again ask the user to enter the number.
	 goto T4Tutorials;
}
   return 0;
}

Output

Prime Number Program for Positive Numbers in C++

Flowchart of Sum of Two Prime Numbers – C++ program

Flowchart of Sum of Two Prime Numbers Program in C++

C++ program of Sum of Two Prime Numbers

#include<iostream>
using namespace std;
int main()
{
	int num, i, j;
	int f1=1 , f2=1, f3=0;
	cout<<"Enter a +ve Integar : ";
	cin>>num;
	i=3 ;
	do
	{
		f1=1;
		f2=1;
		j=2;
		do  
		{
			if(i%j==0)
			{
				f1=0;
				j=i;
			}
			j=2;
			do
			{
				if((num-i)%j==0)
				{
					f2=0; 
					j=num-i;
				}
				j++;
			}
			while(j<num-i );
			if(f1==1 && f2==1)
			{
				cout<<num <<" = "<<i<<" + "<<num-i<<endl;
				f3=1;
			}
			j++;
		}
		while(j<i);
		i++;
	}
	while(i<=num/2);
	if(f3==0)
	{
		cout<<num<<" can not be expressed as sum of two prime numbers.";
	}
}

Output

Enter a +ve Integer: 4

4 = 3+1

Excercise

Find the possible mistakes in the following Shamil’s Flow Table of the program of  Sum of Two Prime Numbers in C++.

Loop 1 Loop 2 Loop

 3

If 1 if 2 if 3 If 4 What line will execute
1 time

i=3;

j=2 j=2

j=3;

j=4

j=5

j=6

 

False false True False 1,2,3,4,5,6,7,8,9,10,11,12,

13,14,15,16,17,

22,23,24,25,30,31,32,(23,24,25,

26,27,28,29,30,31,32)4,33,34,35

,36,37,38,39,40,41,42,43,44,48

2 time

I=4

j=2

j=3

 

J=2

J=3

J=4

J=5

True True False True 1,2,3,4,5,6,7,8,9,10,11,12,13,

14,15,16,17,22,23,24,25,30,

31,32,(23,24,25,

26,27,28,29,30,31,32)4,33,

34,35,36,37,38,39,40,41,

42,43,

10,11,12,13,14,15,16,17,

18,19,20,21,22,(23,24,25,26,

27,28,29,30,31,32)2

,(23,24,25,30,31,32,)2,33,38,

39,40,15,16,17,22,23,24,25,

26,27,28,29,30

,31,32,(23,24,25,30,31,32,)2,

32,33,38,39,40,41,42,

43,44,45,46,47,48

 

3 time

I=5;

J=2 J=2 False False True False 1,2,3,4,5,6,7,8,9,10,11,

12,13,14,15,16,17,22,23,

24,25,30,31,32, (23,24,25,

26,27,28,29,30,31,32)4,

33,34,35,36,37,38,39,

40,41,42,43,

10,11,12,13,14,15,16,

17,18,19,20,21,22,

(23,24,25,26,27,28,29,30,31,32)2

, (23,24,25,30,31,32,)2,

33,38,39,40,15,16,17,22,

23,24,25,26,27,28,29,30

,31,32, (23,24,25,30,31,32,)2,

32,33,38,39,40,41,42,43,

10,11,12,13,14,15,16,17,22,

(23,24,25,26,27,28,29,30,31,32)3,

33,34,35,36,

37,38,39,40, 15,16,17,22,

(23,24,25,26,27,28,29,30,31,32)2,

33,34,35,36,

37,38,39,40, 15,16,17,22, 23,

24,25,26,27,28,29,30,31,32,

33,34,35,36,

37,38,39,40,41,42,43,44,48,

 

 

 

 YouTube video player

Exit mobile version