Ok so the code makes the user type in a number. Ok if the number is odd it must say "Invalid Input". If the number is EVEN it takes the ODD numbers of it and gives you the pairs that add up to the EVEN number.

For example if I type 8

I want

8 = 1 + 7

8 = 3 + 5

how do I extract the digits?

#include <iostream>
using namespace std;

int main()

{
   int q;
   int sum = 0;
   int num;

   cout << " Please enter a positive even interger ";
   cin >> num;

   if ( num % 2 != 0 )

      cout << "Invalid Input" << endl;

   else if ( num % 2 == 0 )


   {   for ( int i = 1; i <= num; i++ )  {


        if ( i % 2 != 0 )

           ( i = q , u );


      }


      if ( q + u  == num )

         cout << q << " + " << u << num <<  endl;

   }

   return 0;


}
Ancient Dragon commented: Thanks for using code tags correctly :) +36

Well in your source code, what does i=q,u mean?

it isnt a valid statement.

You can try doing this.

#include <iostream>
using namespace std;

int main()

{
   int q;
   int sum = 0;
   int num;

   cout << " Please enter a positive even interger ";
   cin >> num;

   if ( num % 2 != 0 )

      cout << "Invalid Input" << endl;

   else if ( num % 2 == 0 )


   {   for ( int i = 1; i <= num; i++ )  {


        if ( i % 2 != 0 )
{     
        q=num-i;
        cout << q << " + " << i <<"="<< num <<  endl;

   }

   return 0;


}

This is what the program is doing now.

It takes in all odd numbers and then instead of iterating again to find a suitable match it does the following mathematically.

Consider a , b are 2 numbers and a+b=sum

Then

knowing the value of a we can find be

a+b=sum
subrating by b on both sides

a+b-b=sum-b;

b's are removed

hence

a=sum-b;

This is the same way we work out the program.

Other correction is the cout statement. You dont have any variable U , ...

So i corrected it out too.

for ( int i = 1; i <= num; i++ ) 
 {
        if ( i % 2 != 0 )

           ( i = q , u );

Your loop should update by adding two, thus you will test sequence 1 3 5.... and won't need the test for even value of i inside the loop.

Given that, subtract i from the number in question to get the other value.

> for ( int i = 1; i <= num; i++ )
for ( int i = 1; i <= num; i += 2 )
would generate all the odd numbers for you.

> 8 = 1 + 7
num = i + x right?
So maybe rearrange the expression to put x (the unknown) on one side, and the two known values on the right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.