to print possible combinations of numbers (234, 243,324,342,etc)

import java.util.*;
class permutationDigits
{
int count = 0;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a digit: ");
int a= sc.nextInt();
String s=String.valueOf(a); //converting int into string
System.out.println("The Anagrams are : ");
compute("",s);
System.out.println("Total Number of Anagrams = "+count);
}
void compute(String s1, String s2)
{
if(s2.length()<=1)
{
count++;
System.out.println(s1+s2);
}
else
{
for(int i=0; i<s2.length(); i++)
{
String a = s2.substring(i, i+1);
String b = s2.substring(0, i);
String c = s2.substring(i+1);
compute(s1+a, b+c);
}
}
}
public static void main(String args[])
{
permutationDigits ob=new permutationDigits();
ob.input();
}
}

Output:

Enter a digit: 234
The Anagrams are :
234
243
324
342
423
432
Total Number of Anagrams = 6

To print Anagrams (eg: TOP,TPO,POT, etc..)

import java.util.*;
class anagramsWord
{
int count = 0;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word : ");
String s = sc.next();
System.out.println("The Anagrams are : ");
compute("",s);
System.out.println("Total NO. of Anagrams = "+count);
}

void compute(String s1, String s2)
{
if(s2.length()<=1)
{
count++; // no of combination words
System.out.println(s1+s2);
}
else
{
for(int i=0; i<s2.length(); i++)
{
String a = s2.substring(i, i+1);
String b = s2.substring(0, i);
String c = s2.substring(i+1);
compute(s1+a, b+c); // recursive method
}
}
}
public static void main(String args[])throws Exception
{
anagramsWord ob=new anagramsWord();
ob.input();
}
}

Output:

Enter a word : TOP
The Anagrams are :
TOP
TPO
OTP
OPT
PTO
POT
Total NO. of Anagrams = 6

Printing possible combinations of 4 digit number

import java.util.*;
public class Combinations
 {
public static void main(String[] args)
 {
int[] a = { 1, 5, 3, 6 }; 
for (int w = 0; w < 4; w++) 
{
    for (int x = 0; x < 4; x++) 
     {
        for (int y = 0; y <4; y++) 
        {
          for (int z = 0; z < 4; z++) 
          {
             if (w!=x && x != y && y != z && z != x) 
             { 
             System.out.println(a[w] + ""+a[x] + "" + a[y] + "" + a[z]); 
             } 
          }
        }
     }
  }
}
}

1513
1516
1531
1536
1561
1563
1315
1316
1351
1356
1361
1365
1615
1613
1651
1653
1631
1635
5153
5156
5135
5136
5165
5163 continue….

Evil Number

An Evil number is a positive whole number which has even number of 1’s in its binary equivalent. Eg: Binary equivalent of 9 is 1001, contains even number of 1’s.

Design a program to accept a positive whole number and find the binary equivalent of the number and count the number of 1’s in it and display whether it is an Evil number or not.

Eg:

INPUT : 15
BINARY EQUIVALENT : 1111
NO. OF 1’S : 4
OUTPUT : EVIL NUMBER

import java.util.Scanner;
class bintoDec
{
    int n, a, count1=0;
    String s = "";
    void input()
     {

     Scanner sc = new Scanner(System.in);
     System.out.println("Enter decimal number ");
     n = sc.nextInt();
     System.out.println("INPUT : "+n);
     }
void calculate()
{
    input();
    while(n>0)
{
a = n%2;
if(a == 1)
{
count1++;
}
s = a+" "+s;
n = n/2;
}
System.out.println("BINARY EQUIVALENT:" +s);
System.out.println("NO. OF 1's " +count1);
}
void count()
{
    calculate();
    if( count1 %2 ==0)
    System.out.println("\n EVIL NUMBER");
    else
    System.out.println("\nNOT AN EVIL NUMBER");
    
}
public static void main(String args[])
{
    bintoDec bin=new bintoDec();
    bin.count();
}
}

Enter decimal number
24
INPUT : 24
BINARY EQUIVALENT:1 1 0 0 0
NO. OF 1’s 2

EVIL NUMBER

Enter decimal number
25
INPUT : 25
BINARY EQUIVALENT:1 1 0 0 1
NO. OF 1’s 3

NOT AN EVIL NUMBER

Sum of Series x2/1! + x4/3! + x6/5! + xn/(n-1)!

A class SeriesOne is desgined to calculate the sum of the following series:

Sum = x2/1! + x4/3! + x6/5! + xn/(n-1)!

Some of the members of the class are given below:

class name : SeriesOne

Data members/ instance variables :

x : to store an integer number
n : to store number of terms
sum : double variable to store the sum of the series

Member functions:

SeriesOne(int xx, int nn) : constructor to assign x=xx and n=nn

double findfact(int m) : to return the factorial of m .

double findpower(int x, int y) : to return x raised to the power of y

void calculate() : to calculate the sum of the series
void display() : to display the sum of the series.

Specify the class SeriesOne, giving the details of the above member data and methods only.

Define the main() function to create an object and call the member function accordingly to enable the task.

import java.util.*;
class SeriesOne
    {
         int x,n,sum;
           
            SeriesOne(int xx, int nn)
            {
                x=xx;
                n=nn;
            }
          
        double findfact(int m)
        {
            int fact=1;
            for(int i=1;i <=m; i++)
            fact=fact*i;
            return fact;
        }
        
        double findpower(int x, int y)
        {
          double res;
           res= Math.pow(x,y);
           return res;
        }
         void cal()
         {
          
             double term=0;
             for(int i=2; i <=n; i=i+2)
             {
             term= findpower(x,i) / findfact( i-1);
             sum+=term;
            }
         }
        void display()
        {
            System.out.println("Sum of the series:"+sum);
        }
        public static void main(String[] args){
            SeriesOne  so=new SeriesOne();
          
            so.cal();
            so.display();
          
        }
    }

Enter the value of x:
3
Enter the value of n:
4
Sum of the series:22

Disarium Number using Methods

Disarium number is a number defined by the following process:

Sum of its digits powered with their respective position is equal to the original number. Some other DISARIUM are 89, 135,175, 518 etc

Input   : n = 135Output  : Yes 1^1 + 3^2 + 5^3 = 135Therefore, 135 is a Disarium number
import java.util.*;
class DisariumNumber
    {
         int n,rem, sum,len;
           DisariumNumber()
           {
            n =0;
            rem = 0;
            sum = 0;
            
            }
            void read()
            {
            Scanner sc=new Scanner(System.in);
            System.out.print("Enter a number : ");
            n = sc.nextInt();
            }
    
            void cal()
            {
                read();
            int copy = n; 
            String s = Integer.toString(n); //converting the number into a String
            len = s.length(); //finding the length of the number 
             
            while(copy>0)
            {
                rem = copy % 10; //extracting the last digit
                sum = sum + (int)Math.pow(rem,len);
                len--;
                copy = copy / 10;
            }
              if(sum == n)
                System.out.println(n+" is a Disarium Number.");
            else
                System.out.println(n+" is not a Disarium Number.");
        }
        public static void main(String[] args){
            DisariumNumber ds= new DisariumNumber();
            ds.cal();
          
        }
    }

Enter a number : 135
135 is a Disarium Number.
Enter a number : 234
234 is not a Disarium Number.
Enter a number : 89
89 is a Disarium Number.

Prime Numbers Factorial

Write a program to check whether the given number is prime number or not, if it is prime number display the factorial.

import java.util.*;
public class primeFactorial
{    
    int i,m,flag,n;  
    primeFactorial()
    {
        n=0;
        m=0;
        flag=0;
    }
    public  void input()
    {    
        Scanner sc = new Scanner (System.in);
        System.out.println("Enter the number:");
        n=sc.nextInt();
    }
    public void calculate()
    {
       input();        // input()method invocation
        m=n/2;      
        if(n==0||n==1)
        {  
            System.out.println(n+" is not prime number");      
           
        }
        else
        {  
            for(i=2;i<=m;i++)
            {      
                if(n%i==0)
                {          
                    flag=1;      //status variable, if any divisor found, flag=1
                    break;      
                }      
            } // end of for loop
            if(flag==0) 
            { 
                System.out.println(n+" is prime number"); 
                fact(n);
                
            }
            else    
            {
                System.out.println(n+" is not prime number");
            }
        }//end of outer else  
    }
   public void fact(int num1)
   {
       int n2=num1,fact=1;
       for(int j=1;j<=n2;j++)
       fact=fact *j;
       System.out.println("Factorial " +   fact);
    }
    
 public static void main(String args[])
 {    
     primeFactorial pN=new primeFactorial();
     pN.calculate();   // calculate() method call
  
    }
}   

Output:

Enter the number:
5
5 is prime number
Factorial 120


Enter the number:
4
4 is not prime number

Prime Number

Prime Number : – a number which is divisible by 1 and itself (i.e it has only two factors).
0 & 1 are non prime numbers.
Eg: 5 ===> 5 * 1 = 5 (1 , 5 are the two factors – 5 is Prime no.)

15 =====> 15 * 1= 15 , 5 * 3= 15
Factors of 15 are 1, 3 and 5. (3 factors – 15 is not a Prime no.)
——————————————————————————————————————–

Write a Java program to check whether the given number is
PRIME or NOT.

To understand the program, here a simple explanation:

for loop variable starts with 2 (i =2 ) because 1 is a factor of every number (prime and non prime). We only have to loop through 2 to half of given number , because no number is divisible by more than its half.

(Divisibilty concept a % b == 0 , to find the divisor. If any divisor finds, the
value of flag updates as 1 and immediately exit from the loop by the break statement. )

If the flag =1 means it is not a prime number

import java.util.*;
public class primeNumberProgram
{    
 public static void main(String args[])
 {    
  int i,m=0,flag=0,n;      
  Scanner sc = new Scanner (System.in);
   System.out.println("Enter the number:");
        n=sc.nextInt();
  m=n/2;      
  if(n==0||n==1)
  {  
   System.out.println(n+" is not prime number");      
  }
  else
  {  
   for(i=2;i<=m;i++)
   {      
    if(n%i==0)
    {          
     flag=1;      //status variable, if any divisor found, flag=1
     break;      
    }      
   } // end of for loop
   if(flag==0) 
   { 
       System.out.println(n+" is prime number"); 
    }
   else    
   {
       System.out.println(n+" is not prime number");
    }
  }//end of outer else  
}// main    
}//class   

Output:

Enter the number:
25
25 is not prime number
Enter the number:
23
23 is prime number
Enter the number:
19
19 is prime number

m=n/2, because to find factors of a number,
eg: 10 can be written as
1 * 10 =10
2 * 5 = 10
can either write as
5 * 2 = 10
10 * 1= 10

From the above example, you can understand by finding the factors from 1 to n is same as 1 to n/2.

till half of the n value is required. (m =n/2)

Object as parameter

Java Programs -ISC & ICSE

Money is an unit that has 2 parts, Rupees and Paise, where 100 Paise=1 Rupee.
Design a  class named Money whose details are as follows:
Class Name : Money
Data member:
int rs, ps  : integer to store the value of Rupees and paise.
Member methods:
Money(…..)     : parameterized constructor to initialize member data
void fnShow()  : to show the member data as Money [Rs 819.75]
Money fnAdd(Money m1, Money m2) :
Add m1 and m2. Store the result in corresponding member data and return it.

Specify the class Money, giving the details of the above member data and methods. Also write the main() to create objects and call the other methods accordingly to enable the task of the adding 2 units on Money.

Program:

import java.util.*; class Money { int rs, ps; public Money(int rs1, int ps1) { rs=rs1; ps=ps1; } public Money() { rs=0; ps=0; } void fnShow()…

View original post 71 more words

Emirp Number

An emirp number is a number which is prime backwards and forwards. Example : 13 and 31 are both prime numbers. Thus 13 is a emirp number.

import java.util.*;
class Emirp
{ 
    int n, rev,f;
    Emirp(int nn)
    {
        n=nn;
        rev=0;
        f=2;
    }
    int isprime(int x)
    {
        if(n==x)
        return 1;
        else if (n%x==0 || n==1)
        return 0;
        else
        return isprime(x+1);
    }
    void isEmirp()
    {
        int x=n;
        while(x!=0)
     {
         rev=rev*10 + x%10;
         x=x/10;
         
        }
        int ans1=isprime(f);
        n=rev;
        f=2;
        int ans2=isprime(f);
        if(ans1==1 && ans2==1)
        System.out.println(n+"is an emirp no");
        else
        System.out.println(n+"is not an emirp no.");
        
    }
    public static void main(String[] args) 
    { 
      int a; 
      Scanner sc= new Scanner(System.in);
      System.out.println("Enter the number:");
      a=sc.nextInt();
        Emirp emp=new Emirp(a);
        emp.isEmirp();

    } 
} 

Enter the number:
13
31is an emirp no
Enter the number:
51
15is not an emirp no.