Link to home
Start Free TrialLog in
Avatar of missq
missq

asked on

data entry error..!!

Can somebody please tell me what to put in my code for positive integer division if someone enters a negative number...??  This is my code:

/*integer division using 2 positive integers
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

{
      main ()

      int dividend = N;
      int didvisor = D;
      int quotient = Q;
      int remainder = R;

      printf("Enter a positive whole number to be used as the dividend");
      scanf("%d", &N);
      printf("Enter a positive whole number to be used as the divisor");
      scanf("%d", &D);

      //perform the integer division
      Q = N/D;
      printf("\n The quotient is %d", Q);

      //perform the modulus
      R = N%D;
      printf(("\n The remainder is %d", R);
      return 0;
}
ASKER CERTIFIED SOLUTION
Avatar of newexpert
newexpert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of onki
onki

You can first check whether the input number is positive or not before handling it, like this:

scanf("%d", &N);

/* set a while loop to check if N is a non-positive number */
while(N<=0)   printf("Please enter a POSITIVE INTEGER!\n");

/* until the input is valid, the while loop will no longer be satisfies and the following code will be generated then */
Try to use the good old friend abs() liket this:
//perform the integer division
    Q = abs(N)/abs(D);
    printf("\n The quotient is %d", Q);

    //perform the modulus
    R = abs(N)%abs(D);
    printf(("\n The remainder is %d", R);
    return 0;

Avatar of ozo
What if D == 0?
Hi missq,
                           First you declare N,D, R, &Q as integers and you have to assign some value to them. Then you check for just greater than zero (D or N > 0) nor for D or N >=0
O.K.
Hi missq,
                  why i told you to declare and assign because you're assigning these values to some other variables as shown in your code.
Avatar of missq

ASKER

thanks so much for your help....!!  you people are geniuses..!!