Skip to content
Permalink
ef528edc11
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
55 lines (40 sloc) 895 Bytes
#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;
}