Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider a list of single digit numbers. For example, [2,2,5]. Consider the succ

ID: 3798336 • Letter: C

Question

Consider a list of single digit numbers. For example, [2,2,5]. Consider the successors of such lists. The successor of [2,2,5] is [2,2,6]. The successor of [2,3,9,9] is [2,4,0,0]. The successor of [9,9,9,9] is [1,0,0,0,0]. Write a Java program that takes such a list of numbers as input and returns its successor. You cannot express this list as a decimal number and compute its successor as the number may be so large that an overflow occurs. Make your program as short as possible. Submit code in JAVA along with compilation instructions and test scripts.

Explanation / Answer

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Surya
*/
public class successor {
  
    static int [] Successor(int a[])
    {
        int i,k=1;
        for(i=a.length-1;i>=0;i--)//finding successor..
        {
            a[i]=a[i]+k;//storing in list a
            if(a[i]>9)
            {
                k = 1;
                a[i]=a[i]-10;
            }
            else k=0;
        }
        if(k==1)
        {
            int temp[] =new int[a.length+1];
            temp[0]=k;
            for(i=1;i<a.length+1;i++)temp[i]=a[i-1];
            a=temp;
        }
       // printArray(a);
      
        return a;//returning result
      
    }
    static void printArray(int a[])//program to print list..
    {
        int i;
        System.out.print("[ ");
        for(i=0;i<a.length-1;i++)
            System.out.print(a[i]+",");
        System.out.println(a[i]+" ]");
  
    }
  
    public static void main(String argv[])
    {
  
        //tester program...
      
        int a[] = {2,2,5};
        a=Successor(a);//calling method to test
        printArray(a);//showing result
        int b[] = {2,3,9,9};
        b=Successor(b);//calling method
        printArray(b);//showing result
        int c[] = {9,9,9,9};
        c=Successor(c);//METHOD CALING
        printArray(c);
      
  
    }
  
  
  
}

output:-

run:
[ 2,2,6 ]
[ 2,4,0,0 ]
[ 1,0,0,0,0 ]
BUILD SUCCESSFUL (total time: 0 seconds)

//to compile

javac successor.java

//to execute

java successor

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote