Computer Science, asked by archit04a, 11 months ago

wap in java to swap first and last digits of an entered number and display the no. with swapped digits

Answers

Answered by QGP
15

Java Programming

We are asked to write a program in Java to swap the first and last digits of an entered number.

We will first take the user input using Scanner class.

Then, we will find the number of digits in the entered number.

Suppose the number of digits in number num is n.

Then, we will extract the first and last digits of the number. In Java Integer Operations, we can symbolize this as:

\sf\textsf{firstDigit} =quotient \left(\dfrac{num}{10^{n-1}}\right) \\\\\\ lastDigit = remainder\left(\dfrac{num}{10}\right)

Then, we can replace the first and last digits with these operations:

\displaystyle\sf \underline{\textsf{1) Replacing the first digit with original last digit:}} \\\\ num = remainder\left(\frac{num}{10^{n-1}}\right) + lastDigit\times 10^{n-1} \\\\\\ \underline{\textsf{2) Replacing the last digit with the original first digit}} \\\\ num = quotient\left(\frac{num}{10}\right) \\\\\\ \rightarrow num = num\times 10 + firstDigit

Here's the Program Code:

Program Code

import java.util.Scanner;

public class DigitSwap

{

   public static void main(String[] args)

   {

       Scanner sc = new Scanner(System.in);

       

       System.out.print("Enter a number: ");

       int num = sc.nextInt();     //Taking User Input

       

       int n = 0;      //Number of Digits

       int num2 = num;  //Another variable storing number

       

       //This loop will help to find number of digits

       //We keep on dividing num2 by 10, and do n++ till num2 becomes 0

       

       while(num2>0)  

       {

           num2 /= 10;

           n++;

       }

       

       //Extracting First Digit

       //First digit is just num/10^(n-1)

       int firstDigit = num/(int)Math.pow(10,n-1);

       

       //Extracting Last Digit

       //Last digit is simply num/10

       int lastDigit = num%10;

       

       //Now we take remainder of num divided by 10^(n-1), and add lastDigit*10^(n-1)

       num = (num%(int)Math.pow(10,n-1))+(lastDigit*(int)Math.pow(10,n-1));

       //Now, we have replaced the first digit with the original last digit

       

       //Divide the number to 10 to remove last digit

       //Integer Data type makes sure that we only get quotient

       num = num/10;

       

       //Now, multiply num with 10 and simply add original first digit

       num = num*10+firstDigit;

       

       System.out.println("Number with swapped digits: "+num); //Printing final number

   

       

   }

}

Attachments:
Answered by atrs7391
3

package com.company;

/* Write a program to input two unequal numbers. Display the numbers after swapping their values in  the variables without using a third variable.

Sample Input: a=23, b=56

Sample Output: a=56, b=23

*/

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {

       Scanner sc = new Scanner (System.in);

       System.out.println("Enter Number a:");

       int a = sc.nextInt();

       System.out.println("Enter Number b:");

       int b = sc.nextInt();

       if (a!=b){

           a = a+b;

           b = a-b;

           a = a-b;

       }

       System.out.println("a = "+a+"    b = "+b);

   }

}

Similar questions