Signup/Sign In

C Porgram of Division of two numbers entered by the user

learn C language tutorial

In C language, to read the inputs we use the scanf() function, and then to print the result we use the printf() function. The %d used in scanf() and printf() functions is the format specifier that is used for int datatype in C.

In this example, the user will provide two numbers and our task is to perform the division operation and find the quotient and remainder, and round it up to two decimal places.

Division of two numbers entered by the user

Input: Enterthe numbers: 25 5

Output:

Quotient=5.00

Remainder= 0.00

This problem can be solved in the following ways:

Method 1: Display the quotient and remainder directly using another variable

Method 2: Display the quotient and remainder by calling a single function with a void return type

Method 3: Display the quotient and remainder by calling two different functions with void return type

Method 4: Display the quotient and remainder directly without using another variable

Method 5: Display the quotient and remainder by calling a function without using another variable.

Let us look at each of these methods separately.

Program 1: Division of Two Numbers entered by the User

In this method, two new variables are used to store the quotient and the remainder after performing the division operation.

Algorithm:

  1. Start
  2. Declare two variables.
  3. Initialize the two variables.
  4. Use two other variables to store the quotient and the remainder after the division operation.
  5. Print the result using the format specifier.
  6. Stop.

Below is the code for the same

Here, the user is asked to enter two numbers. Then, these two numbers are divided and the quotient and the remainder is stored in two different variables and the resultant is displayed. All of these tasks are done in the main method itself.

#include <stdio.h>

int main() 
{
    int x, y;         //Declare the dividend and divisor
    float quotient;
    int remainder;
    printf("Enter dividend: ");
    scanf("%d", &x);     //Initialize the dividend
    printf("Enter divisor: ");
    scanf("%d", &y);     //Initialize the divisor

    // Computes quotient
    if(y!=0)
    {
       quotient = (float)x / (float)y;
       printf("Quotient = %.2f\n", quotient);   //print the quotient
    }
    else
    {
        printf("Division not possible.\n");
    }
    // Computes remainder
    remainder = x % y;

    printf("Remainder = %.2d", remainder);  //print the remainder
    
    return 0;
}


Enter dividend: 65
Enter divisor: 2
Quotient = 32.50
Remainder = 01

Program 2: Division of Two Numbers entered by the User

In this method, we will perform the division operation in another function by using two different variable. These variables will store the quotient and the remainder and display it.

Algorithm:

  1. Start
  2. Declare two variables.
  3. Initialize the two variables.
  4. Call a function to perform the division operation.
  5. Use two other variable that will store the result of these two numbers.
  6. Print the result using the format specifier.
  7. Stop.

Below is the code for the same

Here, the user is asked to enter two numbers. Then, a function is called and the numbers are passed as arguments. Other two variables are declared in that function which stores the quotient and the remainder after performing the division operation. Finally, the result is displayed in that function.

#include <stdio.h>
void divide(int x, int y);    //Function Declaration
/*Create a user defined divide function that will perform the division operation*/
int main() 
{
    int x, y;         //Declare the dividend and divisor
    printf("Enter dividend: ");
    scanf("%d", &x);     //Initialize the dividend
    printf("Enter divisor: ");
    scanf("%d", &y);     //Initialize the divisor
    divide(x,y);         //Function Call
    
    return 0;
}
void divide(int x, int y)     //Function Definition
{
    float quotient;
    int remainder;
    // Computes quotient
    if(y!=0)
    {
       quotient = (float)x / (float)y;
       printf("Quotient = %.2f\n", quotient);   //print the quotient
    }
    else
    {
        printf("Division not possible.\n");
    }
    // Computes remainder
    remainder = x % y;

    printf("Remainder = %.2d", remainder);  //print the remainder
    
}


Enter dividend: 45
Enter divisor: 2
Quotient = 22.50
Remainder = 01

Program 3: Division of Two Numbers entered by the User

In this method, we will find the quotient and the remainder after performing the division operation in two different functions by using two different variable. These variables will store the quotient and the remainder and display it.

Algorithm:

  1. Start
  2. Declare two variables.
  3. Initialize the two variables.
  4. Call two functions to perform the division operation. The first function will find the quotient and the second function will find the remainder.
  5. Use one variable in each of the function that will store the result of these two numbers.
  6. Print the result using the format specifier.
  7. Stop.

Below is the code for the same

Here, the user is asked to enter two numbers. Then, two functions are called and the numbers are passed as arguments. One variable in each function is declared to store the quotient and the remainder after performing the division operation. Finally, the result is displayed in that function.

#include <stdio.h>
void findQuotient(int x, int y);    //Function Declaration for Quotient
/*Create a user defined divide function that will perform the division operation and find the quotient*/

void findRemainder(int x, int y);  //Function Declaration for remainder
/*Create a user defined divide function that will perform the division operation and find the remainder*/

int main() 
{
    int x, y;         //Declare the dividend and divisor
    printf("Enter dividend: ");
    scanf("%d", &x);     //Initialize the dividend
    printf("Enter divisor: ");
    scanf("%d", &y);     //Initialize the divisor
    
    if(y!=0)
    {
        findQuotient(x,y);      //Function Call
        findRemainder(x,y);     //Function Call
    }
    else
    {
        printf("Division not possible.\n");
    }
    return 0;
}
void findQuotient(int x, int y)     //Function Definition
{
       // Computes quotient  
       float quotient;
       quotient = (float)x / (float)y;
       printf("Quotient = %.2f\n", quotient);   //print the quotient
    
}
void findRemainder(int x, int y)     //Function Definition
{
    // Computes remainder
    int remainder;
    remainder = x % y;
    printf("Remainder = %.2d", remainder);  //print the remainder
    
}


Enter dividend: 295
Enter divisor: 6
Quotient = 49.17
Remainder = 01

Program 4: Division of Two Numbers entered by the User

In this method, the quotient and remainder are calculated directly without using any other variable.

Algorithm:

  1. Start
  2. Declare two variables.
  3. Initialize the two variables.
  4. Without using any other variable directly calculate the quotient and the remainder after performing the division operation.
  5. Print the result using the format specifier.
  6. Stop.

Below is the code for the same

Here, the user is asked to enter two numbers. Then, the quotient and the remainder of these two numbers is displayed. All of these tasks are done in the main method itself.

#include <stdio.h>

int main() 
{
    int x, y;         //Declare the dividend and divisor
    printf("Enter dividend: ");
    scanf("%d", &x);     //Initialize the dividend
    printf("Enter divisor: ");
    scanf("%d", &y);     //Initialize the divisor
    
    if(y!=0)
    {
        printf("Quotient = %.2f\n", (float)x / (float)y);   //print the quotient
         printf("Remainder = %.2d",x % y );  //print the remainder
    }
    else
    {
        printf("Division not possible.\n");
    }
    return 0;
}


Enter dividend: 84
Enter divisor: 3
Quotient = 28.00
Remainder = 00

Program 5: Division of Two Numbers entered by the User

In this method, we will perform the division operation in another method without using any third variable. Here, the quotient and the remainder are calculated in their respective functions and then the values are displayed.

Algorithm:

  1. Start
  2. Declare two variables.
  3. Initialize the two variables.
  4. Call a function to perform the division operation.
  5. Without using any other variable directly calculate the quotient and the remainder.
  6. Print the result using the format specifier.
  7. Stop.

Below is the code for the same

Here, the user is asked to enter two numbers. Then, a function is called and the numbers are passed as arguments. In that function, the division operation of the two numbers is performed directly and the values are displayed.

#include <stdio.h>
void findQuotient(int x, int y);    //Function Declaration for Quotient
/*Create a user defined divide function that will perform the division operation and find the quotient*/

void findRemainder(int x, int y);  //Function Declaration for remainder
/*Create a user defined divide function that will perform the division operation and find the remainder*/

int main() 
{
    int x, y;         //Declare the dividend and divisor
    printf("Enter dividend: ");
    scanf("%d", &x);     //Initialize the dividend
    printf("Enter divisor: ");
    scanf("%d", &y);     //Initialize the divisor
    
    if(y!=0)
    {
        findQuotient(x,y);      //Function Call
        findRemainder(x,y);     //Function Call
    }
    else
    {
        printf("Division not possible.\n");
    }
    return 0;
}
void findQuotient(int x, int y)     //Function Definition
{
       //Computes Quotient
       printf("Quotient = %.2f\n", (float)x / (float)y);   //print the quotient
}
void findRemainder(int x, int y)     //Function Definition
{
    // Computes remainder
    printf("Remainder = %.2d",x % y );  //print the remainder 
}


Enter dividend: 25
Enter divisor: 5
Quotient = 5.00
Remainder = 00



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight