Odd/Even integers

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 2081 Views - Last Post: 03 August 2011 - 09:10 PM Rate Topic: -----

#1 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Odd/Even integers

Posted 02 August 2011 - 01:57 AM

I need to write a program that prints out all the odd or even integers between any two integers (inclusive) entered by the user. The user should be able to select whether odd or even integers are to be printed. Use a while loop to force correct entry for the odd/even choice (use type char for choice).

The program should contain two ways for printing integers, and automatically produce two outputs (which obviously must be identical); these two ways are in seperate functions.

a. The first way your code should contain the odd/even conditional statements before a while loop, and use a loop increment of two.

b. In the second way the odd/even conditional statement(s) should be inside the while loop, which should use a loop increment of one.

Is This A Good Question/Topic? 0
  • +

Replies To: Odd/Even integers

#2 CreamDelight  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 90
  • Joined: 04-July 11

Re: Odd/Even integers

Posted 02 August 2011 - 02:04 AM

Okay.. we got the problem but how far have you gone..
Have you started making this?

Post your code if you need some clarification.. ^^
Was This Post Helpful? 0
  • +
  • -

#3 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: Odd/Even integers

Posted 02 August 2011 - 02:05 AM

View PostCreamDelight, on 02 August 2011 - 02:04 AM, said:

Okay.. we got the problem but how far have you gone..
Have you started making this?

Post your code if you need some clarification.. ^^


Oops yeah I have written some code! Having trouble with my computer at the moment, working on it...
Was This Post Helpful? 0
  • +
  • -

#4 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: Odd/Even integers

Posted 02 August 2011 - 02:17 AM

I can't find the file I wrote anymore!

I have:



//-----------------------------------------------------------------------------
//Programmer: Charles Syms
//Date: 2nd August 2011
//Folder: H:\My Documents\Programming Assignments\Assignment 3
//Description: Question 9 of Assignment 3

#include <iostream>
#include <conio.h>
using namespace std;

void printInteger( char );
//-----------------------------------------------------------------------------
int main(int argc, char *argv[])
{   
   int myInteger1;
   int myInteger2;
   
   cout << "Enter any 2 integers: " << endl << endl;
   cin >> myInteger1;
   
   cout << "... and the next one: " << endl << endl;
   cin >> myInteger2;
    
   _getch();
   return 0;
}




even if you could help me towards what I need to do...
Was This Post Helpful? 0
  • +
  • -

#5 CreamDelight  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 90
  • Joined: 04-July 11

Re: Odd/Even integers

Posted 02 August 2011 - 02:31 AM

okay here's what you need..

you said that the user will select if the one to be printed is odd or even, so you need to let the user input..

Next.. finish your function..
void printInteger( char )

i'll ask you, in your college algebra, how do you identify if a number is odd or even? ^^
If you will get that you will easily get the code..
Was This Post Helpful? 0
  • +
  • -

#6 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: Odd/Even integers

Posted 02 August 2011 - 02:35 AM

View PostCreamDelight, on 02 August 2011 - 02:31 AM, said:

okay here's what you need..

you said that the user will select if the one to be printed is odd or even, so you need to let the user input..

Next.. finish your function..
void printInteger( char )

i'll ask you, in your college algebra, how do you identify if a number is odd or even? ^^
If you will get that you will easily get the code..


I never did algebra at college...! I know that it has to be divisible by 2. so 2n even 2n+1 odd

This post has been edited by rdhc1330: 02 August 2011 - 02:37 AM

Was This Post Helpful? 0
  • +
  • -

#7 CreamDelight  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 90
  • Joined: 04-July 11

Re: Odd/Even integers

Posted 02 August 2011 - 02:46 AM

Your right..

so...
 if(n % 2 == 0)
      i am an even
 else
      i am an odd

...You already got the idea

this snippet will print all even integers in a range of 0 - 10..

for(int x = 0; x <= 10; x++)
{
   if(x % 2 == 0)
      printf("%d\n", x);
}

This post has been edited by CreamDelight: 02 August 2011 - 02:47 AM

Was This Post Helpful? 0
  • +
  • -

#8 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: Odd/Even integers

Posted 02 August 2011 - 02:48 AM

View PostCreamDelight, on 02 August 2011 - 02:46 AM, said:

Your right..

so...
 if(n % 2 == 0)
      i am an even
 else
      i am an odd

...You already got the idea

this snippet will print all even integers in a range of 0 - 10..

for(int x = 0; x < 10; x++)
{
   if(x % 2 == 0)
      printf("%d\n", x);
}


yes - but the user should get to decide whether odd or even integers are to be printed? and also a while loop needs to be used, not a for loop.

This post has been edited by rdhc1330: 02 August 2011 - 02:54 AM

Was This Post Helpful? 0
  • +
  • -

#9 CreamDelight  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 90
  • Joined: 04-July 11

Re: Odd/Even integers

Posted 02 August 2011 - 02:54 AM

you need to scan an input..

scanf("%c", choice);

for(int x = 0; x <= 10; x++)
{
     if(choice == 'o')
     {
       if(x % 2 != 0)
           printf("%d\n", x);
     }
     else if(choice == 'e')
     {
       if(x % 2 == 0)
          printf("%d\n", x);
     }
}


oH.. i'm giving the whole code..
i think you can see everything here so any problem just refer to the code and your logic..
Was This Post Helpful? 0
  • +
  • -

#10 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: Odd/Even integers

Posted 02 August 2011 - 02:58 AM

View PostCreamDelight, on 02 August 2011 - 02:54 AM, said:

you need to scan an input..

scanf("%c", choice);

for(int x = 0; x <= 10; x++)
{
     if(choice == 'o')
     {
       if(x % 2 != 0)
           printf("%d\n", x);
     }
     else if(choice == 'e')
     {
       if(x % 2 == 0)
          printf("%d\n", x);
     }
}


oH.. i'm giving the whole code..
i think you can see everything here so any problem just refer to the code and your logic..


can you not give me the rest?
I'm confused now
Was This Post Helpful? 0
  • +
  • -

#11 CreamDelight  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 90
  • Joined: 04-July 11

Re: Odd/Even integers

Posted 02 August 2011 - 03:03 AM

I think its illegal here to let other people do the code and your not giving your part..
maybe do some and code and post here were your stuck..
Was This Post Helpful? 0
  • +
  • -

#12 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: Odd/Even integers

Posted 02 August 2011 - 03:08 AM

View PostCreamDelight, on 02 August 2011 - 03:03 AM, said:

I think its illegal here to let other people do the code and your not giving your part..
maybe do some and code and post here were your stuck..



I have this:


//-----------------------------------------------------------------------------

#include <iostream>
#include <conio.h>
using namespace std;

void printInt1( char number );
void printInt2( char  );
//-----------------------------------------------------------------------------
int main(int argc, char *argv[])
{   
   int myInteger1;
   int myInteger2;
   
   cout << "Enter any 2 integers: " << endl << endl;
   cin >> myInteger1;
   
   cout << endl << "... and the next one: " << endl << endl;
   cin >> myInteger2;
   
   printInt1( myInteger1 );
    
   _getch();
   return 0;
}
//-----------------------------------------------------------------------------
void printInt1( char number );
{
   scanf("%c", choice);

   for(int x = 0; x <= 10; x++)
   {
      if(choice == 'o')
      {
      if(x % 2 != 0)
      cout << ("%d\n", x);
      }
      
      else if(choice == 'e')
      {
      if(x % 2 == 0)
      cout << ("%d\n", x);
      }
   }
}



Was This Post Helpful? 0
  • +
  • -

#13 CreamDelight  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 90
  • Joined: 04-July 11

Re: Odd/Even integers

Posted 02 August 2011 - 03:18 AM

Your function should receive three parameters..
Like this..

void printInt1( char choice, int int1, int int2 )

char choice is for the decision of the user if he will display it odd or even..
int1 is for the first integer and int2 for the second..

having these three parameters we can now start the loop..


void printInt1( char choice, int int1, int int2 )
{
   for(; int1 <= int2; int1++)
   {
      if(choice == 'o' || choice == 'O')
      {
      if(int1 % 2 != 0)
      cout << int1 << endl;
      }
      
      else if(choice == 'e' || choice == 'E')
      {
      if(int1 % 2 == 0)
      cout << int1 << endl;
      }
   }
}


That's all i can offer to you..
You should be the one who will make your main function...
Was This Post Helpful? 1
  • +
  • -

#14 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: Odd/Even integers

Posted 02 August 2011 - 03:23 AM

View PostCreamDelight, on 02 August 2011 - 03:18 AM, said:

Your function should receive three parameters..
Like this..

void printInt1( char choice, int int1, int int2 )

char choice is for the decision of the user if he will display it odd or even..
int1 is for the first integer and int2 for the second..

having these three parameters we can now start the loop..


void printInt1( char choice, int int1, int int2 )
{
   for(; int1 <= int2; int1++)
   {
      if(choice == 'o' || choice == 'O')
      {
      if(int1 % 2 != 0)
      cout << int1 << endl;
      }
      
      else if(choice == 'e' || choice == 'E')
      {
      if(int1 % 2 == 0)
      cout << int1 << endl;
      }
   }
}


That's all i can offer to you..
You should be the one who will make your main function...


thankyou
Was This Post Helpful? 0
  • +
  • -

#15 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: Odd/Even integers

Posted 02 August 2011 - 04:02 AM

I'm still amazingly confused, any help you could offer me would be much appreciated. Understandable if you can't... I have this:


//-----------------------------------------------------------------------------
//Programmer: Charles Syms
//Date: 2nd August 2011
//Folder: H:\My Documents\Programming Assignments\Assignment 3
//Description: Question 9 of Assignment 3

#include <iostream>
#include <conio.h>
using namespace std;

void printInt1( char choice );
void printInt2( char  );
//-----------------------------------------------------------------------------
int main(int argc, char *argv[])
{   
   int myInteger1;
   int myInteger2;
   
   cout << "Enter any 2 integers: " << endl << endl;
   cin >> myInteger1;
   
   cout << endl << "... and the next one: " << endl << endl;
   cin >> myInteger2;
   
   printInt1;
    
   _getch();
   return 0;
}
//-----------------------------------------------------------------------------
void printInt1( char choice, int int1, int int2 )
{
   for(; int1 <= int2; int1++)
   {
      if(choice == 'o' || choice == 'O')
      {
      if(int1 % 2 != 0)
      cout << int1 << endl;
      }
       
      else if(choice == 'e' || choice == 'E')
      {
      if(int1 % 2 == 0)
      cout << int1 << endl;
      }
   }
}



I don't know what to do next.

This post has been edited by rdhc1330: 02 August 2011 - 04:19 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2