
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
K-th digit in ‘a’ raised to power ‘b’ in C++
In this tutorial, we are going to write a program that finds the k-th digit from the right side in the number ab
It's a straightforward problem. Let's see the steps to solve it.
- Initialise the numbers a, b, and k.
- Find the value of abusing pow method.
- Write a loop that iterates until power value is less than zero or count is less than k.
- Get the last digit from the power value.
- Increment the counter.
- Check whether k and counter are equal or not.
- Return the digit if they are equal
- Return -1.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; int getTheDigit(int a, int b, int k) { int power = pow(a, b); int count = 0; while (power > 0 && count < k) { int rem = power % 10; count++; if (count == k) { return rem; } power /= 10; } return -1; } int main() { int a = 5, b = 6; int k = 3; cout << getTheDigit(a, b, k) << endl; return 0; }
Output
If you run the above code, then you will get the following result.
6
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Questions & Answers
- Convert all substrings of length ‘k’ from base ‘b’ to decimal in C++
- Validate input: replace all ‘a’ with ‘@’ and ‘i’ with ‘!’JavaScript
- Python program to replace first ‘K’ elements by ‘N’
- Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++
- Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time in C++
- Are ‘this’ and ‘super’ keywords in Java?
- ‘AND’ vs ‘&&’ operators in PHP
- ‘this’ keyword in C#
- Count ‘d’ digit positive integers with 0 as a digit in C++
- What is the use of ‘ALL’, ‘ANY’, ’SOME’, ’IN’ operators with MySQL subquery?
- Find (a^b)%m where ‘a’ is very large in C++
- Why in MySQL, we cannot use arithmetic operators like ‘=’, ‘<’ or ‘<>’ with NULL?
- Find all the names beginning with the letter 'a' or ‘b’ or ‘c’ using MySQL query?
- Replacing ‘public’ with ‘private’ in “main” in Java
- How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux
Advertisements