0

Given n, the program should calculate 1^1 + 2^2 + 3^3 + ... till n-1^n-1. Below is my code, in which there is one function inside while loop which and the passed value is from n-1 in the function. The function definition has two variables which return the ans. Output is wrong always 1.

#include <stdio.h>
#include <stdlib.h>

int power(int x, int y)
{
    int la, ans;
    if(y==0)
        return 1;
    else
        la= (x*power(x, y-1));  
        ans+=la;
        return ans;
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, m, a, b, res, res1;
        scanf("%d%d", &n, &m);
        while(n-- && n>0)
        {
            a = power(n-1, n-1);
        }
        printf("%d", a);
    }
    return 0;
}

2 Answers2

1

Some problems in your code.

  1. As pointed in another answer, your power function was broken:

    • ans was not initialized
    • { } were missing after the else
  2. in the while, you compute x^x, but you forget the result, whearas you should sum it.

  3. first thing you do in while loop is to decrease n and to compute power(n-1, n-1) that sound not logical.

Hence, your corrected code could be:

#include <stdio.h>
#include <stdlib.h>

int power(int x, int y)
{
    if(y==0)
        return 1;
    else
        return x*power(x, y-1);  
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, m, b, a = 0;
        scanf("%d%d", &n, &m);
        while(n>1)
        {
            --n;
            b = power(n, n);
            a += b;
            printf("%d^%d -> %3d\n",n, n, b);                
        }
        printf("sum= %d", a);
    }
    return 0;
}

Gives for n = 6:

5^5 -> 3125
4^4 -> 256
3^3 ->  27
2^2 ->   4
1^1 ->   1
sum=3413
Mathieu
  • 8,840
  • 7
  • 32
  • 45
  • What does that mean `ans` was not initialized – hemant vikas patnaik Sep 27 '18 at 10:18
  • When you type "`int ans;`", you create a *variable* which *initial* value can be anything, if you type "`int ans = 0;`", you create a *variable* with *inital* value set to `0`. – Mathieu Sep 27 '18 at 10:35
  • When you write `ans += 1;`, if the initial value of `ans` is anything, the result will be anything too. See this [question](https://stackoverflow.com/q/1597405/1212012) – Mathieu Sep 27 '18 at 10:45
0

C uses braces to form blocks, your power() function looks like it's wanting to use indentation like in Python.

It should probably be:

int power(int x, int y)
{
    int la, ans;
    if(y==0)
        return 1;
    else
    {
        la= (x*power(x, y-1));  
        ans+=la;
        return ans;
    }
}

Of course since the first if has a return, the else is pointless, and you can simplify the code:

int power(int x, int y)
{
    if (y==0)
        return 1;
    return x * power(x, y-1);
}

The variable ans was never assigned to, that looked broken so I simplified it out.

Of course this is susceptible to integer overflow.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    but that won't calculate the sum of 1^1 + 2^2 + 3^3 + ... till n-1^n-1, I want output as a complete sum of **1^1 + 2^2 + 3^3 + ... till n-1^n-1** – hemant vikas patnaik Sep 27 '18 at 09:13
  • @hemantvikaspatnaik No, my understanding was that `power()` should just compute integer exponentiation, and then you should have a loop elsewhere to do the summing. – unwind Sep 27 '18 at 09:21