📄 2prime.cpp
字号:
#include<stdio.h>
#include<conio.h>
#include<math.h>
/*
Written By: Morteza Zafari 2001
This program calculates all possible combination of two
prime numbers which produce a given even number.
*/
int IsPrime(int); //returns 1 if prime. returns 0 if not prime.
void main()
{
int i,r,num;
clrscr();
printf ("_____________________________________________________________\n");
printf (" This program will calculate all possible combination of two\n prime numbers which produce a given even number. \n");
printf ("_____________________________________________________________\n");
printf ("\nPlease input an even number: ");
scanf ("%d",&num); // Getting a number from the user.
if (num%2 != 0) // Checking if the given number is even or not.
{
printf("\n%d is not an even number",num);
getch();
return; //Exit the program.
}
//---------- Start -----------//
/*
This program uses just one 'For' statement
as we know 'A+(B-A)=B' so if 'A' AND '(B-A)'
are prime numbers, they are the answer.
*/
for (i=2;i<=(num/2);i++)
{
if ( IsPrime(i) && IsPrime(num-i) )
{ // If BOTH of them are prime, Show them.
printf ("\n %4d + %4d = %d ",i,(num-i),num);
}
}
printf("\n\n Thanks for using this program.\n Morteza Zafari\n 2001");
getch();
}
//---------- End -----------//
IsPrime(int num)
{
int i;
if (num==1) return(0); //If it's 1, it's not prime.
if (num==2) return(1); //If it's 2, it's prime.
if (num%2==0) return(0); //If it is even then sure it's not prime.
else
{
for (i=2;i<=sqrt(num);i++)
{
if ( (num%i) == 0 ) return(0);
}
return(1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -