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

Exercise Solutions of Java Functions

The document contains sample solutions to exercises on user defined functions in C/C++. It includes function signatures, return types, formal parameters, call by reference vs value, and examples of computing LCM, factorials, and digital root.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Exercise Solutions of Java Functions

The document contains sample solutions to exercises on user defined functions in C/C++. It includes function signatures, return types, formal parameters, call by reference vs value, and examples of computing LCM, factorials, and digital root.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Solutions to Exercise on User Defined Functions

Answer #1.
i) Function Signature
ii) Functions not returning any value also have a return type which void. This type of function usually
handles the processing but does not return any value.
iii) Formal Parameter
iv) call by reference
v) call by value

Answer #2
QUESTION REASON TO BE INVALID CORRECT PROTOTYPE
i) float average(a, b){ } data types are absent float average(double a, double b)
{}

ii) float multiply(int x, y){ } data type of 'y' is absent. float multiply(int x, int y){ }

iii) float clcPI(int, float = 3.14) int and float cannot be float clacPI(int a, float b=3.14)
variable names.

Answer #1
i)
x<y s= h= a= a%s!=0 a+=h
no 6 8 8 (8%6)yes 16
(16%6)yes 24
(24%8)no -----
return value of a = 24

ii) The method is computing the LCM of 2 given numbers.

Answer #2
i) true
ii) false

Answer #3
i)a) n= 8, i=4, k=1;
i>0 k*i*n i-- n--
yes 1*4*8=32 3 7
yes 32*3*7=672 2 6
yes 672*2*6=8064 1 5
yes 8064*1*5=40320 0 4
no

OUTPUT:-
40320

b)n= 7, i=3, k=7;


i>0 k*i*n i-- n--
yes 7*3*7=147 2 6
yes 147*2*6=1764 1 5
1
yes 1764*1*5=8820 0 4
no

OUTPUT:-
5040

ii) The function is computing the Factorial value.

Answer #4
i)
value of n k k>9 s i i!=0 s=s+i%10 i/=10
9876 9876 yes 0 9876 yes s=6 987
6 987 yes s=13 98
13 98 yes s=22 9
22 9 yes s=22 0
22 0 no -------- -----
22 yes 0 22 yes s=2 2
2 2 yes 2 0
2 0 no -------- -----
2 no ------ ------- ------- -------- -----
return false;

ii)
value of n k k>9 s i i!=0 s=s+i%10 i/=10
298 298 yes 0 298 yes s=8 29
8 29 yes s=10 2
10 2 yes s=10 0
10 0 no ------- -----
10 yes 0 10 yes s=1 1
1 1 yes 1 0
1 0 no ------- -----
1
return true;

You might also like