Posts

Search

Amdocs Coding Question 2

 Given a range of integers [X,Y] where X and Y both are included. Write a program to find the composite numbers and the count C of numbers in that sub-range. A composite number is a number (except 1) that i snot a prime number.

Constraints:-

1. X, Y>1

2. X<Y

Input Format:-

 A single line of input contains X and Y separated by a single white space .

Output Format:-

Multiple lines of output contain three integers each M,N and C where [M,N] are the numbers and C is the count of numbers in the sub-range. Integers in each output line are separated by a single white space.

Solution:-

import java.util.*;

public class Main

{

    static boolean isPrime(int n) { 

     if (n%2==0) return false; 

    for(int i=3;i<=Math.sqrt(n);i+=2) { 

        if(n%i==0) 

            return false; 

    } 

    return true; 

static void sieveAlg1(int x,int y)

{

    int ar[]=new int[50];

    int co=0,z=0;

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

    {

        boolean c=isPrime(i);

        if(c==false)

        {

            co++;

            ar[z++]=i;

            continue;

        }

        if(co>=7)

        {

            System.out.println(ar[0]+" "+ar[co-1]+" "+co);

            z=0;

            co=0;

        }

        else if(c==true)

        {

            co=0;

            z=0;

        }

   }

    

}

public static void main(String[] args) {

    Scanner sc =new Scanner(System.in);

    int testStart=sc.nextInt();

    int testStop=sc.nextInt();

    sieveAlg1(testStart,testStop);     

}

}


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]);

        }

        }     

}

}

}