0% found this document useful (0 votes)
43 views

#Include Int Int: "Enter A Number: " "%D"

Here is a program that evaluates x raised to the power y using a recursive power function: Program Power; Function Power(base, exponent: Integer): Integer; Begin If exponent = 0 then Power := 1 Else Power := base * Power(base, exponent-1); End; Var base, exponent, result: Integer; Begin Write('Enter the base: '); Readln(base); Write('Enter the exponent: '); Readln(exponent); result := Power(base, exponent); Writeln(base, ' raised to the power ', exponent, ' is ', result); Readln; End.

Uploaded by

Nelson Nykam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

#Include Int Int: "Enter A Number: " "%D"

Here is a program that evaluates x raised to the power y using a recursive power function: Program Power; Function Power(base, exponent: Integer): Integer; Begin If exponent = 0 then Power := 1 Else Power := base * Power(base, exponent-1); End; Var base, exponent, result: Integer; Begin Write('Enter the base: '); Readln(base); Write('Enter the exponent: '); Readln(exponent); result := Power(base, exponent); Writeln(base, ' raised to the power ', exponent, ' is ', result); Readln; End.

Uploaded by

Nelson Nykam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C Version Pascal Version

#include<stdio.h> Program inputOutput;


int main(){ Uses crt;
  int i=1,f=1,num; Var i, f, num: integer;
  printf("Enter a number: "); Begin
  scanf("%d",&num);
Clrscr;
  while(i<=num){ i:=1;
      f=f*i; f:=1;
      i++; Writeln(‘Enter a Number’);
  } Readln(num);
printf("Factorial of %d is: While i<=num do
%d",num,f); Begin
  return 0; f:=f+i;
} i:=i+1;
end;
Writeln(‘The factorial of ‘,num, ‘is ‘,num);
Readln;
End.

Multiplication Table

C version Pascal Version


#include <stdio.h> Program inputOutput;
int main() Uses crt;
{
int n, i; Var i, n, p: integer;
printf("Enter an integer: "); Begin
scanf("%d",&n); Clrscr;
Writeln(‘Enter an Integer’);
for(i=1; i<=12; ++i) Readln(n);
{
For i:= to 12 do
printf("%d * %d = %d \n", n,
i, n*i); Begin
} p:=n*i;
return 0; Writeln(n,’*’,i,’=’,p);
} end;
Readln;
End.
Pascal Version C Version
program power; #include <stdio.h>
uses crt; int power(int base, int exp)
var x,n,i,y:integer; {
begin int pow;
clrscr; }
writeln("ENTER THE BASE NUNBER"); int main(){
read(x); int base, exp;
writeln("ENTER THE POWER NUMBER"); int answer;
read(n); printf("Welcome to power program! ");
y:=x; printf("Please use integers only.\n");
for i:=1 to (n-1) do //get base
y:=y*x; printf("Enter the base you will like raised to a
writeln(x," TO THE POWER OF ",n," IS ",y); power: ");
Readln; scanf("%d",&base);
end. //get exponent
printf("Enter the power you will like it raised to:
");
scanf("%d",&exp);
answer = power(base, exp);
printf("%d raised to the %d is %d\n", base, exp,
answer);
system("pause");
return 0;
}

Defn (Recursion): Recursion is a programming method in which a routine calls itself.

It is a method of defining a situation such that the very method is used in understanding the definition until an
acceptable level of understanding is attained. (Ikome, 2011)
Recursion is defined by
a) a base case or known case and
b) a general case which reduces to the base case.
Recursive functions: This is an algorithm that represents a recursion to return a value after a process.
The syntax of a Pascal function is as follows

Function identifier(parameter: type): type;

Begin

Statements.

End;

Examples of recursive functions


1.) Factorial
Definition
0!=1!=1, 2!=1x2, 3!=3x2x1
Gen. n!=nx(n-1)!
Function factorial(n): integer;
begin

If n = 0 then factorial=1

else factorial=n * factorial(n-1);

End;

2.) Fibonacci Numbers


Fibbo seq: 1, 1, 2, 3, 5, 8, 13,…

F(1)=1, F(2)=1 (Base case)

F(n)=F(n-1) + F(n-2) (Gen case)

Function fibo(x: integer)r;

begin

if x=1 then fibo:=1

else if x=2 then fibo:=1

else fibo:=fibo(x-1)+fibo(x-2);

end;

end;
Exercise
Define a recursive function that evaluates the sum of the first n integers. E.g. Sum(4)=1+2+3+4
Or Sum(4)=10.
Corretion
Function sum(x);

Begin

if n is equal to 0, then sum=0;

else, sum=x + sum(x - 1))

Program factorial;

var x: integer;

Function fact(n: integer):integer;

begin

if n=0 then fact:=1

else fact:=n*fact(n-1);
end;

begin

write('The factorial of: ');

readln(x);

writeln('The factorial of ',x,' is ',fact(x));

readln;

end..

Program fibbo;

var i, n: integer;

function fibo(x: integer):integer;

begin

if <=2 then fibo:=1

else fibo:=fibo(x-1)+fibo(x-2);

end;

begin

writeln('Give an integer');

readln(n);
writeln('The ',n,'th',' number in the

fibonacci sequence is ',fibo(n));

writeln;

writeln('The fibo sequence is');

i:=1;

repeat

write(' ',fibo(i):2);

i:=i+1;

until i=n+1; readln;


end.

1. write a program that evaluates the value of x raised to the power y i.e. (x y) where x and y are integers.
Use a power function in your program code.

You might also like