1
1
1
import java.util.Scanner;
class Swap1
System.out.println("ENTER A WORD");
String o=sc.next();char b;
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
import java.util.Scanner;
class Second2
{
public static void main()
System.out.println("ENTER A NUMBER");
String o="";
o= o + sc.nextInt();
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
System.out.println("ENTER A SENTENCE");
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
System.out.println("NTER A SENTENCE");
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
import java.util.Scanner;
class Menu5
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;
}
Data Members:
Member Methods:
void merge() -To merge the individual digits of the two integers as shown in the
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;
p=sc.nextInt();
q=sc.nextInt();
}
public void merge()
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);
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;
System.out.println("enter a limit");
int n = sc.nextInt();
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;
8. Wap to accept N words in an array in ascending order and search for a particular word
import java.util.Scanner;
System.out.println("ENTER A LIMIT");
int n= sc.nextInt();
int m=0,m1=n,h;
for(int i=0;i<n;i++)
String check=sc.next();
for(int j=m;j<m1;j++)
h=j+((m1-j)/2);
if(s[h].equalsIgnoreCase(check))
break;
else if(s[h].charAt(0)>check.charAt(0))
j=0;
m1=h;
else
j=h;
m1=n;
if(h==n-1)
break;
}
}
a) void manip(String s, int n): To print the same word after replacing the duplicating
b) void manip(String s1, String s2): To print the sentence which has less number of words in
it.�
import java.util.Scanner;
if(n%2!=0)
for(int i=0;i<s.length();i++)
if(m.indexOf(s.charAt(i))==-1)
m=m+s.charAt(i);
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";
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
String x=sc.nextLine();
String p=sc.nextLine();
String str=sc.next().toUpperCase();
System.out.println("enter a number");
int n=sc.nextInt();
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
Difference: 4086
import java.util.Scanner;
String str=""+n,h="",g="";
char a[]=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("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
import java.util.Scanner;
int a=x*x;
System.out.println("ENTER A NUMBER");
int b=sc.nextInt();
while(b>0)
ob.squareit(b%10);
b=b/10;
class Pattern12
{
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;
S = S + 1/ x2 + 2 / x3 + 3 / x4 +….N
import java.util.Scanner;
{
Scanner sc = new Scanner(System.in);
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)));
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.
import java.util.Scanner;
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;
System.out.println("ENTER A NUMBER");
int a = sc.nextInt();
int b=ob.catcon(a);
15. Write a program that, for all positive integers i, j, k and l from 1 through 1000, finds and
class Combination15
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;
else
continue;