C program for modular division of large number





1. C code for modular division of big numbers:

2. How to get modular division of two very large numbers larger or beyond than long int in c programming language

#include<stdio.h>
#include<string.h>
#define MAX 10000

int validate(char []);
int modulerDivision(char[],unsigned long);


int main(){

    char dividend[MAX];
    unsigned long int divisor,remainder;

    printf("Enter dividend: ");
    scanf("%s",dividend);
    if(validate(dividend))
         return 0;

    printf("Enter divisor: ");
    scanf("%lu",&divisor);

    remainder = modulerDivision(dividend,divisor);

    printf("Modular division: %s %% %lu  =  %lu",dividend,divisor,remainder);

    return 0;
}

int validate(char num[]){
    int i=0;

    while(num[i]){
         if(num[i] < 48 || num[i]> 57){
             printf("Invalid positive integer: %s",num);
             return 1;
         }
         i++;
    }

    return 0;
}

int modulerDivision(char dividend[],unsigned long divisor){
   
    unsigned long temp=0;
    int i=0;

    while(dividend[i]){
        
         temp = temp*10 + (dividend[i] -48);
         if(temp>=divisor){
             temp = temp % divisor;
         }
   
         i++;
    }

    return temp;
}

Sample output:
Enter dividend: 123456789
Enter divisor: 56
Modular division: 123456789 % 56 = 29


Algorithm to find the modular division of large numbers:



Use array to store big numbers. 







2 comments:

zarf ben said...

Hi, I need help for my assignment, I need to modular addition and modular multiplication on large integers ie 128 using arrays. My numbers are stored in char arrays in decimal form, so how to perform modular addition and multiplication on large char arrays. please

Lemza said...

Hi! I request for help in writing. Program with functions that relate to bank transactions. Prompt user for a password.if the password is wrong, program must give an error message and ask him/ her to try again. Allow three trials, on the fourth time quit the program. Recall the use of strcmp() and strcpy() from the string.h in dealing with strings. The program must display the user's initial balance as R10000 and give the user a menu of depositing and withdrawal., have two functions, one deposit into your account and another to withdraw money from the account. Each function at the end displays the new balance in your account. Give the user an option to quit or continue with the program. If the user continues the program must go back to the menu with no password required. If the user quits, the program must end. No global varibles!