Jump to content

Create a program that will display even or odd numbers from x to y

- - - - -

  • Please log in to reply
16 replies to this topic

#1
ren

ren

    Newbie

  • Members
  • Pip
  • 2 posts
who can help me in my program..who knows about this ...please answer this...

Create a program that will display even or odd numbers from x to y.
where
x ≤ y
x,y ≥ 0

Sample Output 1:

Enter x: 3
Enter y: 12
Press [1] to display Even numbers and [2] to display Odd numbers.
1

Even Numbers from 3 to 12:
4
6
8
10
12

Sample Output 2:

Enter x: 3
Enter y: 12
Press [1] to display Even numbers and [2] to display Odd numbers.
1

Odd Numbers from 3 to 12:
3
5
7
9
11

Sample Output 3:

Enter x: 12
Enter y: 3
x should be less than or equal to y

Sample Output 4:

Enter x: -3
Enter y: 12
x and y should be greater than zero.

Edited by Roger, 05 August 2010 - 03:01 PM.
move to C forum


#2
ren

ren

    Newbie

  • Members
  • Pip
  • 2 posts
we are using C.

#3
Roger

Roger

    If nothing goes right, go left.

  • Administrators
  • 718 posts
  • Programming Language:C, PHP
  • Learning:Python
Is this for homework help? You should post your code here and someone might be able to help you. No one is going to write the code for you, though.
Check out our update Guidelines/FAQ. When posting code, remember to use code tags - Posted Image.

#4
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
The logic remains the same for any programming language. Please provide your code, although I don't program in C.

If you didn't write any code, I will give you some heads up:

You have a very simple problem here - let's turn this into 1st grade problem.

1. What is the first thing we do? Find out the goals and what we needed.
Clearly we have a problem with asks the users to manually input two numbers, namely x and y, where x must be less than or equal to y, and both x and y must be greater than or equal to zero. There should be two options, 1 and 2, 1 is evne, and 2 is odd.

We know our goal, and conditions. Now, move on to the "need".
What do we need? Something to hold the input values. How many of them do we need? You answer it. What kind of "thing" do we use to hold a value in computer programming? You figure that word out, it begins with the letter v.

2. Now, let's not write our program. Do it on your paper.

In C/C++, for the very beginner, we have a basic template, for C:
#include<?????>

int main()
{
// do something about it

return 0;
}

Do something means write the code. Let's begin constructing our code - but we do a logic coding before typing the codes!!!!
Logic code - what is it? Simply the process of operation.

Have m numbers of "thing" to hold that m numbers of values we need from user.
From sample output, we have to print out a message to ask the user to input x and y, consecutively.
Then we determine the condition
If it conditions is good, we do something about it
User continues to choose 1 or 2.
Choose 1 we let perform something to make go through the numbers between x and y, and print only the even one
Choose 2 we do similar thing but only the odd one
If it didn't, we do another thing about it.


Final hints:
if, else, for, while.... choose any combination...

Now you can write your code.
Google C tutorial if you don't even know how to write in C.

#5
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Also note

0%2=0
1%2=1
2%2=0
3%2=1

Notice all 0's are even, and 1's are odd. Throw it in a loop, add an if statement that echo's it out.

#6
breza

breza

    Newbie

  • Members
  • Pip
  • 5 posts
clear!

Edited by breza, 08 August 2010 - 01:12 AM.


#7
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
When you read z variable, check if it's 1 or 2 and then display numbers. Also, next time please use code tag ( # button ), makes it easier to read. :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#8
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
I formated the code a bit, make it a little bit easier to read. Don;t worry about space! If I were to grade your program based on your previous format, I would give you an F.

// [P0] BEGINS

#include <stdio.h>
int main()
{

// [P0] ENDS

   int x,y,z,i,n;     // [P1]

// [P2] BEGIN
   printf("x:");             
   scanf("%d",&x);     

   printf("y:");          
   scanf("%d",&y);   

//    END of [P2]


// [P3] BEGIN
   printf("Press [1] to display Even numbers and [2] to display Odd numbers");
   scanf("%d",&z);
   for(i=0;i<=x;i++);
   {
      if((i%2)==0);
      printf("%d",z);
   }
// END OF [P3]


   return 0;
}

[P0] GOOD.
[P1]
So far so good. However,

int x,y,z,i,n;
What happen to the n? I don't see it throughout the program.

[P2]
Up until this point, I would urge you to follow the logic that I constructed, which is exactly what the program intends to be.
Evaluation!! You forgot to evaluate x and y (the two conditions!!!) before asking for the even / odd.

Look at [P3] carefully.

// [P3] BEGIN
   printf("Press [1] to display Even numbers and [2] to display Odd numbers");
   scanf("%d",&z);
   for(i=0;i<=x;i++);
   {
      if((i%2)==0);
      printf("%d",z);
   }
// END OF [P3]
So z is the input of [1] or [2].
You are iterating (looping) through from i = 0 to x. The condition says x is less than or equal to y, so why are you looping up until x? The end point of the iteration is the y value, right?

Remember the condition, x, y greater than or equal to zero, and x must be less than or equal to y.

Now as far as the loop goes, it doesn't start from i = 0 either. Remember your condition. It is the odd / even numbers between x and y.
Your loop should evaluate the condition between x and y (including x and y).

Once you are done with the program. I will discuss the loop with you. You have to be careful with i <= y and i < y, I will not discuss this at the moment, however. Keep my version: i < y

To save the code construct, you can do a trick.
Keep this structure


// [P3] BEGIN
   printf("Press [1] to display Even numbers and [2] to display Odd numbers: ");
   scanf("%d",&z);
   for(i = x;i < y;i++)
   {
        // add by jwxie
       // you need two if statements to evaluate the conditions of [1] and [2]
       // review my previous post about the program logic
       
     // for [1] as well as even, do something about it
    // what is it? well, print out each individual i that satisfies your if condition
    // what kind of if condition? i told you, [1] and even
 
   // for [2] and odd, do similar operation
   }
// END OF [P3]

Your whole program should be something like this

#include <stdio.h>
int main()
{
   int x,y,z,i;     // [P1]

// [P2] BEGIN
   printf("x:");             
   scanf("%d",&x);     

   printf("y:");          
   scanf("%d",&y);   

//    END of [P2]


// [P3] BEGIN
   printf("Press [1] to display Even numbers and [2] to display Odd numbers: ");
   scanf("%d",&z);
   for(i = x;i < y;i++)
   {
        // add by jwxie
       // you need two if statements to evaluate the conditions of [1] and [2]
       // review my previous post about the program logic
       
     // for [1] as well as even, do something about it
    // what is it? well, print out each individual i that satisfies your if condition
    // what kind of if condition? i told you, [1] and even
 
   // for [2] and odd, do similar operation
   }
// END OF [P3]


   return 0;
}
I even fixed the those errors for you.

Look. You missed some semicolon ; and something you misued the semicolon.

Look at your code. For the for loop, you immediately close it with a semicolon. NOTHING WILL BE DONE THEN.

Read compiler errors.

If you really need to write C code, and don't have a good IDE, I will urge you use jGRASP
Here is the tutorial I wrote. You can use this for C too.
http://www.i3physics...iles/jGRASP.pdf

I wrote your program already. When you are done, and we are happy with it, I will let you see my version.
Don't get fancy for now. Just follow the logics!

#9
breza

breza

    Newbie

  • Members
  • Pip
  • 5 posts
thnx for the help.. i will continue construct my program.... im so tired doing it last night i will continue it now.. thnx for the help again..

#10
breza

breza

    Newbie

  • Members
  • Pip
  • 5 posts
do i have to make another 2 variable to evaluate the condition of 2 and 1?
how will i use modulo?
can i use if else?

#include <stdio.h>

int main()

{

   int x,y,z,i;

   printf("Enter x:");

   scanf("%d",&x);

   printf("Enter y:");

   scanf("%d",&y);

   printf("Press [1] to display Even numbers and [2] to display Odd numbers: ");

   scanf("%d",&z);

   for(i = x;i < y;i++)

   {


        // add by jwxie

       if(z=1)

       // you need two if statements to evaluate the conditions of [1] and [2]

       // review my previous post about the program logic

       printf("Even numbers from %d to %d:\n",x,y);//how can i print the even numbers below?

     // for [1] as well as even, do something about it

    // what is it? well, print out each individual i that satisfies your if condition

    // what kind of if condition? i told you, [1] and even


   // for [2] and odd, do similar operation

     else

   printf("Odd numbers from %d to %d:\n",x,y);//how can i print odd numbers below?

   }

// END OF [P3]



   return 0;

}


sorry for that bad program.. i cant fix it right now because still have my class... i dont have much time ill continue later... deadline for this program is on monday... plss give me some hints... because im not that good in programming... its not my major.. im an Electrical Engineering Technology.... but im doing my best...

i just do 4 program the helloworld, Celsius to Fahrenheit, longhand multiplication, and area of circle but one of them has no similarities to this program i dont understand much in looping.

again thnx for the help guys... i really appreciate your comments and suggestions..

#11
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
Hi. I understand the problem, but you have to put up some confidence. I was at your position a year ago, and I still have problem writing codes. Everyone does.

To really clear up the problem here, let me walk through the whole program with you again.

1. Ask the user to input x
2. Ask the user to input y
3. Now, evaluate x,y ( x must be less than or equal to y, and both x and y must be greater than or equal to 0)
4. If x,y meets the requirement, go on to #5. Otherwise, print out an error message asking it again (we can just let the program die in this case)
5. Now, let's ask the user to choose [1] for even, [2] for odd
6. If the user choose [1], we print out only the even numbers that is between x and y (including x and y)
7. If the user choose [2], we print out only the odd numbers that is between x and y (including x and y)
8. end of the program


Yes. Use what I provide, and let's start from there.
I don't want to destroy your code so I said keep the structure.

Quote

for(i = x;i < y;i++)
{
if(z=1)
printf("Even numbers from %d to %d:\n",x,y);
You are one the right track. You are asking the right question. How do we print out the even / odd numbers? Well. I said keep the structure, and in programming, we can often use comparison to evaluate two or more things.

But first let me correct you one little thing.
if (z = 1) is wrong, because the equal sign = is not valid when we are trying to compare something. The statement if (z = 1) is trying to see whether z is equivalent to the integer 1.

In general, when we have a variable, let say
int x = 2, we are assigning the variable which is an integer, the value 2. This = sign means "assignment". In other words, x now holds an integer value "2".

Remember, whenever you try to evaluate something (usually when we say evaluate, we are suggesting comparison, or checking something... true/false, for example), you don't use = for comparison.

There is this == double equal signs operator. We use this equal operators for comparison.

Does x has the value of 2? x == 2 true since int x = 2, right?

So we have to fix that.

if(z==1)
printf("Even numbers from %d to %d:\n",x,y);
Now let;s answer your question: how do I print out only the even? Well, let's give a little thought. You are iterating (looping) from x to y. (let say x = 2, and y = 8)

So the first value we take is i = x, which is now i = x = 2, thus i = 2, right?
This is how a for loop works:
let's take a simple for loop
for ( int aNumb = x; aNumb  < y; aNumb++)
{
   printf("%d", aNumb)    // we want to print every value that aNumb is assigned to
}
[1] the very first time the loop begins with the first value assigned to aNumb (our variable is aNumb in this case)
[2] then the compiler moves to the second piece of the for loop --> aNumb < y
[3] If #2 is true, we jump down the printf statement. Since aNumb is 2, the compiler will print 2
[4] Now, there is no furthur instructions follows by the printf statement, so we go back to the aNumb++ increment piece.
[5] This increment made aNumb = 2 increased by 1, so aNumb is now aNumb = 3
[6] Now we check the 2nd piece again: aNumb < y??? TRUE
[7] now printf statement
[8] increment by 1, so aNumb = 4
...... and so on
[nth] so until aNumb = 7 (and remember our y = 8), great. Is aNumb < y ?? True! So printf statement.
Then increment by 1, omg, aNumb = 8, and this fail the condition aNumb < y since 8 < y, so the for loop comes to an end.

As you can see, each time we going through the current number of aNumb, the compiler will check the condition aNumb < y, and if it is true, we continues.

Since we are constructing an if-statement: if the user enters [1]. But to save time and space, we can do another check simultaneously.

If we do the check [1] or [2] and checking even and odd numbers separately, we will require 4 if statements, and 2 for loops. To keep your construct, we only require 1 for loop, and 2 if statements. The following is the outcome:

ASK FOR AN INPUT  [1] or [2] (z variable):
if ( z == 1)
{
    print only the even through a loop
}

if ( z == 2)
{
   print only the odd one through a loop
}
That's a waste of time and bad logic.

Look at your original program. We begins the for loop AFTER the user instructs [1] or [2]. We have that value saved in the variable z . Thus, we don't need to worry about losing track of it. We can use it at any time we want in this particular program.

After the users put down [1], z = 1, good. Now the loop begins, it begins with i = x (let say x = 2), so i = 2. Great.

We can now print out the even numbers using the
modulo method.
Basically:

a_number % 2 == 0 is even
(a_number % 2 != 0) is odd
a_number % 2 == 1 is odd

The modulo method gives the remainder of a division. For an even number, the reminder is always zero. Plain zero.

So what if we do the check z == 1 and i%2 == 0 inside the same if condition, and connect them using the AND operator && ??

Note that && is the AND operator, and || (two bars) is the OR operator

If we check both (z == 1 && (i%2 == 0)) and is true, then the compiler will only print out the even numbers.

We perform a similar code for the odd conditions.

When you print, you are printing the variable i. Remember, x and y are just the initial and end points. Your loop is going from x to y, and in between there are other values too! So if you just printf x,y you will never get anything.

So your printf statement should only print the variable i


PS: For EE student, don't underestimate the power of programming, and the need of programming. You definitely need it.

#12
breza

breza

    Newbie

  • Members
  • Pip
  • 5 posts
my current status.. it works but it dosent display the correct value for odd and it display twice...
but i think its better than before.. :)




2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users