this program is trying to add from 1.. to x with return function.
but when I enter 10 sum is 54 which is wrong. 1,2,3,4,5,6,7,8,9,10 = 55

#include <cstdlib>
#include <iostream>

int burcin(int);

using namespace std;

int main(int argc, char *argv[])
{
    int x;
    int ft=0;
    cout << "enter the number=\n";
    cin >> x;
    ft=+burcin(x);
    cout << "from 1"<<"to"<<x<<"\ntotal value="<<ft<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

int burcin(int x)
{
     if (x==1)  return 0; 
     else
     return (x+ burcin(x-1));
     
}

Recommended Answers

All 4 Replies

Try changing your function to:

int burcin(int x)
{
     if (x==1)  return 1; 
     else
     return (x+ burcin(x-1));
     
}

you could cheat and return

return ((pow(x,2)+x)/2);

maybe also if theres don't work try declaring a global variable and set it equal to (x+ burcin(x-1))

then just return the variable

thanks the program calculates correct
i dont understand why return 1

Be a part of the DaniWeb community

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