• No se han encontrado resultados

Identificación de variables e indicadores

STEP 2 - INPUT string s

STEP 3 - StringTokenizer st = s STEP 4 - l =str.length()

STEP 5 - INPUT look STEP 6 - flag = -1

STEP 7 - IF (st.hasMoreElements()) REPEAT STEP 8

STEP 8 - IF (look.equals(st.nextElement())) THEN flag =1 STEP 9 - IF flag = - 1 GOTO STEP 10 OTHERWISE STEP 11 STEP 10 - PRINT "word not found"

STEP 11 - PRINT "word found"

STEP 12 - END

Solution:

import java.util.StringTokenizer;

import java.io.*;

public class word_search {

public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("enter the string=");

String s = br.readLine();

StringTokenizer st = new StringTokenizer(s," ");

System.out.println("enter the word to be searched =");

String look = br.readLine();

int flag = -1;

while(st.hasMoreElements()) {

if(look.equals(st.nextElement())) flag =1;

}

if(flag ==-1) {

System.out.println("the word not found");

} else {

System.out.println("the word found");

} } }

Output:

enter the string=

there are 7 days in a week

enter the word to be searched = are

the word found

PROGRAM 20 - To decode the entered string

Algorithm :

STEP 1 - START

STEP 2 - INPUT name, n STEP 3 - l=name.length()

STEP 4 - PRINT original string is "+name STEP 5 - IF i=0 THEN GOTO STEP 6 STEP 6 - char c1=name.charAt(i) STEP 7 - c=(int)c1

STEP 8 - IF n>0 THEN GOTO STEP 9 THERWISE GOTO STEP 12

STEP 9 - IF (c+n)<=90 THEN GOTO STEP 10 OTHERWISE GOTO STEP 11 STEP 10 - PRINT (char)(c+n)

STEP 11 - c=c+n;c=c%10,c=65+(c-1) & PRINT (char)(c)

STEP 12 - ELSE IF n<0 THEN GOTO STEP 13 OTHERWISE GOTO STEP 19 STEP 13 - n1=Math.abs(n)

STEP 14 - IF (c-n1) >=65 THEN GOTO STEP 15 OTHERWISE GOTO STEP 16 STEP 15 - DISPLAY (char) (c-n1)

STEP 16 - IF c>65 THEN GOTO STEP 17 OTHERWISE GOTO STEP 18 STEP 17 - c=c-65,

STEP 18 - c=n1 & PRINT (char)(90-(c-1)) STEP 19 - ELSE IF n==0

STEP 20 - DISPLAY "no change "+name STEP 21 - END

Solution :

class decode {

public void compute(String name,int n) {

int j,i,l,c=0,y,n1;

l=name.length();

System.out.println("original string is "+name);

for(i=0;i<l;i++) {

char c1=name.charAt(i);

try {

c=(int)c1 ; }

catch(NumberFormatException e) {}

if(n>0) {

if((c+n)<=90)

System.out.print((char)(c+n));

else {

c=c+n;c=c%10;

c=65+(c-1);

System.out.print((char)(c));

} }

else if(n<0) {

n1=Math.abs(n);

if((c-n1) >=65)

System.out.print((char) (c-n1));

else {

if(c>65) c=c-65;

else c=n1;

System.out.print((char)(90-(c-1)));

} }

else if (n==0) {

System.out.println("no change "+name);

break;

} } } }

Output:

original string is ABCDE n = 4

decoded string is EFGHI

PROGRAM 21 - To display the entered string in alphabetical order .

Algorithm :

STEP 1 - START

STEP 2 - str = "" , l = 0 STEP 3 - INPUT string str STEP 4 - l =str.length()

STEP 5 - FROM i=0 to i<l REPEAT STEP 6 STEP 6 - c[i] = str.charAt(i)

STEP 7 - FROM i=0 to i<l-1 REPEAT STEP 8 STEP 8 - FROM j=0 to i<l-1 REPEAT STEP 9

STEP 9 - temp =c[j], c[j] = c[j+1] , c[j+1] = temp STEP 10 - FROM i=0 to i<l REPEAT STEP 11

STEP 11 - PRINT c[i]

STEP 12 - END

Solution:

import java.io.*;

class Alpha {

String str;

int l;

char c[] = new char[100];

public Alpha() {

str = "";

l =0;

}

public void readword() throws IOException {

System.out.println("enter word - ");

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

str = br.readLine();

l = str.length();

public static void main(String args[]) throws IOException {

Output:

enter word - window dinoww

PROGRAM 2 2 - To create a string and count number of vowels and consonants .

Algorithm :

STEP 1 - START

STEP 2 - a = "Computer Applications"

STEP 3 - z = a.length() STEP 4 - x= 0 , b= 0

STEP 5 - FROM y =0 to y<z REPEAT STEP 6

STEP 6 - IF (a.charAt(y)=='a'||a.charAt(y)=='e'||a.charAt(y)=='i'||

a.charAt(y)=='o'||

a.charAt(y)=='u') THEN x =x +1 OTHERWISE b = b+1 STEP 7 - PRINT x

STEP 8 - PRINT b STEP 9 - END

Solution:

class p42 {

public static void main(String args[]) {

String a="Computer Applications";//initialising string int z=a.length(),y,x=0,b=0;

for(y=0;y<z;y++)//loop for counting number of vowels {

if(a.charAt(y)=='a'||a.charAt(y)=='e'||a.charAt(y)=='i'||a.charAt(y)=='o'||

a.charAt(y)=='u') x++;

else b++;

}

System.out.println("Number of vowels in string ="+x);

System.out.println("Number of consonants in string ="+b);

} }

Output:

Number of vowels in string =7

Number of consonants in string =12

PROGRAM 23 - To create a string and count number of words .

Algorithm :

STEP 1 - START

STEP 2 - a = "Computer Applications"

STEP 3 - z = a.length() STEP 4 - x= 0

STEP 5 - FROM y =0 to y<z REPEAT STEP 6 STEP 6 - IF (a.charAt(y)==' ' ) then x =x+1

STEP 7 - PRINT "Number of words in string ="+(x+1) STEP 8 - END

Solution:

class p45 {

public static void main(String args[]) {

String a="Computer Applications"; //initialising string System.out.println("The string is -"+a);

int z=a.length(),y,x=0;

for(y=0;y<z;y++)//loop for counting number of spaces {

if(a.charAt(y)==' ') x=x+1;

}

System.out.println("Number of words in string ="+(x+1));

} }

Output:

The string is -Computer Applications Number of words in string =2

PROGRAM 24 - To create a string and replace all vowels with * .

Algorithm :

STEP 1 - START

STEP 2 - a = "Computer Applications"

STEP 3 - x= 0

STEP 4 - FROM z =0 to z<a.length() REPEAT STEP 5

STEP 5 - if(a.charAt(z)=='a'||a.charAt(z)=='e'||a.charAt(z)=='i'||

a.charAt(z)=='o'||

a.charAt(z)=='u') THEN a.setCharAt(z,'*') STEP 6 - PRINT "New String -"+a

STEP 7 - END

Solution:

import java.io.*;

class p48 {

public static void main(String args[]) {

StringBuffer a=new StringBuffer("Computer Applications");

System.out.println("Original String -"+a);

int z=0;

for(z=0;z<a.length();z++)//loop for replacing vowels with "*"

{

if(a.charAt(z)=='a'||a.charAt(z)=='e'||a.charAt(z)=='i'||a.charAt(z)=='o'||

a.charAt(z)=='u') a.setCharAt(z,'*');

}

System.out.println("New String -"+a);

} }

Output:

Original String -Computer Applications

New String -C*mp*t*r Appl*c*t**ns

PROGRAM 2 5 - To create a double-dimensional array of 4*4 subscripts.

Algorithm :

STEP 1- START STEP 2- INPUT a[]

STEP 3- FROM x =0 to x<3 REPEAT STEP 4 STEP 4- FROM y =0 to y<3 REPEAT STEP 5 STEP 5- PRINT (a[x][y]+" "

STEP 6- END

Solution:

class p31 {

public static void main(String args[])throws IOException { int a[][]=new int[3][3], x,y,z;

BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the array");

for(x=0;x<3;x++)//loop for reading the array {

for(y=0;y<3;y++)

{ z=Integer.parseInt(aa.readLine());

a[x][y]=z;

} }

System.out.println("Array -");

for(x=0;x<3;x++)//loop for printing the array {

for(y=0;y<3;y++) {

System.out.print(a[x][y]+" ");

}

System.out.print("\n");

} } }

Output:

Enter the array 1

2 3 4 5 6 7 8 9 Array -1 2 3 4 5 6 7 8 9

PROGRAM 2 6 - To generate sum of all elements of a double dimensional array of 5*5 subscripts

Algorithm :

STEP 1 - START STEP 2 - INPUT a[]

STEP 3 - FROM x =0 to x<5 REPEAT STEP 4 STEP 4 - FROM y =0 to y<5 REPEAT STEP 5 STEP 5 - PRINT (a[x][y]+" "

STEP 6 - FROM x =0 to x<5 REPEAT STEP 7 STEP 7 - FROM y =0 to y<5 REPEAT STEP 8 STEP 8 - Sum=Sum+a[x][y]

STEP 9 - PRINT Sum STEP10 - END

Solution:

class p33 {

public static void main(String args[])throws IOException { int a[][]=new int[5][5];

BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));

int x,y,z,Sum=0;

System.out.println("Enter the array");

for(x=0;x<5;x++)//loop for reading array

for(x=0;x<5;x++)//loop for printing array {

for(x=0;x<5;x++)//loop for printing sum of array elements {

System.out.println("Sum of Array elements="+Sum);

}

4 3 2 1 2 3 4 5 6 Array -1 2 3 4 5 6 7 8 8 9 0 9 8 7 6 5 4 3 2 1 2 3 4 5 6

Sum of Array elements=118

PROGRAM 2 7 - To generate product of two arrays of 5 subscripts as a third array.

Algorithm :

STEP 1 - START

STEP 2 - INPUT a[] , b[]

STEP 3 - FROM i =0 to i<5 REPEAT STEP 4 and STEP 5 STEP 4 - c[i] = a[i] * b[i]

STEP 5 - PRINT c[i]

STEP 6 - END

Solution:

class p35 {

public static void y(int a[],int b[]) { int c[]=new int[5];

int i;

System.out.println("Product of two arrays is-");

for(i=0;i<5;i++)//loop for finding product of the two arrays { c[i]=a[i]*b[i];

System.out.print(+c[i]+" ");

} }

}

Output:

Product of two arrays is-4 6 21 32 25

PROGRAM 2 8 - To find sum of each column of a double dimensional array.

Algorithm :

STEP 1 - START STEP 2 - INPUT a[]

STEP 3 - FROM x =0 to x<4 REPEAT STEP 4 STEP 4 - FROM y =0 to y<4 REPEAT STEP 5 STEP 5 - PRINT (a[x][y]+" "

STEP 6 - FROM x =0 to x<4 REPEAT STEP 7 , STEP 9 and STEP 10 STEP 7 - FROM y =0 to y<4 REPEAT STEP 8

STEP 8 - Sum=Sum+a[x][y] , STEP 9 - PRINT Sum

STEP 10 - Sum = 0 STEP11 - END

Solution:

class p39 {

public static void main(String args[])throws IOException {

int a[][]=new int[4][4];

BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));

int x,y,z,Sum=0;

System.out.println("Enter the array");//reading array for(x=0;x<4;x++)

{

for(y=0;y<4;y++) {

z=Integer.parseInt(aa.readLine());

a[x][y]=z;

} }

System.out.println("Array -");//printing the array in matrix form

for(x=0;x<4;x++)

System.out.println("Sum of column "+(y+1)+" is "+Sum);//printing sum of Sum=0; //column

Sum of column 3 is 24 Sum of column 4 is 24

PROGRAM 2 9 - To find sum of diagonal of a double dimensional array of 4*4 subscripts.

Algorithm :

STEP 1- START STEP 2- INPUT a[]

STEP 3- FROM x =0 to x<4 REPEAT STEP 4 STEP 4- FROM y =0 to y<4 REPEAT STEP 5 STEP 5- PRINT (a[x][y]+" "

STEP 6- FROM x =0 to x<4 REPEAT STEP 7 STEP 7 - Sum=Sum+a[x][y] , y=y+1

STEP 9- PRINT Sum STEP 10 - Sum = 0 STEP11- END

Solution:

class p40 {

public static void main(String args[])throws IOException {

int a[][]=new int[4][4];

BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));

int x,y,z,Sum=0;

System.out.println("Enter the array");

for(x=0;x<4;x++) {

for(y=0;y<4;y++) {

z=Integer.parseInt(aa.readLine());

a[x][y]=z;

} }

System.out.println("Array -");

for(x=0;x<4;x++) {

for(y=0;y<4;y++)

{

System.out.print(a[x][y]+" ");

}

System.out.print("\n");

} y=0;

for(x=0;x<4;x++)//loop for finding sum of diagonal {

Sum=Sum+a[x][y];

y=y+1;

}

System.out.println("Sum of diagonal is " +Sum);

Sum=0;

} }

Output:

Enter the array 1

2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 Array -1 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4

Sum of diagonal is 20

PROGRAM 30 - To calculate the commission of a

salesman as per the following data:

Sales Commission

>=100000 25% of sales 80000-99999 22.5% of sales 60000-79999 20% of sales 40000-59999 15% of sales

<40000 12.5% of sales

Algorithm :

STEP 1 - START STEP 2 - INPUT sales

STEP 3 - IF (sales>=100000) THEN comm=0.25 *sales OTHERWISE GOTO

STEP 4

STEP 4 - IF (sales>=80000) THEN comm=0.225*sales OTHERWISE GOTO

STEP 5

STEP 5 - IF (sales>=60000) THEN comm=0.2 *sales OTHERWISE GOTO

STEP 6

STEP 6 - IF (sales>=40000) THEN comm=0.15 *sales OTHERWISE GOTO

STEP 7

STEP 7 - comm=0.125*sales

STEP 8 - PRINT "Commission of the employee="+comm STEP 9 - END

Solution:

class p14 {

public static void main(String args[])throws IOException {

double sales,comm;

BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));

System.out.println(“Enter sales”);

sales=Double.parseDouble(aa.readLine());//reading sales from the keyboard if(sales>=100000)

comm=0.25*sales;

else

if(sales>=80000) comm=0.225*sales;

else

if(sales>=60000) comm=0.2*sales;

else

if(sales>=40000) comm=0.15*sales;

else

comm=0.125*sales;

System.out.println("Commission of the employee="+comm);

} }

Output:

Enter sales 60000

Commission of employee=12000

Documento similar