d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Basic C Program Questions Help
12Next
Add Reply New Topic New Poll
Member
Posts: 194
Joined: Jun 21 2003
Gold: 17.10
Jan 30 2013 06:18pm
So I'm working on two questions while learning C.

The first question is, I need to create a program that prompts a user "Please enter an integer" and display whether the answer is odd or even. I'm using the logic that you can divide the input by 2, if the answer is a whole number it is even, if it's a decimal it is odd. How can you check whether the input is a decimal or not? Would this solution work for a question that asked for an input of 2 numbers, say "15 5"? Let 15 be a, 5 be b and divide a by b; if answer is whole number then it is a multiple, else it is an odd number?

Second question is, I need to create a program that prompts a user "Please enter a five integers:" for example "5 12 -10 0 1". From there it will display "The max is: 12" and "The min is -10". How can I do this without using an array?
Member
Posts: 5,366
Joined: Aug 31 2008
Gold: 1,047.00
Jan 30 2013 06:44pm
you basically use cin << "enter your integer" << endl;

if whatever he entered = even do this
else
display its odd

its not aabout checking its about programming the system to tell you whether it is odd or even. use srand % 1 so that if the remainder of the number has a 1 its odd.

we did something similar to this in my first year
Member
Posts: 194
Joined: Jun 21 2003
Gold: 17.10
Jan 30 2013 06:50pm
I have to keep it basic. Haven't used those commands yet.

cin is c++ isn't it? I'm strictly basic C.
Member
Posts: 5,366
Joined: Aug 31 2008
Gold: 1,047.00
Jan 30 2013 07:17pm
sucks2beu
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Jan 30 2013 08:03pm
using an if conditional statement, use the % operator to check whether a number is even or odd.

ex.

Code
if(3 % 2)
{
    printf("3 is even.");
}
else
{
    printf("3 is odd.");
}
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jan 30 2013 08:05pm
Quote (TheProMaster @ Jan 30 2013 08:44pm)
you basically use cin << "enter your integer" << endl;

if whatever he entered = even do this
else
display its odd

its not aabout checking its about programming the system to tell you whether it is odd or even. use srand % 1 so that if the remainder of the number has a 1 its odd.

we did something similar to this in my first year


man your first year showed you allot.... (sarcasism)

@op disregard this guys post this is simple.

basically you use the modulus (%) operator and check to see if the result returns 0 or not.

ex:

Code
if(n % 2 == 0)
{
  printf("Even\n");
}
else
{
  printf("Odd\n");
}


if you dont know how to ask a user for input and store it in a variable yet i highly recommend that you google for a beginning tutorial to c++. i recommend cpluspus.com

edit:: self got to it before me qq lol

edit2:: as for your second problem just cin all your data into separate variables ex:

Code
int a,b,c,d,e;

cin << a << b << c << d << e;


then do a series of if statements to check which is greater

Code
if(a > b && a > c && a > d && a > e)
{
  printf("a is the greatest\n);
}


This post was edited by AbDuCt on Jan 30 2013 08:12pm
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Jan 30 2013 08:09pm
Quote (AbDuCt @ Jan 30 2013 06:05pm)
man your first year showed you allot.... (sarcasism)

@op disregard this guys post this is simple.

basically you use the modulus (%) operator and check to see if the result returns 0 or not.

ex:

Code
if(n % 2 == 0)
{
  printf("Even\n");
}
else
{
  printf("Odd\n");
}


if you dont know how to ask a user for input and store it in a variable yet i highly recommend that you google for a beginning tutorial to c++. i recommend cpluspus.com

edit:: self got to it before me qq lol


;)

and for inputing five numbers and getting the min and max out of them just do something like this, there are probably better ways but w/e

Code
int n;
int max, min;

for(int i = 0; i < 5; i++)
   {
       scanf("%d", &n);

       if(i == 0)
       {
           max = n;
           min = n;
       }

       if(n < min)
       {
           min = n;
       }

       if(n > max)
       {
           max = n;
       }
   }

printf("Max: %d\nMin: %d", max, min);



This post was edited by SelfTaught on Jan 30 2013 08:12pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jan 30 2013 08:13pm
Quote (SelfTaught @ Jan 30 2013 10:09pm)
;)

and for inputing five numbers and getting the min and max out of them just do something like this, there are probably better ways but w/e

Code
int n;
int max, min;

for(int i = 0; i < 5; i++)
   {
       scanf("%d", &n);

       if(i == 0)
       {
           max = n;
           min = n;
       }

       if(n < min)
       {
           min = n;
       }

       if(n > max)
       {
           max = n;
       }
   }


that solution is cleaner then mine but it depends on if you're allowed to use loops or not. if you are use his method if not use my edit which involves allot of if checks.
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Jan 30 2013 08:22pm
Quote (AbDuCt @ Jan 30 2013 06:13pm)
that solution is cleaner then mine but it depends on if you're allowed to use loops or not. if you are use his method if not use my edit which involves allot of if checks.


is std::cin a part of C? I thought it was only C++
Member
Posts: 194
Joined: Jun 21 2003
Gold: 17.10
Jan 30 2013 08:40pm
Thanks for the great help guys!

Yeah I know how to prompt for input. And I think that "cin" is for c++ and this class is strictly C. However I think I know how to use that idea in C.

On a side note, for the min/max question; I am not allowed to use arrays. So that would mean I cannot use abducts method I assume?

Awesome help!
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll