Solved

data entry error..!!

Posted on 1998-09-04
7
195 Views
Last Modified: 2010-05-19
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
Comment
Question by:missq
7 Comments
 
LVL 1

Accepted Solution

by:
newexpert earned 100 total points
ID: 1252507
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
 

Expert Comment

by:onki
ID: 1252508
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
 

Expert Comment

by:bertil
ID: 1252509
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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 84

Expert Comment

by:ozo
ID: 1252510
What if D == 0?
0
 

Expert Comment

by:manjucee
ID: 1252511
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
 

Expert Comment

by:manjucee
ID: 1252512
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
 

Author Comment

by:missq
ID: 1252513
thanks so much for your help....!!  you people are geniuses..!!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C: GetDiskFreeSpaceEx() for default drive 14 114
Why does module make constant click noise ? 9 135
Line meaning 9 87
Autosar OS Multicore Share Resources confusion ? 2 71
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question