Showing posts with label ISC. Show all posts
Showing posts with label ISC. Show all posts

Friday, February 10, 2012

Java program to find the divisibility by 2 without using mod(%)

/**to find divisibility by 2 without using "%"
*/
public class Shivam
{
public static boolean check(int a)
{
if(a/2.0-a/2==0)
return true;
return false;

}
}

Recursive function in java to print all natural numbers from 1 upto (n-1)

// recursive func to print a series of natural nos. upto (x-1)
import java.io.*;
public class RecFUNCseries
{
public static void main(String args[]) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter an integer: ");
int x=Integer.parseInt(br.readLine());
series(x);
}
public static void series(int x)
{
if(x>0)
series(--x);
if(x!=0)
System.out.print(x+" ");
}
}

Java programs to find the coprimes of a number

// coprimes of any number n are numbers bw 1 and n, which are not divisible by any factors of n
public class Coprimes
{

public static void main(String args[])
{
int sum=0;
int p=0;
int y;
for(int i=18; i<=23; i++)
{
sum=0;
p=0;
int[]a=new int[i/2];
for(int j=2; j<=i/2; j++)
{
if(i%j==0 && IsPrime(j)==true)
a[p++]=j;
}

System.out.println("For "+i+", the co-primes are:");
for(int x=1; x {
for(y=0; y {
if(x%a[y]==0)
break;
}
if(y==p)
{
sum+=x;
System.out.println(x);
}
}
System.out.println("Their sum is "+sum);
System.out.println("");
System.out.println("");
}
}

public static boolean IsPrime(int a)
{
a=Math.abs(a);
for(int i=2; i<=a/2; i++)
if(a%i==0)
return false;
return true;
}
}

Java program To remove duplicates from an array of integers so that each number may appear only once

import java.io.*;
class remDup
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the length of the array: ");
int l=Integer.parseInt(br.readLine());
int[]a=new int[l];
for(int i=0; i {
System.out.print("Enter the element number "+(i+1)+" of the array: ");
a[i]=Integer.parseInt(br.readLine());
}

//now the real work begins

for(int i=0; i {
for(int j=i+1; j {
while(a[i]==a[j])
{
for(int A=j; A a[A]=a[A+1];
l--;
}
}
}

// real work is done now!!!!


for(int i=0; i {
System.out.println(a[i]);
}

}// end of main
}// end of class

Wednesday, February 8, 2012

java program To find the coprimes of a number

// coprimes of any number n are numbers bw 1 and n, which are not divisible by any factors of n
public class Coprimes
{

public static void main(String args[])
{
int sum=0;
int p=0;
int y;
for(int i=18; i<=23; i++)
{
sum=0;
p=0;
int[]a=new int[i/2];
for(int j=2; j<=i/2; j++)
{
if(i%j==0 && IsPrime(j)==true)
a[p++]=j;
}

System.out.println("For "+i+", the co-primes are:");
for(int x=1; x{
for(y=0; y{
if(x%a[y]==0)
break;
}
if(y==p)
{
sum+=x;
System.out.println(x);
}
}
System.out.println("Their sum is "+sum);
System.out.println("");
System.out.println("");
}
}

public static boolean IsPrime(int a)
{
a=Math.abs(a);
for(int i=2; i<=a/2; i++)
if(a%i==0)
return false;
return true;
}
}

java program: Recursive function to multiply two numbers

// to multiply 2 nos. using recursive functions
import java.io.*;
class recursiveMultiplication
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a number: ");
String s=br.readLine();
System.out.print("Enter another number: ");
String S=br.readLine();
int a=Integer.parseInt(s);
int b=Integer.parseInt(S);
if((a<0&&b>0)||(a>0&&b<0))
System.out.print(-Mult(Math.abs(a),Math.abs(b)));
else System.out.print(Mult(Math.abs(a),Math.abs(b)));
}
public static int Mult(int a, int b)
{
if(a==0||b==0)
return 0;
else if(b==1)
return a;

return(a+Mult(a,(b-1)));
}

}

java program To print a String in reverse order using recursion

// to print a string in reverse order using recursion
import java.io.*;
class reverseString
{
public static void main(String args[])throws IOException
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter a String: ");

String s=br.readLine();
reverse(s, s.length());
}

public static void reverse(String S, int a)throws IOException
{
System.out.print(S.charAt(--a));
if(a>0)
reverse(S,a);
}
}

Saturday, February 6, 2010

Shortest Function to find if a number is prime

class prime
{
public static boolean PrimeCheck(int a)//a should be greater than 0
{
for(int i=2; i<=a/2; i++)
if(a%i==0)
return false;
return true;
}
}