Solved

data entry error..!!

Posted on 1998-09-04
7
190 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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now