1. Take 10 integer inputs from user and store them in an array.
Now, copy all the
elements in an another array but in reverse order.
2. import java.util.*;
3.
4. class Ans{
5. public static void main(String[] args){
6. Scanner s = new Scanner(System.in);
7. int[] a = new int[10];
8. int[] b = new int[10];
9. for(int i =0;i<a.length;i++){
10. System.out.println("Enter the value of a["+i+"]");
11. a[i] = s.nextInt();
12. }
13. int j = 0;
14. for(int i = b.length-1;i>=0;i--){
15. b[i] = a[j];
16. j++;
17. }
18. for(int i = 0; i< b.length; i++){
19. System.out.println("The value of b["+i+"] is "+b[i]);
20. }
21. }
22. }
2Consider an integer array, the number of elements in which is determined by the
user. The elements are also taken as input from the user. Write a program to find
those pair of elements that has the maximum and minimum difference among all
element pairs.
4. Write a program to shift every element of an array to circularly right.
E.g.-
INPUT : 1 2 3 4 5
OUTPUT : 5 1 2 3 4
import java.util.*;
class Ans{
public static void main(String[] args){
int a[] = { 1,2,3,4,5 };
int t = a[4];
for(int i = 4; i>=1; i--)
{
a[i]=a[i-1];
}
a[0]=t;
for(int i = 0; i<=4; i++)
{
System.out.println(a[i]);
}
}
}
5. Input any number. Find the sum of the digits of the number using a
recursive function.
import java.util.*;
class Ans{
public static int sum(int x){
if((x/10) == 0){
return x;
}
else{
return sum(x/10)+(x%10);
}
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int x;
x = s.nextInt();
System.out.println(sum(x));
}
}
7.Write a program that takes your full name as input and displays the abbreviations of the
first and middle names except the last name which is displayed as it is. For example, if your
name is Robert Brett Roser, then the output should be R.B.Roser.
import java.util.*;
class Ans{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
String st = s.nextLine();
String sr = "";
sr = sr+st.charAt(0);
sr = sr+". ";
for (int i = 0; i<st.length();i++){
if(st.charAt(i) == ' ' && st.charAt(i+1)!=' ' && i+1<st.length()){
sr = (sr+st.charAt(i+1)).toUpperCase();
sr = sr+". ";
}
}
String last_wrd = "";
//to get the last word
for(int i = st.length()-1;i>=0;i--){
if(st.charAt(i) == ' '){
last_wrd = st.substring(i+2);
break;
}
}
//to remove last ". "
sr = sr.substring(0,sr.length()-2);
sr = sr+last_wrd;
System.out.println(sr);
}
}
10.Write down the names of 10 of your friends in an array and then sort those
in alphabetically ascending order.
import java.util.*;
class Ans{
public static void main(String[] args){
String[] a =
{"Jack","Jhon","Aman","Brute","Hank","Canny","Lee","Louis","June","Frank"};
for(int i = 0;i<a.length-1;i++){
for(int j = i;j<a.length;j++){
if(a[i].compareTo(a[j])>1){
String temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(String i:a){
System.out.println(i);
}
}
}
11.Input a string which contains some palindrome substrings. Find out
the position of palindrome substrings if exist and replace it by *. (For
example if input string is “bob has a radar plane” then it should convert
in “*** has a ***** plane”.
import java.util.*;
class Ans{
public static boolean isPali(String st){
boolean pali = true;
for(int i = 0;i<(st.length()/2);i++){
if(st.charAt(i)!=st.charAt(st.length()-1-i)){
pali = false;
break;
}
}
return pali;
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
String a = s.nextLine();
int last_index = 0;
String replace = "";
for(int i = 0;i<a.length();i++){
if(a.charAt(i) == ' '){
if(isPali(a.substring(last_index,i))){
for(int j = last_index;j<=i-1;j++){
replace = replace+"*";
}
last_index = i+1;
replace = replace+" ";
}
else{
for(int j = last_index;j<=i-1;j++){
replace = replace+a.charAt(j);
}
last_index = i+1;
replace = replace+" ";
}
}
}
if(isPali(a.substring(last_index,a.length()))){
for(int i = last_index;i<a.length();i++){
replace = replace+"*";
}
}
else{
for(int i = last_index;i<a.length();i++){
replace = replace+a.charAt(i);
}
}
System.out.println(replace);
}
}
Print the multiplication table of 15 using recursion.
import java.util.*;
class Ans{
public static void table(int x, int y){
if(y != 1){
table(x,y-1);
}
System.out.println(x*y);
}
public static void main(String[] args) {
table(15, 10);
}
}
16. Write a program to Sort an array in ascending order using quicksort
public class Ascending_Quicksort
{
static void QuickSort(int a[], int f, int l)
{
int pivot = 0;
int temp = 0;
int i = 0;
int j = 0;
if (f < l)
{
pivot = f;
i = f;
j = l;
while(i < j)
{
while(a[i] <= a[pivot] && i < l)
{
i = i + 1;
}
while(a[j] > a[pivot])
{
j = j - 1;
}
if(i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[pivot];
a[pivot] = a[j];
a[j] = temp;
QuickSort(a, f, j - 1);
QuickSort(a, j + 1, l);
}
}
public static void main(String[] args)
{
int i = 0;
int j = 0;
int t = 0;
int a[] = {2, 65, 23, 13, 18, 30, 46, 17, 52, 78};
QuickSort(a, 0, a.length - 1);
System.out.println("Sorted Array in Ascending Order ..");
i = 0;
while(i < 10)
{
System.out.print(a[i] + " ");
i = i + 1;
}
}
}
Output
Sorted Array in Ascending Order ..
2 13 17 18 23 30 46 52 65 78