QA.TechInterviews.com - your tech questions answered
I Need A HELP FOR C++ ASSIGNMENT ~ASAP?
i have problem with my assignment!!

the question is " write a c++ program that, given an even integer greater than two, (but no greater than one thounsand), produce as output every pair of prime number that sum to it."
eg: input a number is 12
output is 5 7

eg: 38
output: 7 31
19 19

my c++ code is:

//countfactors.cpp
//This program will determine the number of factors of a given number
//And wiil produce every pair of prime number that sum to the given number

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
int cout=0; //the number of factors

//get input
cout << " Please enter a positive even interger: ";
cin >> number;

for(int i=2; i<=1000; i++)
{
if (number%1==0)
{
cout ++;
}//end if
}//end loop
cout << "There are " << cout << " factors" << endl;
if(cout==2)
{
cout << "The number is prime" << endl;
}//end if

system("PAUSE");
return 0;
}//end main

can u check my code??
i need to give it to teacher on
tuesday 1 August 2007 , so can u reply to me today.. thanks
I Need someone to correct my C++. I just doing the basic C++ the beginner course~
Orkut Community?? what is the web address??
i am using DEV-C++ to write the code
Let me try!
Modify the input code first. Your code takes an Input, it doesn't make sure whether the input is actually +ve even integer less than 1000. That will cause problem in the codes below.
here it is:
--------------------
int number=0; // U Have not declared number as int , no can do
while (number<=2 || number >=1000)
{
cout << "\nPlease enter a positive even interger <1000 : ";
cin>>number;
}
--------------------
now the code to count factors.

declare int count , not int cout . cout is part of c++ iostream library.
loop for n / 2 times. Obviously a number greater than n / 2 can't be factor of
n , can it ? NO.
So save time and make the loop i=2 to n / 2 .
code:
------------------------
int count=0;
for(int i=2; i<=(number/2); i++)
{
if (number % i == 0)
count ++; // A single statement just after if does not require braces
}//end loop
------------------------
Check prime or not!
A number is always divisible by 1 and itself. If they are the only 2 factors the its a prime. So checking from 2 to n/2 , if the number encounters no factors then its a prime. count=0 is the condition.
Your code checks for 2 to <= n , so if u want to keep that [ i wud recommend not to ] then check for count = 1, as finally i becomes = n , and n % n = 0 !
And that must be the only factor [ seeing that you started from 2 , 1 is not checked at all] .
code:
------------------------
cout << "There are " << count << " factors" << endl;
if(count == 0)
cout << "The number is prime" << endl;
------------------------
Your code till this only checks whether number is prime and its no. of factors. The rest of the dirty works still remain. U still have to find pairs of prime numbers whose sum yield the Input number.

Try urself and put the code on or mail me. I shall check the code and advise you likewise. If you put the code here as a new question email me the topic name ok ? It wud be better if u put it with same topic.
------=-=-=-=-=-=-------
To pause the program use getch() ; and b4 include conio.h
getch() is a function which holds the program till there is a niput from keyboard. effectivly , unless user hits a [any] key the program cannot start execution of next lines of code.
code:
------------------------
cout<<" Hit a key to end execution ";
getch();
return 0;
}
------------------------
Orkut community @
http://www.orkut.com/Community.aspx?cmm=591
http://www.orkut.com/Community.aspx?cmm=38750
main()
{int a,b,c,first,second,given_n,num;
float check;
top:
clrscr();
printf("Enter number smaller than 1000 & greater than two :");
scanf("%d",&given_n);
if(given_n>1000&&given_=<2)goto top;
for(first=1;first<given_n;)
{
for(a=2,b=1;a<num;a++)
{
if(num%a==0)
{b=0;goto step;}
}
step :
if(b==1)
{first=num;
second=given_n-first;c=1;goto no2;
}
c=0;
no2:
if(c==1)
{for(b=1,a=2;a<second;a++)
{
if(second%a==0)
{b=0;goto step1;}
}
}
step1:
if(b==1)
printf("%d %d\n\n",first,second);
}
getch();
}
are you looking for us to do your work ???
Let me try!
Modify the input code first. Your code takes an Input, it doesn't make sure whether the input is actually +ve even integer less than 1000. That will cause problem in the codes below.
here it is:
--------------------
int number=0; // U Have not declared number as int , no can do
while (number<=2 || number >=1000)
{
cout << "\nPlease enter a positive even interger <1000 : ";
cin>>number;
}
--------------------
now the code to count factors.

declare int count , not int cout . cout is part of c++ iostream library.
loop for n / 2 times. Obviously a number greater than n / 2 can't be factor of
n , can it ? NO.
So save time and make the loop i=2 to n / 2 .
code:
------------------------
int count=0;
for(int i=2; i<=(number/2); i++)
{
if (number % i == 0)
count ++; // A single statement just after if does not require braces
}//end loop
------------------------
Check prime or not!
A number is always divisible by 1 and itself. If they are the only 2 factors the its a prime. So checking from 2 to n/2 , if the number encounters no factors then its a prime. count=0 is the condition.
Your code checks for 2 to <= n , so if u want to keep that [ i wud recommend not to ] then check for count = 1, as finally i becomes = n , and n % n = 0 !
And that must be the only factor [ seeing that you started from 2 , 1 is not checked at all] .
code:
------------------------
cout << "There are " << count << " factors" << endl;
if(count == 0)
cout << "The number is prime" << endl;
------------------------
Your code till this only checks whether number is prime and its no. of factors. The rest of the dirty works still remain. U still have to find pairs of prime numbers whose sum yield the Input number.

Try urself and put the code on or mail me. I shall check the code and advise you likewise. If you put the code here as a new question email me the topic name ok ? It wud be better if u put it with same topic.
------=-=-=-=-=-=-------
To pause the program use getch() ; and b4 include conio.h
getch() is a function which holds the program till there is a niput from keyboard. effectivly , unless user hits a [any] key the program cannot start execution of next lines of code.
code:
------------------------
cout<<" Hit a key to end execution ";
getch();
return 0;
}
------------------------
Orkut community @
http://www.orkut.com/Community.aspx?cmm=591
http://www.orkut.com/Community.aspx?cmm=38750
join orkut C++ community they will definitly help u

Back to QA. TechInterviews.com. Powered by Yahoo! Answers and TechInterviews.com community.