Coding Competition Solution
Coding Competition Solution
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);
}
}
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);
}
}
}
import java.util.*;
class Ans{
for(int i = 0;i<(st.length()/2);i++){
if(st.charAt(i)!=st.charAt(st.length()-1-i)){
pali = false;
break;
}
}
return pali;
}
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);
}
}
import java.util.*;
class Ans{
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);
}
}
Output
Sorted Array in Ascending Order ..
2 13 17 18 23 30 46 52 65 78