top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to count the number of digits in numbers in different bases?

+3 votes
372 views

With different bases i.e. 10, 8, 16, etc; I'm trying to count the number of characters in each number.

Example

Number: ABCDEF
Number of digits: 6

Any sample or pointer for C?

posted Apr 24, 2015 by Nikhil Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Here is the program for the above (tested).

#include <stdio.h>

int main()
{
     int num = 0, choice = 0, ret;
     char no[32];
     char frmt[] = {"dox"};

     printf("Entery your choice < 1:desimal > < 2:octal > < 3:hexadecimal > :  ");
     scanf("%d", &choice);

     printf("\nEnter your number : ");

     /*
        After getting the input from user just storing the input number into char array as a string.
     */
     switch (choice) {
         case 1:
             scanf("%d", &num);
             sprintf(no, "%0d", num);
             break;
         case 2:
              scanf("%o", &num);
              sprintf(no, "%0o", num);
              break;
         case 3:
             scanf("%x", &num);
             sprintf(no, "%0x", num);
             break;
         default:
             printf("Invalid choice...\n");
    }
    /*
        The below print will print the input number in respective format i.e decimal/ octal/ hexadecimal.
        and the number of digit for that number.
   */
    printf(" : contains %d digits.\n", printf("%s", no));

    return 0;
}

If you have any question you can ask me :)

answer Apr 27, 2015 by Arshad Khan
Similar Questions
+1 vote

Write a java program to count unique digits in the number, if a given number is 393 it should return 2 since only 3,9 are unique digits?

+3 votes

I want to count the number of bytes in a stream that contains nulls. I know I can't use strlen() for this, is there an alternative?

char *stream = "\x11\x12\x13\x00\x12\x13\x14\x15";
+1 vote

Alex has been asked by his teacher to do an assignment on sums of digits of a number. The assignment requires Alex to find the sum of sums of digits of a given number, as per the method mentioned below.
If the given number is 582109, the Sum of Sums of Digits will be calculated as =
= (5 + 8 + 2 + 1 + 0 + 9) + (8 + 2 + 1 + 0 + 9) + (2 + 1 + 0 + 9) + (1 + 0 + 9) + (0 + 9) + (9)
= 25 + 20 + 12 + 10 + 9 + 9 = 85

Alex contacts you to help him write a program for finding the Sum of Sums of Digits for any given number, using the above method.

Help Alex by completing the login in the given function sumOfSumsOfDigits which takes as input an integer input1 representing the given number.
The function is expected to return the "Sum of Sums of Digits" of input1.

Assumptions: For this assignment, let us assume that the given number will always contain more than 1 digit, i.e. the given number will always be >9.

...