Wednesday, 3 August 2011

PROGRAM TO PRINT ALL POSSIBLE COMBINATIONS OF A FOUR LETTERED STRING


PROGRAM-1

PROGRAM TO PRINT ALL POSSIBLE COMBINATIONS OF A
FOUR LETTERED STRING

ALGORITHM
Step-1 Start
Step-2 Enter the string and store it in‘s’
Step-3 Store the length of the string in ‘l’
Step-4 Take three loops
            1st loop- for (a=0; a<l;a++)
            2nd loop-for (b=0; b<l;b++)
            3rd loop -for(c=0; c<l;c++)
Step-5 If a,b and c are not equal then add them ,subtract them from 6 and store the result in ‘d’
Step-6 Add all the characters of‘s’ at locations a,b,c,d and store it ‘w’
Step-7 Print ‘w’
Step-8 Exit






PROGRAM

import java.io.*;
class combinations
{
public static void main()throws IOException
{
int l,a,b,c,d;
char r,t,u,v;
String w="",s="";
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A 4 LETTERED STRING");
s=br.readLine();
l=s.length();
System.out.println ("COMBINATIONS ARE");
for(a=0;a<l;a++)
{
for (b=0;b<l;b++)
{
for (c=0;c<l;c++)
{
if (a!=b&&b!=c&&c!=a)
{
d=6-(a+b+c);
r=s.charAt(a);
t=s.charAt(b);
u=s.charAt(c);
v=s.charAt(d);
w=r+""+t+""+u+""+v;
System.out.println (w);
}
w="";}}}}}


VARIABLE DESCRIPTION-

VARIABLE USED
DATATYPE
DESCRIPTION
l
Integer type
For storing the length.
a,b,c
Integer type
Used for running different loops for combinations.
r,t,u,v
Character type
Used for storing different characters.
s
String type
Used for storing the entered string.
w
String type
Used for storing combinations.












INPUT/OUTPUT-

INPUT
OUTPUT







ENTER A 4 LETTERED STRING
COMP
COMBINATIONS ARE
COMP
COPM
CMOP
CMPO
CPOM
CPMO
OCMP
OCPM
OMCP
OMPC
OPCM
OPMC
MCOP
MCPO
MOCP
MOPC
MPCO
MPOC
PCOM
PCMO
POCM
POMC
PMCO
      PMOC

No comments:

Post a Comment