Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
modexpo/modexpo.c
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
55 lines (40 sloc)
895 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
long int recursive_mod(int b, int e, int m) | |
{ | |
//Fill in your code below | |
if (e == 1){ | |
return b%m; | |
} | |
else{ | |
return b *(recursive_mod(b, e-1, m)) %m; | |
} | |
} | |
long int loop_mod(long int b, long int e, long int m) | |
{ | |
//Fill in your code below | |
long int modanswer = 1; | |
for (int i = 1; i < e+1; i++) | |
{ | |
if (e == 1) { | |
modanswer = (b&m); | |
} | |
else { | |
modanswer = (b * modanswer) % m; | |
} | |
} | |
return modanswer; | |
} | |
//Do not change the code below | |
int main(void) | |
{ | |
int b,e,m; | |
printf("Enter b:"); | |
scanf("%d",&b); | |
printf("Enter e:"); | |
scanf("%d",&e); | |
printf("Enter m:"); | |
scanf("%d",&m); | |
printf("Result using loop:%ld\n",recursive_mod(b,e,m)); | |
printf("Result using recursion:%ld\n",loop_mod(b,e,m)); | |
return 0; | |
} |