Напишите функцию, которая возводит число a в степень b. Причем a и b вводятся с клавиатуры.
Решение:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <math.h>
#include «stdafx.h» #include <iostream> using namespace std; float degree_number(float number, float degree) { return (pow(number, degree)); } int main() {setlocale(LC_ALL,«Rus»); float a, b; cout<<» Введите число a >>»; cin>>a; cout<<» Введите степень в которую нужно возвести число а >>»; cin>>b; cout<<a<<» в степени «<<b<<» равно «<<degree_number(a, b); system(«pause>>void»); return 0; } |
Похожие записи:
#include
using namespace std;
using std::cout;
using std::cin;
using std::endl;
int main(){
int a, b, r=1;
setlocale(0, «»);
cout << "Введите число которое будете возводить в степень" a;
cout << "Введите степень" b;
for(int c = 0;c < b;c++){
r*=a; }
cout << "Ðåçóëüòàò: "<< r << endl;
system("PAUSE");
return 0;
}
В задании сказано «Напишите функцию …». А если опустить это, то ваш код можно так оптимизировать:
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
void main(){
int a, b, r=1;
setlocale(0, "");
cout << "Введите число которое будете возводить в степень ";
cin>>a;
cout << "Введите степень "; cin>>b;
for(int c=0; c<b; c++) r*=a;
cout << "Ответ: "<< r << endl;
system("PAUSE"); }
#include
#include
#include
using namespace std;
int main()
{
int n, a, b;
cin>>n>>b;
a = pow(n, b);
cout<<a;
return 0;
}
Молодец!
#include
#include ;
#include «windows.h»;
using namespace std;
int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
float x = 0;
int y = 0;
int rez = 1;
int a = 1;
cout <> x;
cout <> y;
if (y == 0)
{
x = 1;
}
if (y == 1)
{
x = x;
}
if (y < 0)
{
y = -1 * y;
while (a != y)
{
x *= x;
a += 1;
}
x = 1 / x;
}
else
{
while (a != y)
{
x *= x;
a += 1;
}
}
cout << x;
return 0;
}