Binomial Series

Before I start I want to explain that I have searched the forums and haven't found my solution yet.

My teacher gave us a problem to solve he wants a program to solve binomials of the form (ax+b)^n where a,b,n are integers given by the user.

I have solved 90% of it but there remains a problem.
usually (a+b)^2 = a^2+2ab+b^2 and so on and so forth

my problem is that my program is giving a^2+ab+b^2 thus calculating wrong can someone help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  // Project 2a.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>


using namespace std;

unsigned long factorial(unsigned long);

int main()
{
	int r = 1;

	while (r == 1)
	{
		int n = 0;
		int a = 0;
		int b = 0;
		int k = 0;
		int y = 0;
		
		cout << "This is a program to solve (ax+b)^n given a,b,n \n" << endl;
		cout << "Give me a: \t";
		while (!(cin >> a))
		{
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			cout << "\nPlease type a number a:\t";
		}
		
		cout << "Give me b: \t";
		while (!(cin >> b))
		{
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			cout << "Error \n \nPlease type a number b:\t";
		}
		
		cout << "Give me n: \t";
		while (!(cin >> n))
		{
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			cout << "Error \n \nPlease type a number:\t";
		}

	
		
		n = abs(n);

		cout << "(";
		cout << a;
		cout << "x";
		cout << " + ";
		cout << b;
		cout << ")^";
		cout << n;
		cout << endl << endl;



		while (n >= k)
		{
		   
			y = y + (factorial(n) / (factorial(k)*factorial(n - k)))*((a ^ (n - k))*(b^k));
			
			cout << (pow(a, (n - k)))*pow(b, k);

			if ((n - k) > 0)
			{
				
				cout << "x";
				cout << "^";
				cout << n - k;
				cout << " + ";
			}
			k = k + 1;
			}
		cout << "\n \nTo repeat press 1, to exit press any other number:\t";
		cin >> r;
		system("CLS");
	}
	cout << endl << endl;
	return 0;
}

    
    unsigned long factorial(unsigned long number)
    {                             
       if (number <= 1)
         return 1;
	
       else
	     return number * factorial(number - 1);
	
		    }       

The problem is on line 70. b^k is not b raised to the k'th power, it's the exclusive-or of b and k. You want pow(b,k) instead.
Topic archived. No new replies allowed.