Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

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;
}
0
missq
Asked:
missq
1 Solution
 
newexpertCommented:
What is N, D, Q, R?  If they are variable you should define them as
int N, D, Q, R;
You can always check whether N, D are positive or not before you do the division.
if (N >= 0 && D > 0)
   {
    Q = N/D; R = N%D; /* ... */
   }
else printf("Error, should be +ve");

You can also define
unsigned int N, D, Q, R;
and use %u in the printf statement.  But that's not recommended.
0
 
onkiCommented:
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 */
0
 
bertilCommented:
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;

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ozoCommented:
What if D == 0?
0
 
manjuceeCommented:
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.
0
 
manjuceeCommented:
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.
0
 
missqAuthor Commented:
thanks so much for your help....!!  you people are geniuses..!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now