Back to Editorials CodeCraft
  • Github
  • Instagram
< >

Digits

TIME LIMIT = 5 SECS.

  • Given two integers, n and m, how many digits does n^m have?

    Examples:

    2 and 10 - 2^10 = 1024 => 4 digits
    3 and 9 - 3^9 = 19683 => 5 digits
Input Output
The input is composed of several test cases. The first line has an integer C, representing the number of test cases.The following C lines contain two integers N and M
(1 <= N, M <= 100).
For each input test case of your program, you must print an integer containing the number of digits of the result of the calculated power in the respective test case.

Example Test Case
Input              Output
4
1 1
2 10
3 9
100 100
1
4
5
201
Solution

#include <stdio.h> #include <math.h> int main() { long long int n, m, temp, d = 0; /* long long int is used because of the input constraints. 'n' = base 'm' = index 'temp' = temporary values 'd' = number of digits respectively. */ // Accept the base and the index scanf("%lld %lld", & n, & m); // Calculate n^m temp = pow(n, m); //As long as the value of temp does not become equal to zero, keep running the loop. while (temp != 0) { //Keep dividing n^m by 10 temp /= 10; //And increment the counter variable 'd' to count the digits. d++; } //Print the number of digits. printf("%lld", d); return 0; }
  • #1 Squared and cubic
  • #2 Digits
  • #3 Multiple Adder
  • #4 Grains in a Chessboard
  • #5 Age In Days
  • #6 Stay or Go
  • #7 Cipher

Get in touch

  • executives@codingstudio.club
  • +91 9010342360
  • Vignana Bharthi Instute of Technology
    Aushapur, Ghatkesar, Hyderabad, Telengana - India

© coding.Studio();