Home Answers Viewqa Java-Beginners permutstion of numbers in java

 
 


anuj
permutstion of numbers in java
2 Answer(s)      a year and a month ago
Posted in : Java Beginners

Is it possible to enter the number in a char so that permutation of a number can be acheived which will be in the form of string????? here is the coding i did...it worked really well when i initialized the char as(1,2,3).but its not working when i want the input from user.. .like if user enters 3...then string shud bcum 123 and permutation shud be obtained...plz help me i need to submit coding tomorrow itself...... import java.util.*; public class major1

{ int n=0; char c[]; public major1() {

        System.out.println("Enter the number of processors=");
            Scanner input=new Scanner(System.in);
        n=input.nextInt(); 
        for(int j=1;j<=n;j++)
        {
            c[n]=j;
        }
        String s=new String(c);
        showPattern("", s);
}






        public void showPattern(String st, String s) 
        {
            if (s.length() <= 1)

            System.out.println("\n"+st + s);
            else
            {
                for (int i = 0; i < s.length(); i++) 
                {
                    try 
                    {
                        String newString = s.substring(0, i)
                        +s.substring(i + 1);
                        showPattern(st + s.charAt(i), newString);
                    }   
                    catch (Exception e) 
                    {
                    e.printStackTrace();
                    }
                }
            }
        }
        public static void main(String args[])
        {
            major1 m1=new major1();
        }

}

View Answers

April 12, 2012 at 5:26 PM


import java.util.*;

public class PermutationExample extends ArrayList<String> {

public static void main(String[] args) {

int[] set = {1, 2, 3};
PermutationExample retVal = permutations(set, 3);
Collections.sort(retVal);
for (int k = 0; k < retVal.size(); k++) {
System.out.printf(" %s\n", retVal.get(k));
}
}
         private static PermutationExample permutations(int[] set, int choices) {
            PermutationExample perm =new PermutationExample();
            String resetStr, setStr;
            resetStr = setStr = "";
            for (int j = 0; j < choices; j++) {
            resetStr += String.format("%d", set[0]);
            setStr = resetStr;
            }
            java.util.Random rand = new java.util.Random();
            int max = 1;
            int k = 1;
            int nextDown = set.length;
            while (k <= choices) {
            max *= nextDown;
            nextDown--;
            k++;
            }
            while (perm.size()< max) {
            setStr = "";
            java.util.ArrayList<Integer> ele =new java.util.ArrayList<Integer>();
            while (ele.size() < choices) {
            int anInt = set[rand.nextInt(set.length)];
            if (!ele.contains(anInt)) {
            ele.add(anInt);
            }
            }
            setStr =String.format("%d%d%d",ele.get(0), ele.get(1), ele.get(2));
            if (!perm.contains(setStr)) {
            perm.add(setStr);
            }
            }
            return perm;
         }
        private boolean contains(String str) {
            return ((ArrayList) this).contains(str);
        }
      }

April 12, 2012 at 5:27 PM


import java.util.*;

public class PermutationExample {
    public static void main(String args[]) throws Exception {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter String: ");
        String chars = input.next();
        showPattern("", chars);
    }

    public static void showPattern(String st, String chars) {
        if (chars.length() <= 1)
            System.out.println(st + chars);
        else
            for (int i = 0; i < chars.length(); i++) {
                try {
                    String newString = chars.substring(0, i)
                            + chars.substring(i + 1);
                    showPattern(st + chars.charAt(i), newString);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    }
}









Related Pages:
permutstion of numbers in java
permutstion of numbers in java  Is it possible to enter the number in a char so that permutation of a number can be acheived which will be in the form of string????? here is the coding i did...it worked really well when i
Numbers
Java NotesNumbers Two kinds of numbers. There are basically two kinds of numbers in Java and most other programming languages: binary integers (most... numbers, you will usually use decimal numbers in your Java source program
automorphic numbers
automorphic numbers  how to find automorphic number in java   Hi Friend, Pleas visit the following link: Automorphic numbers Thanks
Prime Numbers
Prime Numbers  Create a complete Java program that allows the user to enter a positive integer n, and which then creates and populates an int array with the first n prime numbers. Your program should then display the contents
defining numbers in Java Script
defining numbers in Java Script  Explain about defining numbers in Java Script
prime numbers - Java Beginners
prime numbers  Write a java program to find the prime numbers between n and m
random numbers - Java Beginners
random numbers  write a program to accept 50 numbers and display 5 numbers randomly  Hi Friend, Try the following code: import...); System.out.println("Enter 10 numbers: "); for(int i=0;i<10;i
recursion numbers - Java Beginners
recursion numbers  I need to use recursion to test all values from 0 to 20 and see if they are contain in a 1-D array with values: 2,4,6,8,10,12,14,16,18,20. The results of all numbers from 0-20 will be printed
random numbers - Java Beginners
to display the random numbers, but not twice or more. I mean i need a number to be display once. This code allows some numbers to be displayed more than once. Hi... Scanner(System.in); System.out.println("Enter 10 numbers: "); for(int i=0;i<10;i
Perfect Numbers - Java Beginners
+ 2 + 3 Write a java program that finds and prints the three smallest perfect numbers. Use methods   Hi Friend, Try the following code: public
Java program - convert words into numbers?
Java program - convert words into numbers?   convert words into numbers?   had no answer sir
Java Pyramid of Numbers
Java Pyramid of Numbers  Hi, I want to know how the code to print the pyramid below works. It uses nested for loops. Pyramid: 1 2 1 2
Random numbers - Introduction
Java NotesRandom numbers - Introduction When to use random numbers There are many types of programs that use random numbers. Game programs use them... numbers. You might want to use random numbers to change the appearance
odd numbers with loop
odd numbers with loop  get the odd numbers till 100 with for,while loop   Java find odd numbers: class OddNumbers { public static void main(String[] args) { for(int i=1;i<=100;i
Sum of first n numbers
Sum of first n numbers  i want a simple java program which will show the sum of first n numbers....   import java.util.*; public class...; } System.out.println("Sum of Numbers from 1 to "+n+" : "+sum
Divide 2 numbers
Divide 2 numbers  Write a java program to divide 2 numbers. Avoid division by zeor by catching the exception.   class Divide { public static void main(String[] args) { try{ int num1=8
EVEN NUMBERS - Java Interview Questions
EVEN NUMBERS  i want program of even numbers?i want source code plz reply?  Hi Friend, Try the following code: class EvenNumbers... counter = 0; System.out.println("Even Numbers are:" ); for (int i
Applet for add two numbers
Applet for add two numbers  what is the java applet code for add two numbers?   import java.awt.Graphics; import javax.swing.*; public...); add(text2); label3 = new Label("Sum of Two Numbers
Textbox allows only numbers in java wicket
Textbox allows only numbers in java wicket  Please provide me wicket code for text box that allows only numbers to type. Thank you
adding two numbers - Java Beginners
information : http://www.roseindia.net/java/ Thanks
Java Find Automorphic numbers
Java Find Automorphic numbers In this section, you will learn how to find the automorphic numbers between 1 and 1000. Automorphic numbers are the numbers... of number 6 at the end. Here we are going to find the automorphic numbers between 1
Random Numbers - shuffling
Java NotesRandom Numbers - shuffling A standard approach to shuffling the elements of an array is to write some code as described below. As of Java 2... is described at the bottom. Shuffling: Random numbers without replacement
print 100 numbers using loops
print 100 numbers using loops  how to print from 1 to 100 using...); } } } Thanks   class Numbers { public satic void main...;a++) { System.out.println("val of a is ="+a); } } }   How java
Add Complex Numbers Java
How to Add Complex Numbers Java In this Java tutorial section, you will learn how to add complex Numbers in Java Programming Language. As you are already aware of Complex numbers. It is composed of two part - a real part and an imaginary
Finding all palindrome prime numbers - Java Beginners
Finding all palindrome prime numbers  How do i write a program to Find all palindrome prime numbers between two integers supplied as input (start and end points are excluded
Add two big numbers
Add two big numbers       In this section, you will learn how to add two big numbers. For adding two numbers implement two big decimal numbers then apply the Sum() method
Printing numbers in pyramid format - Java Beginners
Printing numbers in pyramid format  Q) Can you please tel me the code to print the numbers in the following format: 1 2 3 4 5 6 7 8 9 10   Hi Friend, Try
Write a program to list all even numbers between two numbers
Write a program to list all even numbers between two numbers       Java Even Numbers - Even Numbers Example in Java: Here you will learn to write a program for listing out
To find first two maximum numbers in an array
To find first two maximum numbers in an array  Java program to find first two maximum numbers in an array,using single loop without sorting array
Java write even numbers to file
Java write even numbers to file In this section, you will learn how to write the even numbers to file. By using the PrintWriter class, you can write any type... started a loop for numbers 1 to 50. If the number is totally divided by 2
read a positive real numbers from highest to lowest
read a positive real numbers from highest to lowest  write a java program that will read a sequence of 10 positive real nos. entered by the user and will print the same numbers in sorted order from lowest to highest using arrays
Calculate the Sum of three Numbers
Calculate the Sum of Three Numbers       This is simple java programming tutorial . In this section you will learn how to calculate the sum of three numbers by using three
JavaScript array of numbers
in understanding 'Java Script array of numbers'. For this we use  Java... JavaScript array of numbers   ... print the list of arrays of numbers.          
Add Two Numbers in Java
Add Two Numbers in Java     ... these arguments and print the addition of those numbers. In this example, args.... These passed arguments are of String types so these can't be added as numbers
Comparing Two Numbers
Comparing Two Numbers       This is a very simple example of Java that teaches you the method of comparing two numbers and finding out the greater one. First of all, name a class
greatest of 3 numbers using classes and functions.
greatest of 3 numbers using classes and functions.  WAP to calculate greatest of 3 numbers using classes and functions with parameters through input in main?   Java Find Largest Number import java.util.*; class
Program to display palindrome numbers between some range
Program to display palindrome numbers between some range  Hi!I want a java program to display palindrome numbers between 100 to 1000.can you please explain me the logic with an example   import java.util.*; public
Swapping of two numbers
Swapping of two numbers       This Java programming tutorial will teach you the methods for writing program to calculate swap of two numbers. Swapping is used where  you want
Calculate sum of even and odd numbers in Java
Calculate sum of even and odd numbers In this section, you will learn how to read the file that contains even and odd numbers and calculate their sum separately. To do this, first of all, we have found all the even and odd numbers from 1
Generating Random Numbers to Fill array. Java Beginner needing help!
Generating Random Numbers to Fill array. Java Beginner needing help!  Hello all! I am new to this site, and Java programming. My problem is: Write a program that produces random permutations of the numbers 1 to 10. eg
how to write a program in java to print numbers in equalateral triangle
how to write a program in java to print numbers in equalateral triangle  the output must be 1 324 76589   Here is an example of pattern 1 2 3 4 5 6 7 8 9 Example: public class NumberTriangle{ public
Find HCF of three numbers in Java Program
Find HCF of three numbers in Java Program In this java tutorial section, you... Common Divisor) of three numbers. As you already know that HCF or GCD .... Suppose there are three numbers 4,8 and 12. Factors of 4=1,2,4 Factors of 8=1,2,4,8
Java find prime numbers without using break statement
Java find prime numbers without using break statement In this tutorial, you will learn how to find the prime numbers without using break statement. You all are aware of Prime Numbers, these are the numbers which are either divided
ask user how many numbers to be inputted and determine the sum and highest number using an array in java
ask user how many numbers to be inputted and determine the sum and highest number using an array in java  ask user how many numbers to be inputted and determine the sum and highest number using an array in java
sorting numbers
sorting numbers  How to sort the numbers in ascending order   import java.util.*; class SortNumbers{ public static void main(String...=input.nextInt(); list.add(num); } System.out.println("Numbers
Numbers pyramid
Numbers pyramid  Hi sir, Can you please tell me how to output this using nested for loops? 1 2, 1 1, 2, 3 4, 3, 2, 1 1, 2, 3, 4, 5 6, 5, 4, 3, 2, 1 1, 2, 3, 4, 5, 6, 7 8, 7, 6, 5, 4, 3, 2, 1 1, 2, 3, 4, 5, 6, 7, 8, 9 10, 9, 8
Rational Numbers
Rational Numbers   Write and fully test a class that represents rational numbers. A rational number can be represented as the ratio of two integer values, a and b, where b is not zero. The class has attributes for the numerator

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.