Sir, here is the qustion:-

Nestor was doing the work of his math class about three days but he is tired of make operations a lot and he should deliver his task tomorrow. His math’s teacher gives two numbers a and b. The problem consist in find the last digit of the potency of base a and index b. Help Nestor with his problem. You are given two integer numbers: the base a (0 <= a <= 20) and the index b (0 <= b <= 2,147,483,000). You have to find the last digit of a^b.

Input
The first line of input contains an integer t, the number of test cases (t <= 30). t test cases follow. For each test case will appear a and b separated by space.

Output
For each test case output an integer per line representing the result.

Time limit: 1s
Source limit: 700B

Here is my code sir.

#include<stdio.h>
#include<math.h>
int fun(int,int);
int main()
{
int a,mod,nb,t,i;
unsigned long b;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d %lu",&a,&b);
if(a!=0 || b!=0)
{
mod=a%10;
if(mod==2 || mod==3 || mod==7 ||mod==8)
{
nb=b%4;
if(nb==0) nb=4;
if(b==0) nb=0;
fun(mod,nb);
}
else if(mod==4 ||  mod==9)
{
nb=b%2;
if(nb==0) nb=2;
if(b==0) nb=0;
fun(mod,nb);
}
else if(mod==0 || mod==1 || mod==5 || mod==6)
{
if(b==0) nb=0;
else nb=1;
fun(mod,nb);
}
}
}
return 0;
}
int fun(int mod,int nb)
{
int p,ld;
p=pow(mod,nb);
ld=p%10;
printf("%d\n",ld);
}

There is some error, due to which I am getting wrong answer. I have debugged the code lot of times and unable to find the reason of getting wrong answer.

## All 8 Replies

There is some error, due to which I am getting wrong answer.

1) If "there is some error" then you have a mistake. We are not psychic. What's the error?

No sir ,there is not some any type of syntax error or runtime error or compilation error..
I have cheaked and matched the answer from calculation, it is running smoothly , but there is some thing wrong due to which I am getting wrong answer which I dont know..

Hello cse.avinash,
There is no return statement on the function fun.

I have included return 0 in the function fun, its again showing wrong answer

here is improved code:--

#include<stdio.h>
#include<math.h>
int fun(int,int);
int main()
{
int a,mod,nb,t,i;
unsigned long b;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d %lu",&a,&b);
if(a!=0 || b!=0)
{
mod=a%10;
if(mod==2 || mod==3 || mod==7 ||mod==8)
{
nb=b%4;
if(nb==0) nb=4;
if(b==0) nb=0;
fun(mod,nb);
}
else if(mod==4 ||  mod==9)
{
nb=b%2;
if(nb==0) nb=2;
if(b==0) nb=0;
fun(mod,nb);
}
else if(mod==0 || mod==1 || mod==5 || mod==6)
{
if(b==0) nb=0;
else nb=1;
fun(mod,nb);
}
}
}
return 0;
}
int fun(int mod,int nb)
{
int p,ld;
p=pow(mod,nb);
ld=p%10;
printf("%d\n",ld);
return 0;
}

Still can't read you code. Again format it. It's not hard.

I hope u want to find the last digit of an integer.. rite? If so this will give u the last digit of the given number.

int main ()
{
int p=0;
...
...

p = pow(a, b);
printf ("Last Digit : %d\n", p - 10 * (p / 10));

return 0;
}

printf ("Last Digit : %d\n", p - 10 * (p / 10));

Why so complex? printf ("Last Digit : %d\n", p % 10);

You are given two integer numbers: the base a (0 <= a <= 20) and the index b (0 <= b <= 2,147,483,000).

...
if(a!=0 || b!=0)
{
...

What will your program do when both a AND b are zero?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.