code please

Please give the c++ code for following problems.

1) Write a C++ program to calculate x raised to the y power. The program should have a while repetition control structure.solved

2) Print the integers form 1 to 20 using a while loop and the counter variable x. Assume that the variable x has been declared, but not initialized. Print only 5 integers per line. Hint: Use the calculation x%5. When the value of this is 0, print a newline character, otherwise print a tab character.

Last edited on
I'll help you write that if you'd like but I'm not doing your assignment for you because that won't help you at all. Make a stab at the code, post it here and we'll dutifully give you help but you get nowhere in life just asking for it to be done for you.
Just because I'm an ass. Here is your code:
1
2
3
4
5
6
7
8
9
10
11
12
// Number 1
#include <iostream>
#include <cmath>

int main() {
   int x, y;
   std::cout << "Enter X: "; std::cin >> x;
   std::cout << "Enter Y: "; std::cin >> y;
   while (true)
      std::cout << "X^Y = " << pow(x,y) << "\n";
   return 0;
}


1
2
3
4
5
6
7
8
// Number 2
#include <iostream>

int main() {
   while (true)
      std::cout << "12345\n678910\n1112131415\n1617181920\n";
   return 0;
}
@ volatile tx, but your both program gives continuous loop...

finally I got the answer which work perfectly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
	int x, y, i, power;

	i = 1;
	power = 1;

	cout << "Enter x: ";
	cin >> x;
	cout << "Enter y: ";
	cin >> y;

	while ( i <= y ) {             // to multiply the x itself to the no. of y
		power = power * x;
		i++;
	}

	cout << "x to the power y is: " << power << endl;
	return 0;
}


looking for 2nd answer.
Oh fine I'll give you the second answer because I'm a loverly person :P

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
    int x = 0;
    while(x <= 20)
    {
        if(x % 5 == 0 && x != 0)
        {
            cout << " " << x << endl;
        }
        else if((x % 5 == 1 || x == 0) && x != 1)
        {
            cout << x;
        }
        else
        {
            cout << " " << x;
        }
        x++;
    }
}
tx master, here I got the simple code..

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

using namespace std;

int main()
{
	int x;
	x = 1;

	while ( x <= 20 ) {
		cout << x;

		if ( x % 5 == 0 )
			cout << "\n";
		else
			cout << "\t";
		
		x++;
	}
	
	return 0;
}
Topic archived. No new replies allowed.