Program to Swap First and Last Digit of a Number using For loop

Program to Swap First and Last Digit of a Number using For loop

  • Write a program to Swap First and Last Digit of a Number using For loop in C
  • Write a program to Swap First and Last Digit of a Number using For loop in C++
  • Write a program to Swap First and Last Digit of a Number using For loop in Python
  • Write a program to Swap First and Last Digit of a Number using For loop in PHP
  • Write a program to Swap First and Last Digit of a Number using For loop in Java
  • Write a program to Swap First and Last Digit of a Number using For loop in Java Script
  • Write a program to Swap First and Last Digit of a Number using For loop in C#

Explanation:

To swap the first and last digits of a number using a for loop, follow this approach:

Steps:

  1. Extract the last digit: Use the modulo operation (n % 10).
  2. Extract the first digit: Repeatedly divide the number by 10 until only one digit remains.
  3. Remove the first and last digits: Construct a new number by removing the first and last digits and place the swapped digits in their respective positions.
  4. Rebuild the number: Create a new number by placing the last digit at the front and the first digit at the end.

Logic:

  1. Take input for the number (n).
  2. If the number has only one digit, there’s no need to swap.
  3. Get the last digit using n % 10.
  4. Get the first digit by dividing the number by 10 repeatedly until the result is less than 10.
  5. Remove the first and last digits from the number.
  6. Rebuild the number with the first and last digits swapped.

Program to Swap First and Last Digit of a Number using For loop

#include <stdio.h>
#include <math.h>

int main() {
    int num, firstDigit, lastDigit, digits, swappedNum;
    printf("Enter a number: ");
    scanf("%d", &num);

    // Get last digit
    lastDigit = num % 10;

    // Get the number of digits in the number
    digits = (int)log10(num);

    // Get the first digit
    firstDigit = num / (int)pow(10, digits);

    // Swap first and last digits
    swappedNum = num - firstDigit * (int)pow(10, digits);  // Remove the first digit
    swappedNum = swappedNum + lastDigit * (int)pow(10, digits);  // Add the last digit in the first place
    swappedNum = swappedNum - lastDigit + firstDigit;  // Replace the last digit with the first digit

    printf("Number after swapping first and last digits: %d\n", swappedNum);
    return 0;
}

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int num, firstDigit, lastDigit, digits, swappedNum;
    cout << "Enter a number: ";
    cin >> num;

    // Get last digit
    lastDigit = num % 10;

    // Get the number of digits in the number
    digits = (int)log10(num);

    // Get the first digit
    firstDigit = num / (int)pow(10, digits);

    // Swap first and last digits
    swappedNum = num - firstDigit * (int)pow(10, digits);  // Remove the first digit
    swappedNum = swappedNum + lastDigit * (int)pow(10, digits);  // Add the last digit in the first place
    swappedNum = swappedNum - lastDigit + firstDigit;  // Replace the last digit with the first digit

    cout << "Number after swapping first and last digits: " << swappedNum << endl;
    return 0;
}

num = int(input("Enter a number: "))

# Get last digit
last_digit = num % 10

# Get the number of digits in the number
digits = len(str(num))

# Get the first digit
first_digit = int(str(num)[0])

# Swap first and last digits
swapped_num = num - first_digit * (10 ** (digits - 1))  # Remove the first digit
swapped_num = swapped_num + last_digit * (10 ** (digits - 1))  # Add the last digit in the first place
swapped_num = swapped_num - last_digit + first_digit  # Replace the last digit with the first digit

print("Number after swapping first and last digits:", swapped_num)

<?php
$num = (int)readline("Enter a number: ");

// Get last digit
$last_digit = $num % 10;

// Get the number of digits in the number
$digits = strlen((string)$num);

// Get the first digit
$first_digit = (int)(strval($num)[0]);

// Swap first and last digits
$swapped_num = $num - $first_digit * pow(10, $digits - 1);  // Remove the first digit
$swapped_num = $swapped_num + $last_digit * pow(10, $digits - 1);  // Add the last digit in the first place
$swapped_num = $swapped_num - $last_digit + $first_digit;  // Replace the last digit with the first digit

echo "Number after swapping first and last digits: $swapped_num\n";
?>

import java.util.Scanner;

public class SwapDigits {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = scanner.nextInt();

        // Get last digit
        int lastDigit = num % 10;

        // Get the number of digits in the number
        int digits = (int) Math.log10(num);

        // Get the first digit
        int firstDigit = num / (int) Math.pow(10, digits);

        // Swap first and last digits
        int swappedNum = num - firstDigit * (int) Math.pow(10, digits);  // Remove the first digit
        swappedNum = swappedNum + lastDigit * (int) Math.pow(10, digits);  // Add the last digit in the first place
        swappedNum = swappedNum - lastDigit + firstDigit;  // Replace the last digit with the first digit

        System.out.println("Number after swapping first and last digits: " + swappedNum);
        scanner.close();
    }
}

let num = parseInt(prompt("Enter a number:"));

// Get last digit
let lastDigit = num % 10;

// Get the number of digits in the number
let digits = Math.floor(Math.log10(num));

// Get the first digit
let firstDigit = Math.floor(num / Math.pow(10, digits));

// Swap first and last digits
let swappedNum = num - firstDigit * Math.pow(10, digits);  // Remove the first digit
swappedNum = swappedNum + lastDigit * Math.pow(10, digits);  // Add the last digit in the first place
swappedNum = swappedNum - lastDigit + firstDigit;  // Replace the last digit with the first digit

console.log("Number after swapping first and last digits:", swappedNum);

using System;

class Program {
    static void Main() {
        Console.Write("Enter a number: ");
        int num = int.Parse(Console.ReadLine());

        // Get last digit
        int lastDigit = num % 10;

        // Get the number of digits in the number
        int digits = (int)Math.Log10(num);

        // Get the first digit
        int firstDigit = num / (int)Math.Pow(10, digits);

        // Swap first and last digits
        int swappedNum = num - firstDigit * (int)Math.Pow(10, digits);  // Remove the first digit
        swappedNum = swappedNum + lastDigit * (int)Math.Pow(10, digits);  // Add the last digit in the first place
        swappedNum = swappedNum - lastDigit + firstDigit;  // Replace the last digit with the first digit

        Console.WriteLine($"Number after swapping first and last digits: {swappedNum}");
    }
}

List of All Programs