public class darry{
int[] d={1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7};
int[][] td=new int[4][4];
public void put(int k,int e){
int i=k/4;
int j=k%4;
td[i][j]=e;
}
public void stor(){
for(int k=0;k<16;k++){
int e=d[k];
put(k,e);
}
}
public int get(int k){
int i=k/4;
int j=k%4;
return td[i][j];
}
public void print(){
for(int k=0;k<16;k++){
System.out.print(d[k]+" ");
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(td[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] a){
darry s=new darry();
s.stor();
s.print();
System.out.println(s.get(5));
System.out.println(s.get(10));
System.out.println(s.get(15));
}
}
Wednesday, November 30, 2011
convert the 2D array to 1D array
import java.util.*;
public class tDarry{
int n,c,r,e;
int[] d=new int[16];
int td[][]={{1,2,3,4},{5,4,6,8},{9,8,7,5},{3,6,4,7}};
public void put(int i,int j,int e){
int k=i*4+j;
d[k]=e;
}
public int get(int i,int j){
int[] d=new int[16];
int k=i*4+j;
return d[k];
}
public void stor(){
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
int e=td[i][j];
put(i,j,e);
}
}
}
public void print(){
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(td[i][j]+" ");
}
System.out.println();
}
for(int k=0;k<16;k++){
System.out.print(d[k]+" ");
}
}
public static void main(String[] a){
tDarry s=new tDarry();
s.stor();
s.print();
}
}
public class tDarry{
int n,c,r,e;
int[] d=new int[16];
int td[][]={{1,2,3,4},{5,4,6,8},{9,8,7,5},{3,6,4,7}};
public void put(int i,int j,int e){
int k=i*4+j;
d[k]=e;
}
public int get(int i,int j){
int[] d=new int[16];
int k=i*4+j;
return d[k];
}
public void stor(){
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
int e=td[i][j];
put(i,j,e);
}
}
}
public void print(){
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(td[i][j]+" ");
}
System.out.println();
}
for(int k=0;k<16;k++){
System.out.print(d[k]+" ");
}
}
public static void main(String[] a){
tDarry s=new tDarry();
s.stor();
s.print();
}
}
Wednesday, November 16, 2011
find next odd,even,prime and Composite numbers
import java.util.*;
public class mynumber{
int num,odd,even,comp,prime;
public mynumber(){
num=0;
odd=1;
even=1;
comp=4;
prime=3;
}
public int next_number(){
num=num+1;
return num;
}
public boolean isodd(int n){
if(n%2==1)
return true;
else return false;
}
public int next_odd(){
odd=odd+1;
while(!isodd(odd)){
odd=odd+1;
}
return odd;
}
public int next_even(){
even=even+1;
while(isodd(even)){
even=even+1;
}
return even;
}
public int next_comp(){
comp=comp+1;
for(int i=2;i<comp;i++){
if(comp%i==0) break;
else comp=comp+1;
}
return comp;
}
public int next_prime(){
prime=prime+1;
int x=0;
for(int i=2;i<prime;i++){
if(prime%i==0) prime=prime+1;
else break;
}
return prime;
}
public static void main(String[] a){
mynumber n=new mynumber();
System.out.println("next odd number= "+n.next_odd());
System.out.println("next even number= "+n.next_even());
System.out.println("next Composite number= "+n.next_comp());
System.out.println("next prime number= "+n.next_prime());
}
}
/*C:\Users\KAPILAN\Desktop>javac mynumber.java
C:\Users\KAPILAN\Desktop>java mynumber
next odd number= 3
next even number= 2
next Composite number= 6
next prime number= 5*/
public class mynumber{
int num,odd,even,comp,prime;
public mynumber(){
num=0;
odd=1;
even=1;
comp=4;
prime=3;
}
public int next_number(){
num=num+1;
return num;
}
public boolean isodd(int n){
if(n%2==1)
return true;
else return false;
}
public int next_odd(){
odd=odd+1;
while(!isodd(odd)){
odd=odd+1;
}
return odd;
}
public int next_even(){
even=even+1;
while(isodd(even)){
even=even+1;
}
return even;
}
public int next_comp(){
comp=comp+1;
for(int i=2;i<comp;i++){
if(comp%i==0) break;
else comp=comp+1;
}
return comp;
}
public int next_prime(){
prime=prime+1;
int x=0;
for(int i=2;i<prime;i++){
if(prime%i==0) prime=prime+1;
else break;
}
return prime;
}
public static void main(String[] a){
mynumber n=new mynumber();
System.out.println("next odd number= "+n.next_odd());
System.out.println("next even number= "+n.next_even());
System.out.println("next Composite number= "+n.next_comp());
System.out.println("next prime number= "+n.next_prime());
}
}
/*C:\Users\KAPILAN\Desktop>javac mynumber.java
C:\Users\KAPILAN\Desktop>java mynumber
next odd number= 3
next even number= 2
next Composite number= 6
next prime number= 5*/
Subscribe to:
Posts (Atom)