Knowee
Questions
Features
Study Tools

Reverse last k digits of the given numberInput Format:Accept two integer  as inputOutput Format:print the reversed k digits

Question

Reverse last k digits of the given number

Input Format:
Accept two integers as input

Output Format:
Print the reversed k digits

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step solution in Python:

  1. First, we need to accept two integers as input. Let's call the first integer num and the second integer k.
num = int(input("Enter the number: "))
k = int(input("Enter the number of digits to reverse: "))
  1. Next, we need to extract the last k digits from num. We can do this by converting num to a string, slicing the last k characters, and then converting back to an integer.
last_k_digits = int(str(num)[-k:])
  1. Now, we need to reverse these k digits. We can do this by converting last_k_digits to a string, reversing the string using slicing, and then converting back to an integer.
reversed_digits = int(str(last_k_digits)[::-1])
  1. Finally, we need to replace the last k digits of num with reversed_digits. We can do this by subtracting last_k_digits from num, and then adding reversed_digits.
num = num - last_k_digits
num = num + reversed_digits
  1. Print the final result.
print("The number after reversing the last k digits is: ", num)

Please note that this solution assumes that num has at least k digits. If num has fewer than k digits, the program will reverse all the digits of num.

This problem has been solved

Similar Questions

Write a C++ program to find Reverse of a Number using class.inputEnter a number: 12345output54321

Write a program in C to print a string in reverse using a character pointer.Also print the uppercase of the first letter of the reversed word.

Input FormatThe first line of input consist of a Binary numberOutput FormatPrint the decimal equivalent for the given Binary number

Write a program to enter two numbers and print the arithmeticoperations like +,-,*,**, /, // and % using function

Write a program that takes a string of lowercase characters as input and prints it in uppercase, reversed.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.