Write a program to enter 5 numbers in an array. Interchange the first and last digit of each element in the array and print the modified array.

 






Write-a-program-to-enter-5-numbers-in-an-array-Interchange-the-first-and-last-digit-of-each-element

Question asked by Nikita

Thanks for this

SOLUTION
PLS TRY AND THEN SEE IT

import java.util.*;

class interchange

{

    void main()

    {

        Scanner ob=new Scanner(System.in);

        int a[]=new int [5];

        int m,n,s,c,i,first,last,d;

        System.out.println("Enter 5 elements in an array");

        for(i=0;i<5;i++)

        {

            a[i]=ob.nextInt();

        }

        for(i=0;i<5;i++)

        {

            m=n=a[i];

            s=0;

            c=0;

            first=0;

            last=a[i]%10;

            while(n!=0)

            {

                first=n%10;

                n=n/10;

            }

            while(m!=0)

            {

                d=m%10;

                if(d==first)

                s=s+(int)Math.pow(10,c++)*last;

                else if(d==last)

                s=s+(int)Math.pow(10,c++)*first;

                else

                s=s+(int)Math.pow(10,c++)*d;

                m=m/10;

            }

            a[i]=s;

        }

        System.out.println("UPDATED ARRAY IS");

        for(i=0;i<5;i++)

        {

            System.out.println(a[i]+" ");

        }

            }

        }

    EXECUTION TIME-0.02

    STUDENT DIFFICULTY RATING-- 7.3/10;


HAPPY CODING!


Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

It looks like you're using an ad blocker. We rely on ads to keep our site free, please whitelist us!

Click here to continue

Please whitelist our site to continue enjoying our content.

Whitelist | Close