P. 1
Logic Dev Elopement

# Logic Dev Elopement

|Views: 93|Likes:
Logic Development for java
Logic Development for java

See more
See less

04/11/2011

pdf

text

original

# Name: Dil Prasad Kuwor Date: 9/19/2010 Rollno: 064/02 Subject: Cryptography Lab No.: 1 (Logic Development) 1.

An integer number can be enclosed as an array as follows. Each digit n of the number is represented by n zeros followed by a 1. So the digit 5 is represented by 0,0,0,0,1. The encodings of each digit of a number are combined to form the encoding of the number . so the number 1234 is encoded as the array {0, 1 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1}. The first 0, 1 is contributed by the digit 1, the next 0, 0, 1 is contributed by the digit 2, and so on. There is one other encoding rule: if the number is negative, the first element of the encoded array must be -1, so -201 is encoded as {-1, 0, 0, 1, 1, 0, 1}. Note that the 0 digit is represented by no zeros, i.e. there are two consecutive ones! Write a method named encodeArray that takes an integer as an argument and returns the encoded array. Java Code: import java.util.*; class EncodeArray { public static void main (String[] args) { Scanner in=new Scanner(System.in); int a[] = new int[50]; int i=0,c=0; System.out.println("Enter the number"); int n =in.nextInt(); System.out.printf("The encoded array is:"); System.out.printf("{"); if(n<0) { System.out.printf("-1"+","+" "); n=-n; } while(n!=0) { a[c]=n%10; n=n/10; c++; } for(i=c-1;i>=0;i--) { n=a[i]; if(n==0) { System.out.printf("1"); } else { while(n>0) { n--; System.out.printf("0"+","+" "); if(n==0) {

System.out.printf("1"); } } } if(i>0) { System.out.printf(","+" "); } } System.out.printf("}"); } }

2

2. A positive non-zero number n is a factorial prime if it is equal to factorial(n) + 1 for some n andit is prime. Recall that factorial(n) is equal to 1*2*…*n-1*n. if you understand recursion, the recursive definition is Factorial(1) = n*factorial(n-1). For example, factorial(5) = 1*2*3*4*5 = 120. Recall that a prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. Write a method named isFactorialPrime which returns 1 if its argument is a factorial prime number, otherwise it returns(). Java code: import java.util.*; class IsFactorialPrime { public static void main (String[] args) { System.out.println("Enter the number"); Scanner in=new Scanner(System.in); int a=in.nextInt(); int j=1; for(int i=0;i<j;i++) { j++; if((fact(i)+1)==a) { System.out.printf("1"); j=i-1; } else if((fact(i)+1)>a) { System.out.printf("0"); //break; j=i-1; } } } static long fact(long a) { if(a==0) return 1; else return a*fact(a-1); } }

3

3. An array is called systematically increasing if it consists of increasing sequence of the numbers from 1 to n. The first six (there are over 65,000 of them) systematically increasing arrays are: {1} {1, 1, 2} {1, 1, 2, 1, 2, 3} {1, 1, 2, 1, 2, 3, , 1, 2, 3, 4 } Write a function named isSystematicallyIncreasing which returns 1 if its array argument is systematically increasing. Otherwise it returns(). Java Code: import java.util.*; class IsSystematicallyIncreasing { public static void main (String[] args) { int a[] = new int[50]; System.out.println("Enter the size of array"); Scanner in=new Scanner(System.in); int n=in.nextInt(); System.out.println("Enter the element of array"); int j=0,c=1,t=0,i,p=1,g=1,q=7; for(i=0;i<n;i++) { a[i]=in.nextInt(); } for(i=0;i<n;i++) { for(j=1;j<=c;j++) { if(a[i]==p) { t++; } i++; p++; } i--; p=1; c++; } for(i=0;i<q;i++) { System.out.printf("*"); if(i==6&&g==1) { System.out.printf(" "+"value return"+" "); q=q+7; g++; }

4

} System.out.printf("\n"); if(t==n) { System.out.printf("1"); } else System.out.printf("0"); }

}

5

scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->