0% found this document useful (0 votes)
30 views2 pages

Dsa Question

basic dsa

Uploaded by

Siddharth Singh
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)
30 views2 pages

Dsa Question

basic dsa

Uploaded by

Siddharth Singh
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

-Greatest Common Divisor (GCD) >

hcf (higest Common


factor.
divide
We want a
highest number that can both .
number

Our
·
minimum answer

function gcd (a b) ,
is ·
1

Let ans = 1 ;

for (Let i = 2 ; i Math - min (a b) ;


,
i++ ) 2x3
if (a % i = 0 && b % i =
0)[ 4 ans ,

ans i
We use <= because
33
·

max number should


return
3
ans be
multiply by own

Console
Log Eged(27 28) //Output >
-

.
,

Q-2GCD Euclid Theorem


aby
.

function gcd(a , b)
While (b ! == 0) E
b
let temp =

b = a
%b Let numl = 48
a = temp
Let 24
& mm2 =

return a
3 Output= 24

0 - Given a number u . Calculate the sum


of digits of no.
Na
1035(413
En-7136 function sum
(n) [ I
O Remainder 10

T
ans = >

(n 0) [
While
LastD =
>

1 %
-

10 > Quotient 3T
ans += Last D
Rem
U = Math
floor (n/10) possi
3
return ans quotient
3
num
Q4 - Fibonacci Series >
Q5 8 13 2--
-

, , ,

function fib (a) [


if (a 0)[ = =

Console
Vetun ,
.

log (0)
3
if (a) 1)q =

Console .

log (0)
console .

log (1)
it last = 1
let second last = O

i + +) [
for (leti 0 ic = ;
=
; a

let = last
result + Second last

Console .

log (result)
last = result
Second last= last

3
print Fibonacci
(s)
P Reverse number
given
-

function Reverse (a) [


if (a 0)d =

return
Console-log (0)
Let reverseNum = O

while (a z0) [
Last
digit =. a % 10
(RevereeNum 10) + last
ReveseNm =
Digit
*

Math
3
a =
floor (a /10)
. .

return revers Num 3

console. log (Reverse (4356))


6543
=

You might also like