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

C Ob From Functions

The document discusses algorithms for writing C programs that use recursive functions to find the factorial of a number, sum of natural numbers, and value of a number raised to a power by recursively calling functions until the base cases are reached and then printing the output. It provides pseudo code for the algorithms and examples of C programs implementing the recursive functions to calculate the described values.

Uploaded by

harinni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

C Ob From Functions

The document discusses algorithms for writing C programs that use recursive functions to find the factorial of a number, sum of natural numbers, and value of a number raised to a power by recursively calling functions until the base cases are reached and then printing the output. It provides pseudo code for the algorithms and examples of C programs implementing the recursive functions to calculate the described values.

Uploaded by

harinni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Dmhlemntahien ot

functions, ecursive funtions


) Funchions

Finding factoria! of a numbe

Aim
To i t e a C proqroam to find the facoriod

f a number usina funchons

Algorthm
Stpl START

Step 2 READ n

Step3 i fact 1
Stp4: FoR t : n
fact fat *i
Ct+
END FobR

PPINT fact
Steps
Step 6
Construck p m y declaning funchons
agument
th
* t h rebun tyPe
A h agumens
* t t h o u t xturn type

A t t h etum type hout


arguments

thout eturn type, Atthout arguments

Step 7 : STDP

Program
Aith eturn bype, kAth ag umunt

K3tdio As
#in clude

t fact ( int) i
void matn C)

unt n number: ");


Pnintf C" Enter a
scan f ( "d in);
pint (Factorial d, fact Cn))

tht fact Cint n)


t t fact I
for ( t: i<in; le+)
fact fact *t
uturn fact ;

aithout return type tth azumen*

# Lhclude Cstdio.h)
roid fact Cint);
votd mat'n l)

number: ");
pintf ("Enter
a

scanf C "Za% kn)i

fact Cn)
void fa ct ( t n)

t tact 1
ck:n t+)
forCc1
fact fact *;

prntt C" Factonal


u 7d i fa ct);
3

t h Album type athout argument


# nclude <stdio.AK»

t fact c);
void main()

printf C"Factonal d, fact C));


t fact C)

int tn fact I

prinf ("Enter a numb er: "


s can t ("%4 ",kn);
for Ct-, tcan; tt+)
fact fact *t
eburn fact;

thout return type , aithout ag uments

incude <s+dio.As
void fact C);
votd m ain C)

fact C),
3
void fact C)

int t, n, fact : I
Prtf C"Enter a number: ");
sCanfl "7J, *n)
for Ctl, ie en Ctt)

fact facf *i
3
prntf C"Factonial td " fa ct);
etun 0

Pesult
These þrogramd ae exe cuted and heir

ousputs a erhed
Outpuk
Enter a numbe7: 5

Factoriol u 120
2.
Check Palin drome or not

Aim.
TD Condtutt a c prog ram to check huthar the

given Y baund2ome or not nsl'ng funchons.

Agorthmn
Step START
Step 2 NITIALISE void palin d>om ( har sti])
Step 3 NITIALI SE Char str Lsv]

Step 4 READ &t7


Step S palundrom (sh)
Step 6 void palindrome (ehar str[1)
8tep 7 INITALisE C n , lag-0
Sep & len - strlen (str)

Sep FoR (Co; izen it+)


Step lo :
IF St [i3 S+r [len-t-7
tlog
BREAK
END IF

Step l END FoR


Step 12 : IF flaa :0
PRINT Palindome
Step 13 BLSE
PRINTNot palindzome"

Slep 14 : STDP
Pro m

#include (stdlo.As
## include String A>

vold palindrome (char strL7);


nt maint)

char str[o];
printA ("nPleaseenkr Atrng: "7;
jets (str)
palind ome (shr);

return 0

void palindrome ( thar str[)

i int t, en
iht flag-0;

lenA+rlen(str)
for Cto te len ; itt

f (str[i] != str [n i ] )

fl4
brea k

3
If Cflega o)
pintf ('In s u apalind vom stning dtr);

che
printtl"\ns not a palindrome stm'ng str);

Result Ths A ezecued and the oukput


prgram

venAed
Output

Please enter
stnna: abba
abba a
bau'nd rome lmng
3. oPy a string tthout strPy C)

Alm
Td Corstuct a c
program to Copy a
string
Ltthous strpy(

Algonthm
Sep1 START
Sep 2 DECLARE zstrcpy (char al[1, chara2[])
&tep3 n main
function
INITIALIsE al[203, a2 [zo
Step4 READ a1
Step S CALL 2strpy (a1, a2)
Sep 6 : PRINT a , a z

Step 7 END mai'n


funchion
Step 8 n rord zstrepy Ccheral[I, a2[ j)
INITIALISE
P1FoR (t0, al [r]!:
'o; C++)
a2[3 a1[i]
END FobR
Step to: a2[i]'\o'
Step STOP

Pogram:
c l u d e <stdio.As
void xstrcP y ( char al L], char a2[ 1);
ht main C)

char al [203, a2l20


printf("Enter siny: ");
gks Ca1);
strpy l alj aa) ;
pnntf ( "En lend string u Ze\no1)
pintf "Copie tnng u s\n, az);
llurn 0
3
void xstrCPy ( har a1[ ], char a2[ 1)

forCi:o; at[j!: '\o'j lt+)


a[ij: al[ij;
3
a2[ij.'\o';
3

Res ult TR erecud and the


This prg ram
Oudput verified.
Output
Enter stnng: frends
Entered stnna iu hi
fiends
Copi ed Ating u i
fsends
Sorhng 4tnngs.

Aim to ort a stnng


Tb Constu ct a program

descending
0rdr usins funchons
ascn ding o>

Algorthm
pecursive unchons

Factoral of a number

Aim
TD bo factonal
Construct a C poqram fnd the

oa number u4ing recurion.

Algonthm:
Stp START

Step 2 READ number (n


Stp Depne and call a funchon
Step 4 Repeat the funchon wnhl n u gcate thon

0r equal to zeo.

Stp5 PRINT factoriad


Stp6STDP

ram:
#uncludi Cstdio.AS
og t fact (long int);
nt man C)

De bn
number:7
printf ("Enter o
scant ("/1d , *n)
printf 'TK factorias KAd in Kld) n, fac# Cn)) ;
Aetun o
3
int n)
ht fact (long

i (ni?
Atturn n *foct (n-i7;
eue
turn I
3
Result Th proram
euha
and the ouhput
venAed.
Output:
a
numbe: 5
Ener

120
The factonal of
5
Jum ofh natual numbers.

Aim
D Constuct a
cproram find the s m of
n natura numbu us ing retursion .

Algorithm:
Step: START
Skep 2: READ a poSiive number

Step 3 DEFINE and CALL a tunchon.

Step 4REPEATk*e tunchon unhl n is net equal to zeio


Sep PRINT Sum
&tp6: STDP

Program
#ucud Cstdi'o. As
it Sum(int);
bt matn C )

int n
printf (" Enter ranz: ");
sCantC"7d , in) ;
rinlf C TRe sum of td numbers i dyn,SumCni);
Auturn O
3
i t &umCiht n)

ItCn! o)
t u r n n+ Sum(n-1)

che
xturn n
3

RAsut Th prog
pro5** ram
czucukd and the output
Asvenihed
Output:
Enter range: lo
Th Sum
of 1o numbers s S
valut of m
Fiding

Aim C program t fnd the raluu oP


TD Constutt a

musin9 recui'on .

Algonthm:

Step!: START

READ 8ase number (m)


Shep 2:
Shep 3 EAD exponent (n)
DEFINE and CALL a funchon
Stp 4
Shp5REPEAT h funchon wnhl n à not equal to O
Shep6 PRINT POHer

Stp 7 &TOP

Pogram
#in cude (st dio. A
t powey (ints int )

t manC)

int m,n
ninit ("Enter base number: "7;
stnf (" ",km),
prtniF ("Enter ezponent: ")

sCanf("A")an);
printf C Ta valu of d^ya in td ,m,n, poner (min);
Aeturn o

ht powe (int m, intn )

it (n!-o)rttum m'power (m,n - ) ;

elue
veun I
3

Result and ths Outpuf


Ths pro9ram is execuled

Venhed
Octput
number : 2
base
Ener
Enter exponent: lo

lo s lo24
lue of 2
TRe

You might also like