×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

Fast ceiling of an integer division in C / C++

Given two integer values, we have to find ceiling of an integer division. By Shubh Pachori Last updated : April 20, 2023

We know that the "/" operator gives us the value without floating point values but it returns the last quotient by which the dividend is completely divided.

Division operation using divide (/) operator in C and C++

C Code:

#include <stdio.h>

int main()
{
    int x, y, q;
    
    printf("Enter Dividend: ");
    scanf("%d", &x);

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

    q = x / y;
    printf("Quotient: %d", q);

    return 0;
}

Output:

Enter Dividend: 15
Enter Divisor: 7
Quotient: 2

C++ Code:

#include <iostream>
using namespace std;

int main()
{
    int x, y, q;
    
    cout << "Enter Dividend: ";
    cin >> x;

    cout << "Enter Divisor: ";
    cin >> y;

    q = x / y;
    cout << "Quotient: " << q;
    
    return 0;
}

Output:

Enter Dividend: 15
Enter Divisor: 7
Quotient: 2

Ceiling a quotient

To ceil a quotient, you may go for the basic approach of if condition with a divide command but we can clearly see that this code will take much more time in compilation and executing. This method is not efficient approach for this task.

C Code:

#include <stdio.h>

int main()
{
    int x, y, q;

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

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

    q = x / y;
    if (q * y < x)
        q++;
        
    printf("Quotient: %d", q);
    
    return 0;
}

Output:

Enter Dividend: 15
Enter Divisor: 2
Quotient: 8

C++ Code:

#include <iostream>
using namespace std;

int main()
{
    int x, y, q;

    cout << "Enter Dividend: ";
    cin >> x;

    cout << "Enter Divisor: ";
    cin >> y;

    q = x / y;
    if (q * y < x)
        q++;

    cout << "Quotient: " << q;
    
    return 0;
}

Output:

Enter Dividend: 15
Enter Divisor: 2
Quotient: 8

Fast ceiling of an integer division in C / C++

For fast ceiling of an integer division, Hence, the user must have to use the command q = 1 + ( ( x – 1 ) / y  ) ; to execute it even faster and there is no certainty of an overflow of data. It is only applicable for the positive integer.

C Code:

#include <stdio.h>

int main()
{
    int x, y, q;

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

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

    q = 1 + ((x - 1) / y);
    
    printf("Answer: %d", q);
    
    return 0;
}

Output:

Enter Dividend: 15
Enter Divisor: 2
Answer: 8

C++ Code:

#include <iostream>
using namespace std;

int main()
{
    int x, y, q;

    cout << "Enter Dividend: ";
    cin >> x;

    cout << "Enter Divisor: ";
    cin >> y;

    q = 1 + ((x - 1) / y);
    cout << "Quotient: " << q;
    
    return 0;
}

Output:

Enter Dividend: 15
Enter Divisor: 2
Quotient: 8



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.