Solved

data entry error..!!

Posted on 1998-09-04
7
196 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
Independent Software Vendors: 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!

 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rhel6 C system() call to zip to archive has problems 25 248
Console based application in Linux 1 99
convert char array to number in c 5 89
An API detour question 7 101
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

680 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