Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

1. WAP to accept a word and swap the first character with the last.

Sample Input: Julien

Sample Output: nulieJ

import java.util.Scanner;

class Swap1

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("ENTER A WORD");

String o=sc.next();char b;

char a[]=new char[o.length()];

for(int i=0;i<o.length();i++)

a[i]=o.charAt(i);

b=a[0];

a[0]=a[o.length()-1];

a[o.length()-1]=b;

for(int j=0;j<o.length();j++)

System.out.print(a[j]);

Write a program to accept a multi digit number and remove the occurrence of second

similar digit in the number and print the new number so formed. If there are no similar

digits, then the number, input, should be the output.

Example: Input: 2452 Input: 2164

O/P: 245 O/P: 2164

import java.util.Scanner;

class Second2

{
public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("ENTER A NUMBER");

String o="";

o= o + sc.nextInt();

char ch;int q;String e="";int k=0;

for(int i=k;i<o.length();i++)

ch=o.charAt(i);

for(int j=i+1;j<o.length();j++)

if(ch==o.charAt(j))

q=j;

for(int p=0;p<o.length();p++)

{ if(p!=q)

e=e+o.charAt(p);

o="";

o=e;

e="";

k=0;

break;

}
}

System.out.println(o);

3. Write a program to accept a sentence and print the alternate words in reverse order.

Sample Input: This is my school Sample Output: This si my loohcs OR sihT is ym school.

import java.util.Scanner;

class Alter3

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("ENTER A SENTENCE");

String o=sc.nextLine()+" ";

String rev="",wrd="",wrd1="";int f=0;int c=0;

for(int i=0;i<=o.length()-1;i++)

if(o.charAt(i)!=' ')

wrd=wrd+o.charAt(i);

wrd1=o.charAt(i)+wrd1;

else{

f++;

if(f%2==0)

rev=rev+wrd1+" ";

else

rev=rev+wrd+" ";
wrd="";

wrd1="";

System.out.println(rev);

Write a program to accept a name and print the last name along the previous words in

abbreviated form. Sample Input: Subhash Chandra Bose. Sample Output: Mr.Bose. S.C.

import java.util.Scanner;

class Abbreviation4

public static void main()

Scanner sc = new Scanner(System.in);

System.out.println("NTER A SENTENCE");

String a = sc.nextLine()+" ",d="",r="";int t=0,p=0;

for(int i=0;i<=a.length()-1;i++)

if(a.charAt(i)==' ')

t++;

for(int j=0;j<=a.length()-1;j++)

if(a.charAt(j)!=' ')

d=d+a.charAt(j);
}

else

p++;

if(p==t)

r=d+r;

else

r=r+"."+d.charAt(0);

d="";

System.out.println(r+".");

Write a menu driven program to do the following: a) Accept n multidigit numbers and print

only those numbers which are prime. b) Accept n words and print only those words whose

last character is a vowel and first character is not a vowel.

import java.util.Scanner;

class Menu5

public static void main()

Scanner sc = new Scanner(System.in);

System.out.println(" enter a choice ");

char a =Character.toUpperCase(sc.nextLine().charAt(0));int b=0;

switch(a)

case 'A':
System.out.println("enter a limit");

int n=sc.nextInt();

for(int i=1;i<=n;i++)

System.out.println("enter a no");

int x=sc.nextInt();

for(int j=1;j<=x;j++)

if(x%j==0)

b++;

if(b==2)

System.out.println(x);

break;

case 'B':

System.out.println("enter a limit");

int y=sc.nextInt();

for(int i=1;i<=y;i++)

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

String z=sc.next();

char f = z.charAt(0);

char l= z.charAt(z.length()-1);

if((l=='a'||l=='e'||l=='i'||l=='o'||l=='u'||l=='A'||l=='E'||l=='I'||l=='O'||l=='U')&&(f!='a'||f!='e'||f!='i'||f!='o'||f!='u'||f!='A'||f!=
'E'||f!='I'||f!='O'||f!='U'))

System.out.println(z);

break;
}

6. Design a class according to the following specifications:

Class Name: Merge

Data Members:

int p – To assign the first integer

int q – To assign the second integer

int res – To assign the resultant integer after merging

int x - To assign the number of digits in the first integer

int y - To assign the number of digits in the second

Member Methods:

Merge() - Default Constructor

void accept() - To accept two integers

void merge() -To merge the individual digits of the two integers as shown in the

example: If p = 1234 and q = 56438 then res = 152634438

void display() - To display the resultant integer

Write the main function also to create an object and call the member methods�

import java.util.Scanner;

class Merge

{ int p,q,res,x=0,y=0;

public void accept()

Scanner sc = new Scanner(System.in);

System.out.println("enter two numbers");

p=sc.nextInt();

q=sc.nextInt();

}
public void merge()

String a="",b="",c="";int c1=p,c2=q;

while(c1>0)

x++;

c1=c1/10;

while(c2>0)

y++;

c2=c2/10;

if(p>q){

a=a+p;

b=b+q;

else{

a=a+q;

b=b+p;

for(int i=0;i<a.length();i++)

if(i<b.length())

c=c+b.charAt(i);

c=c+a.charAt(i);

}
res=Integer.parseInt(c);

public void display()

System.out.println("THE MERGED NUMBER IS:"+res);

public static void main()

Merge ob=new Merge();

ob.accept();

ob.merge();

ob.display();

7. Wap to accept N integers in an array and print the lowest integer with its index.

import java.util.Scanner;

public class Lowest7

public static void main()

Scanner sc = new Scanner(System.in);

System.out.println("enter a limit");

int n = sc.nextInt();

int a[]=new int[n];

int check,index;

for(int i=0;i<=n-1;i++)

System.out.println("enter a number");

a[i]=sc.nextInt();
}

check=a[0];

index=0;

for(int y=0;y<=n-1;y++)

if(check>a[y])

{check=a[y];

index=y;

System.out.println("THE LOWEST NUMBER IS:"+check);

System.out.println("THE INDEX IS:"+(index+1));

8. Wap to accept N words in an array in ascending order and search for a particular word

using Binary Search technique.

import java.util.Scanner;

public class Binary8

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("ENTER A LIMIT");

int n= sc.nextInt();

String s[]=new String[n];

int m=0,m1=n,h;

for(int i=0;i<n;i++)

System.out.println("ENTER A WORD IN ASCENDING ORDER");


s[i]=sc.next();

System.out.println("ENTER A WORD TO CHECK");

String check=sc.next();

for(int j=m;j<m1;j++)

h=j+((m1-j)/2);

if(s[h].equalsIgnoreCase(check))

System.out.println("THE WORD IS PRESENT");

break;

else if(s[h].charAt(0)>check.charAt(0))

j=0;

m1=h;

else

j=h;

m1=n;

if(h==n-1)

System.out.println("THE WORD IS NOT PRESENT");

break;

}
}

9. Write a program with the following overloaded methods:

a) void manip(String s, int n): To print the same word after replacing the duplicating

characters if n is odd else print the piglatin form of the word.

b) void manip(String s1, String s2): To print the sentence which has less number of words in

it.�

import java.util.Scanner;

public class Overload9

public void mainip(String s,int n)

String m="",a="",b="";char ch;

if(n%2!=0)

for(int i=0;i<s.length();i++)

if(m.indexOf(s.charAt(i))==-1)

m=m+s.charAt(i);

System.out.println("THE NEW WORD IS:"+m);

else

for(int y=0;y<s.length();y++)

{ ch=s.charAt(y);

if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'){

a=s.substring(y,s.length());

break;

}
else

b=b+ch;

m=m+a+b+"AY";

System.out.println("THE NEW WORD IS:"+m);

public void mainip(String s1,String s2)

s1=s1+" ";

s2=s2+" ";

int a=0,b=0;

for(int i=0;i<s1.length();i++)

if(s1.charAt(i)==' ')

a++;

for(int y=0;y<s2.length();y++)

if(s2.charAt(y)==' ')

b++;

if(a>b)

System.out.println(s2);

else if(b>a)

System.out.println(s1);

else

System.out.println("THE NUMBER OF WORDS ARE EQUAL");


}

public static void main()

Scanner sc = new Scanner(System.in);

System.out.println("enter two sentences");

String x=sc.nextLine();

String p=sc.nextLine();

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

String str=sc.next().toUpperCase();

System.out.println("enter a number");

int n=sc.nextInt();

Overload9 ob = new Overload9();

ob.mainip(str,n);

ob.mainip(x,p);

Wap to accept a multidigit number and print the difference between the largest and the

smallest number formed after rearranging the digits of that multidigit number using the

method public static void rearrange( int n ).

Sample Input: 2546

Sample Output: Highest number formed after rearranging: 6542

Lowest number formed after rearranging: 2456

Difference: 4086

import java.util.Scanner;

public class Rearrange10

public static void rearrange(int n)

String str=""+n,h="",g="";
char a[]=new char[str.length()];

char b[]=new char[str.length()];

int m=0,l=0;

for(int i=0;i<str.length();i++)

a[i]=str.charAt(i);

b[i]=str.charAt(i);

while(true)

m=0;

l=0;

for(int j=0;j<str.length()-1;j++)

char ch=a[j];

char ch1=b[j];

if(a[j]>a[j+1])

a[j]=a[j+1];

a[j+1]=ch;

if(b[j]<b[j+1])

b[j]=b[j+1];

b[j+1]=ch1;

}
for(int k=0;k<str.length()-1;k++)

if(a[k]<a[k+1])

m++;

if(b[k]>b[k+1])

l++;

if(l==str.length()-1&&m==str.length()-1)

break;

for(int p=0;p<str.length();p++)

h=h+b[p];

g=g+a[p];

int highest=Integer.parseInt(h);

int lowest=Integer.parseInt(g);

int diff=highest-lowest;

System.out.println("THE DIFFERENCE IS:"+diff);

public static void main()

Scanner sc= new Scanner(System.in);

System.out.println("Enter a no");

int q=sc.nextInt();
Rearrange10.rearrange(q);

11. WAP to accept a multi digit number and print the sum of the squares of each digit using

the method public void squareit( int x ).�

import java.util.Scanner;

public class Square11

public void squareit(int x)

int a=x*x;

System.out.println("THE SQUARE IS: "+a);

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("ENTER A NUMBER");

int b=sc.nextInt();

Square11 ob = new Square11();

while(b>0)

ob.squareit(b%10);

b=b/10;

12. WAP to print the following pattern:

class Pattern12
{

public static void main()

int c=50;

for(int i=1;i<=5;i++)

for(int j=1;j<=c;j++)

System.out.print(" ");

for(int k=1;k<=i;k++)

if(k%2!=0)

System.out.print(" * ");

else

System.out.print(" # ");

System.out.println();

c=c-5;

13. WAP to print the sum of the following series up to n terms :

S = S + 1/ x2 + 2 / x3 + 3 / x4 +….N

import java.util.Scanner;

public class Series13

public static void main()

{
Scanner sc = new Scanner(System.in);

System.out.println("ENTER A LIMIT ");

int n= sc.nextInt();

System.out.println("ENTER A NUMBER");

int x= sc.nextInt();

int s=0;

for(int i=1;i<=n;i++)

s=s+(int)(i/(Math.pow(x,i+1)));

System.out.println("THE SUM OF THE SERIES IS:"+s);

14 WAP to accept a multi digit number and form a new integer by concatenating the squares of

the digit at odd places and square root of digit at even places by the method public int catcon (

int x ). Write the main method also to create an object and call the method.

Sample Input: 1453 Sample Output: 3451

import java.util.Scanner;

public class Concatenation14

public int catcon(int x)

String s="";int n=1;

while(x>0)

if(n%2==0)

s=(int)(Math.sqrt(x%10))+s;

else

s=(int)(Math.pow(x%10,2))+s;
x=x/10;

n=n+1;

int z= Integer.parseInt(s);

return z;

public static void main()

Scanner sc =new Scanner(System.in);

System.out.println("ENTER A NUMBER");

int a = sc.nextInt();

Concatenation14 ob=new Concatenation14();

int b=ob.catcon(a);

System.out.println("THE CONCATENATION IS: "+b);

15. Write a program that, for all positive integers i, j, k and l from 1 through 1000, finds and

prints all combination of i, j, k and l such that i + j + k = l and i < k < l.

class Combination15

public static void main()

int l=0;

for(int i=1;i<=1000;i++)

for(int j=2;j<=1000;j++)

for(int k=3;k<=1000;k++)

{ if(i<j&&j<k)
{

l=i+k+j;

System.out.println("THE COMBINATIONS ARE "+i+"+"+j+"+"+k+"="+l);

else

continue;

ANKIT KUMAR SINGH

You might also like