Clrscr;ДАЮ 30 Б Составить программу на языке Паскаль,возведения числа х в степень числа y.
Ответы на вопрос
var
x, y, result:real;
begin
readln(x, y);
result := 1;
if y > 0 then
begin
while y <> 0 do
begin
result := result * x;
y := y - 1;
end;
end
else if y < 0 then
begin
while y <> 0 do
begin
result := result * x;
y := y + 1;
end;
result := 1 / result;
end
else result := 1;
writeln('x^y = ', result);
end.
In Pascal, you can create a program to calculate the exponentiation of a number x to the power of y. Here's a sample program:
pascalprogram Exponentiation; var x, y, result: real; function Power(base, exponent: real): real; var i: integer; begin result := 1; for i := 1 to trunc(exponent) do result := result * base; end; begin // Clear the screen (optional) ClrScr; // Input the values of x and y writeln('Enter the value of x: '); readln(x); writeln('Enter the value of y: '); readln(y); // Calculate the result using the Power function result := Power(x, y); // Display the result writeln(x, ' raised to the power of ', y, ' is: ', result); // Wait for user input before closing the program (optional) readln; end.
Explanation:
- The
Powerfunction calculates the exponentiation using a simple for loop. - The program asks the user to input the values of
xandy. - It then calls the
Powerfunction and stores the result in theresultvariable. - Finally, it displays the result of
xraised to the power ofy.
Please note that in Pascal, ^ is the exponentiation operator, so you could use x := x^y; instead of defining a separate Power function. However, I've used a custom function to demonstrate the concept of exponentiation.
Похожие вопросы
Топ вопросов за вчера в категории Информатика
Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili
