Posts

Search

Amdocs Coding Question

 Write a program to perform the following operations on a given integer N.

1. Convert all the digits of  N to their character representation (in upper case only) as 1=A,2=B,3=C and so on. If there are any '0' in N, then remove it.

2. Add all digits of N to obtain sum S.

3. If S is odd, print only those character derived by converting odd digits of N. If S is even, print only those characters derived by converting even digits of N. Alphabetical order must be followed in either case.

Constraints:-

N>0

Input Format:-

The input contains an integer N.

Output Format:-

The first line of output contains the characters in alphabetically order.

The second line of output contains the sum S.

The third line of output contains the characters obtained by converting either even or odd numbers, in alphabetical order. In case of absence of any even/odd number in the input integer, the third line of output should not be printed.

Solution:-

import java.util.*;

public class Main

{

public static void main(String[] args) {

    Scanner sc= new Scanner(System.in);

    String a= sc.next();

    char ar[]={'A','B','C','D','E','F','G','H','I','J'};

    int num[]=new int[a.length()];

    int c=0;

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

    {

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

        {

            num[c]=a.charAt(i);

            num[c]=num[c]-48;

            c++;

           // System.out.print(num[c-1]+" ");

        }

    }

    int newn[]=new int[c];

    int newe[]=new int[c];

    int newo[]=new int[c];

    

    int sum=0,e=0,o=0,z=0,y=0;

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

    {

        newn[i]=num[i];

        sum+=newn[i];

        if(newn[i]%2==0)

        {

            newe[z++]=newn[i];

            e++;

        }

        else if(newn[i]%2==1)

        {

            newo[y++]=newn[i];

            o++;

        }

    }

    //System.out.print(e+" "+o);

    int newe1[]=new int[e];

    int newo1[]=new int[o];

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

    {

        newe1[i]=newe[i];

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

        

    }

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

    {

        newo1[i]=newo[i];

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

       

    }

    

    Arrays.sort(newn);

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

    {

        System.out.print(ar[newn[i]-1]);

    }

System.out.println();

System.out.println(sum);

Arrays.sort(newe1);

Arrays.sort(newo1);

if(sum%2==0)

{

    if(e>0)

    {

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

        {

                 System.out.print(ar[newe1[i]-1]);

        }

        }

        

}

else{

    if(o>0)

    {

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

        {

                 System.out.print(ar[newo1[i]-1]);

        }

        }     

}

}

}