-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresult.java
More file actions
49 lines (47 loc) · 1.04 KB
/
result.java
File metadata and controls
49 lines (47 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package demo;
public class DemoTranslation {
public static int search ( int arr [ ] , int arr_len , int x ) {
for ( int i = 0 ; i < arr_len ; i ++ ) if ( arr [ ( int ) i ] == x ) return ( int ) i ;
return - 1 ;
}
public static int verifyFactorial ( int num ) {
int fact = 1 ;
if ( num < 0 ) {
fact = - 1 ;
}
else if ( num > 1 ) {
do {
fact = fact * num ;
num = num - 1 ;
}
while ( num > 1 ) ;
}
else {
fact = 1 ;
}
return fact ;
}
public static void main ( String[] args ) {
int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ;
int num = 6 ;
int result = search ( arr , 6 , num ) ;
System.out.print( "");
System.out.print( result );
System.out.print( "\n");
result = factorial ( num ) ;
System.out.print( "");
System.out.print( result );
System.out.print( "\n");
}
public static int factorial ( int num ) {
int copyNum = num ;
int fact = 1 ;
if ( num < 0 ) fact = - 1 ;
else {
while ( num > 1 ) {
fact = fact * num -- ;
}
}
return fact == verifyFactorial ( copyNum ) ? fact : - 2 ;
}
}