Monday, April 28, 2025

C Program - Find Quotient and Remainder - Modulo Operator

This program prompts the user to enter a dividend and a divisor, then performs integer division on them and displays the quotient and remainder of the division operation.

The program uses the % (modulus) operator to calculate the remainder and the / (division) operator to calculate the quotient. It then displays the result using printf().

Note that this program assumes that the user enters valid integer values for the dividend and divisor, and does not perform any input validation or error checking.

In arithmetic, the modulo operation finds the remainder after division of one number by another. Given two positive integers, a (the dividend) and n (the divisor), the modulo operation (denoted by the symbol %) returns the integer remainder of the division a/n.

For example, the expression 13 % 5 would evaluate to 3, because when 13 is divided by 5, the remainder is 3. Similarly, 24 % 7 would evaluate to 3, because 24 divided by 7 is 3 with a remainder of 3.

#include <stdio.h>

int main()
{

    int dividend_num, divisor_num, quotient, remainder;

    printf("Enter Dividend: ");
    scanf("%d", &dividend_num);

    printf("Enter Divisor: ");
    scanf("%d", &divisor_num);

    quotient = dividend_num / divisor_num;

    remainder = dividend_num % divisor_num;

    printf("----------------------- \n");
    printf("Quotient: %d  \n", quotient);
    printf("Remainder: %d \n", remainder);
    printf("----------------------- \n");

    return 0;
}

Result: 

Enter Dividend: 10
Enter Divisor: 3
-----------------------
Quotient: 3
Remainder: 1
-----------------------

Process returned 0 (0x0)   execution time : 7.556 s
Press any key to continue.

Here's an explanation of each block of code in the program: 

#include <stdio.h>

This line includes the standard input-output library header file.

int main()
{

This line declares the main function that runs when the program is executed. The int keyword specifies that the function returns an integer value (0 in this case). 

int dividend_num, divisor_num, quotient, remainder;

This block of code declares four integer variables:

  • dividend_num: the number that will be divided by the divisor_num
  • divisor_num: the number that dividend_num will be divided by
  • quotient: the result of the division operation, which is the whole number part of the answer
  • remainder: the result of the division operation, which is the remainder part of the answer 
printf("Enter Dividend: ");
scanf("%d", &dividend_num);

printf("Enter Divisor: ");
scanf("%d", &divisor_num);

These lines prompt the user to enter the dividend_num and divisor_num values and read them into the corresponding integer variables using scanf()

quotient = dividend_num / divisor_num;

remainder = dividend_num % divisor_num;

These lines perform the division operation on dividend_num and divisor_num and store the results in quotient and remainder respectively. The / operator is used for the division operation, which returns the whole number part of the answer, while the % operator is used for the modulus operation, which returns the remainder part of the answer. 

printf("----------------------- \n");
printf("Quotient: %d  \n", quotient);
printf("Remainder: %d \n", remainder);
printf("----------------------- \n");

These lines use printf() to display the quotient and remainder values, along with some additional formatting. The ----------------------- string is used to separate the output for visual clarity. 

return 0;

This line returns an integer value of 0 to the operating system to indicate that the program executed successfully.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...