How to Get the Digits of a Number Without Converting It to a String/ Char Array

How to get the digits of a number without converting it to a string/ char array?

The following prints the digits in order of ascending significance (i.e. units, then tens, etc.):

do {
int digit = n % 10;
putchar('0' + digit);
n /= 10;
} while (n > 0);

Get individual digits from an Int without using strings?

You could do something like this:

int ith_digit(int n, int i) {
return (int) (n / pow(10, i)) % 10;
}

We can get the ith digit by reducing the number down to a point where that digit we want becomes in the one's place, example:

Let's say you wanted the third digit in 12345, then by reducing it to 123 (by dividing it by 10 i number of times) we can then take the remainder of that number divided by ten to get the last digit, which is the digit we wanted.

Displaying the digits of an integer without using a reverse number or arrays

So, through a lot of stumbling around, I managed to make my code work the way it was intended using only the knowledge I was allowed to use. Here's the finished product (unless someone sees any huge errors):

    //initializing variables, of course
int userinput, number1, number2, numbersum;
int div = 1;
float number3;

//displaying instructions to the user
printf("Please input a number: ");
scanf("%d", &userinput);

printf("You have entered: ");

if (userinput < 0)
{
printf("Negative ");
userinput = -userinput;
}

//the variables number1-3 are for the data analysis at the end
//I am preserving the original input in them so I can mutilate it in the following step
number1 = userinput;
number2 = userinput;

while (div <= userinput)
{
div = div*10;
}

do
{
if (userinput != 0)
{
div = div/10;

switch (userinput/div)
{
case 0:
{
printf("Zero ");
break;
}
case 1:
{
printf("One ");
break;
}
case 2:
{
printf("Two ");
break;
}
case 3:
{
printf("Three ");
break;
}
case 4:
{
printf("Four ");
break;
}
case 5:
{
printf("Five ");
break;
}
case 6:
{
printf("Six ");
break;
}
case 7:
{
printf("Seven ");
break;
}
case 8:
{
printf("Eight ");
break;
}
case 9:
{
printf("Nine ");
break;
}
default:
{
break;
}

}

userinput = userinput%div;

}

else
{
printf("Zero");
}

} while (userinput > 0);

//line break to make it look pretty
printf("\n");

//boring math to determine the sum of the digits
//assuming all are positive due to know contrary instructions
//set equal to zero since this variable refers to itself in the following function
numbersum = 0;

while (number1 > 0)
{
numbersum = numbersum + (number1 % 10);
number1 = number1 / 10;
}

//nested switch in if statement to print english if digits less than or equal to 10
if (numbersum <= 10)
{
switch (numbersum)
{
case 0:
{
printf("The sum of the individual integers is: Zero");
break;
}
case 1:
{
printf("The sum of the individual integers is: One");
break;
}
case 2:
{
printf("The sum of the individual integers is: Two");
break;
}
case 3:
{
printf("The sum of the individual integers is: Three");
break;
}
case 4:
{
printf("The sum of the individual integers is: Four");
break;
}
case 5:
{
printf("The sum of the individual integers is: Five");
break;
}
case 6:
{
printf("The sum of the individual integers is: Six");
break;
}
case 7:
{
printf("The sum of the individual integers is: Seven");
break;
}
case 8:
{
printf("The sum of the individual integers is: Eight");
break;
}
case 9:
{
printf("The sum of the individual integers is: Nine");
break;
}
case 10:
{
printf("The sum of the individual integers is: Ten");
}
default:
{
break;
}

}

printf("\n");
}

//else if greater than 10, just print the decimal number
else
{
printf("The sum of the individual digits in the integer is: %d\n", numbersum);
}

if (numbersum == 0)
{
printf("The average is of zero is not a number.\n");
}

else
{

//initializing a variable here because it's totally irrelevant to the above functions
//and this feels cleaner because of it. I'm not sure if this is bad etiquette
int i;

//picks out the number of digits in the input and effectively sets i to that number
for (i = 0; number2 > 0; i++)
{
number2 = number2/10;
}

//this is necessary for turning number3 into an actual floating point, not an int stored as float
number3 = numbersum;

//math to determine average (sum of digits divided by number of digits)
number3 = number3 / i;

printf("The average is: %.2f\n", number3);

}

return 0;

It's big and it's probably kind of sloppy (due to how new I am), but it works and that's all that really matters to me right now.

Thanks for the help, guys.

How to convert number into character array in java without using any java inbuilt function

This is the general idea of the algorithm, modify it to create a char array instead of printing the results:

void printNumber(int n) {
if (n < 10)
System.out.println(n);
else {
printNumber(n / 10);
System.out.println(n % 10);
}
}

I wrote it using a recursive algorithm, because I fancy recursion ... but it's trivial to convert this to an iterative solution.

How can I safely and quickly extract digits from an int?

Using sprintf should be fine. sizeof type * 3 * CHAR_BIT / 8 + 2 is a sufficiently large buffer for printing an integer of type type. You can simplify this expression if you assume CHAR_BIT is 8 or if you only care about unsigned formats. The basic idea behind it is that each byte contributes at most 3 digits in decimal (or octal), and you need space for a sign and null termination.



Related Topics



Leave a reply



Submit