easy but confusing!!

hey all :D
I Would like first to thank all the people on this forum for the great help!!

can anyone help me with this problem??

Write a C++ program that prints the squares of all the numbers from 1 to a number n entered by the user. A sample run can be as the following:
Enter a number: 3
1 1
2 4
3 9

here what I did

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cmath>
using namespace std;

int pow (int x,int y)
{
	return x ;
}
 void main ()
 {
	 int number;
	 cout<<"please enter a number to square \n";
	 cin>>number;
	 cout<< "the answer is \n";
	 for (int i=1;i<=number;i++)
	 {
		 
		 cout<<i<<" "<<pow(i,2)<<endl;
	 }
	 system ("pause");
 }


but the result is like:
1 1
2 2
3 3

the power isnt working i guess,
can anyone help??! :D
1
2
3
4
int pow (int x,int y)
{
	return x ;
}


Can you explain how the above is supposed to raise x to the power of y?
closed account (18hRX9L8)
1
2
3
4
5
6
7
8
9
10
11
12
13
int pow (int x,int y)
{
     int counter,point_to_x;

     point_to_x=x;

     for(counter=1;counter<y;counter++)
    {
          x = x * point_to_x;
    }
    
    return(x);
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdlib>
using namespace std;

 int main ()
 {
	 int number;

	 cout<<"please enter a number to square: ";

	 cin>>number;

	 cout<< "\nthe answer is \n";
	 for (int i=1;i<=number;i++)
	 {
		 
		 cout << i << " " << i * i <<endl;
	 }

	 system ("pause");
 }
Last edited on
thank you all for the great help, it worked!!
I was super stupid lol xD
closed account (18hRX9L8)
Don't use system("PAUSE").
Use cin.ignore(); or cin.get();
Topic archived. No new replies allowed.