Hello

Would somebody help me with this exercice?
Write a program that calculates bth power of a modulus k. For example, if you are asked to calculate 2^6 mod 7; 6th power of 2 is 64 thus 64 modulus 7 is 1.

Input specification
You will be given 3 integers, a, b, and k where b represents the power and k represents the modulus operand and 0 ≤ b < (a and k) ≤ 1000.

Output specification
Show just one integer number which is between 0 and k-1.
So, what is your problem? We won't do your homework; We can help you, but you need to show that you tried to solve your problem.

Start writing this program, and if you have problem/error - then ask.
I know that you don't do homeworks.I wrote this code to solve this exercice.So I need some help to find the mistake..

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
int m,n,k,result,remainder;
cin >> n >> m >> k;
result=1;
int cnt=1;
while (cnt<=m)
{
cnt=cnt+1;
result=result*n;
remainder=result%k;
}
printf ("%d",remainder);
return 0;
}
You could do it easily if you wrote pow(or power) function

1
2
3
4
5
//Pseudocode
declare a,b,k,result
get a, b, k
result is a^b
show result % k


Probably your while loop is not working as intended. Using function would result in clearer code.

Topic archived. No new replies allowed.