Jump to content

Odd / Even Programme in C#...Please Help,Help,Help.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
umair10

umair10

    Newbie

  • Members
  • Pip
  • 3 posts
hi,

I am new to C# Programming so Is there any Angel who can post codes for simple Application of Odd/Even in C#....Programme is as follows

Have the user enter a value, and display a message telling the user whether the integer number was entered Odd or Even?:confused:

I will be very thankful.:)

Edited by umair10, 29 June 2010 - 02:12 PM.


#2
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
How far have you got; what are you stuck on?

#3
umair10

umair10

    Newbie

  • Members
  • Pip
  • 3 posts
hello,

I have done one simple math Calculation small application....i just want to know what should i change in this to get to know Odd/Even...i mean if user enters any value then message should display that entered integer number was odd / even? i am showing you down project so you can assess which change i need in this.

Simple Math Calculation

private void btnCalc_Click(object sender, EventArgs e)
{
bool flag;
int Operand1;
int Operand2;
int answer;
//check First Input step
flag = int.TryParse(txtOperand1.Text, out Operand1);
if (flag == false)
{
MessageBox.Show( "Enter a whole number" , "input Error");
txtOperand1.Focus();
return;
}
//Check 2nd Input Step
flag = int.TryParse(txtOperand2.Text, out Operand2);
if (flag == false)
{
MessageBox.Show("Enter a whole number", "input Error");
txtOperand2.Focus();
return;
}
// Process Step
answer = Operand1 / Operand2;
//Display Step

txtResult.Text = Operand1.ToString() + "divided by" +
Operand2.ToString() +
"equals" + answer.ToString();

txtResult.Visible = true;
}

private void btnExit_Click(object sender, EventArgs e)
{
Close();
}



}

Thanx in advance:)

#4
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
Ok, well you program there has all the bones nessary. Have you thought how your will be able to tell if its even or odd? Anyway when you know how todo it, change the code so that you only have 1 operand, replace operand2 with a value which lets you determine which of the two, even or odd it is. The do a messagebox as your've already done.

#5
umair10

umair10

    Newbie

  • Members
  • Pip
  • 3 posts
hi
hmm still little confused. what do u mean by replace operand2 with value that determines odd/even....rest i have understood that i need 1 operand. Can you please write the code? i have thought a lot but no way.

#6
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts

umair10 said:

hi
hmm still little confused. what do u mean by replace operand2 with value that determines odd/even....rest i have understood that i need 1 operand. Can you please write the code? i have thought a lot but no way.

Sorry that sentance didn't make that much sense. You only need one operand, so you can remove the code which extracts or does anything to operand2. Then you need to replace the line which reads
// Process Step
answer = Operand1 / Operand2;

with somthing like:
bool odd = false;
if(Operand1??????)
{
    odd = true;
}

Where the ???? bit is your code to check if it's odd or not.

I'm afraid I'm not going to write the code. Programming is about translating what you want to coputer todo into a langauge that enables it to do it, at the moment I'm not sure you know what you want to do at a low enough level.

So the first step is this, if I gave you three numbers 2, 3 and 4, can you tell me which ones are odd and which are even. Then can you tell me the algorithm you use to work this out. Then if you have trouble translating the algorithm, we can help.

#7
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
Use [ code] tags [ /code] to write your code on the forum.
As for you program, all you have to do is check if it can be divided by 2:
//Taking input from textBox
double Number = double.Parse(textBox1.Text);
if (Number % 2 == 0) {
   //If the number divided by 2 has no remainder then it's even
   label1.Text = "Number is even";
}
else {
  //If it has a remainder then it is an odd number
  label1.Text = "Number is odd";
}
Any questions?
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#8
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
You can also check my tutorial to learn some C# basics that you need to know: Visual C# Programming Basics
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics