Reputation: 6143
I am trying to write a c program when I input an integer using scanf
and split that integer number and print it line by line without an array. I can show you example how I am going to do that.
123 / 100 = 1
123 % 100 = 23
23 / 10 = 2
23 % 19 = 3
1
2
3
I know how to do that but the problem is when I run this code .
# include <stdio.h>
# include <string.h>
# include <math.h>
int main (void)
{
int no, a;
int count, new;
int newNum = 0;
printf("Enter an intger number = ");
scanf("%d", &no);
newNum = no;
printf("You entered = %d\n", newNum);
while(newNum != 0){
newNum = newNum / 10;
count++;
}
count--;
count = pow(10, count);
printf("Power of ten = %d\n", count);
while(count != 1){
new = no / count;
no = no % count;
printf("%d\n", new);
count = count / 10;
}
return 0;
}
Output:
Enter an intger number = 123
You entered = 123
Power of ten = -2147483648
0
0
0
0
0
0
0
0
-5
-9
Floating point exception (core dumped)
Problem is power of ten line did not output right value But If I comment second while loop section.
# include <stdio.h>
# include <string.h>
# include <math.h>
int main (void)
{
int no;
int count, new;
int newNum = 0;
printf("Enter an intger number = ");
scanf("%d", &no);
newNum = no;
printf("You entered = %d\n", newNum);
while(newNum != 0){
newNum = newNum / 10;
count++;
}
count--;
count = pow(10, count);
printf("Power of ten = %d\n", count);
// while(count != 1){
// new = no / count;
// no = no % count;
// printf("%d\n", new);
// count = count / 10;
// }
return 0;
}
Output:
Enter an intger number = 123
You entered = 123
Power of ten = 100
This time Power of ten shows the correct value.
What can I do to avoid this issue?
and
Upvotes: 2
Views: 1176
Reputation: 6143
In my program I have set my integer variables equal to 0 like this.
int no, a = 0;
int count = 0, new = 0;
int newNum = 0;
After that my program works perfectly, but didn't get the full output. That problem came from this section
while(count != 1){
new = no / count;
no = no % count;
printf("%d\n", new);
count = count / 10;
}
Output -:
Enter an intger number = 123
You entered = 123
Power of ten = 100
1
2
You can see 3
didn't in output section. There for I have add extra line outside of the while loop to get my expected output. I have add a following.
while(count != 1){
new = no / count;
no = no % count;
printf("%d\n", new);
count = count / 10;
}
printf("%d\n", no);
Output -:
Enter an intger number = 123
You entered = 123
Power of ten = 100
1
2
3
Now it is completed.
Upvotes: 1
Reputation: 310990
For starters you should be caution and not to make an overflow calculating the power of 10.
Always test your program for boundary values as for example INT_MAX
or UINT_MAX
.
There is no need to use mathematical functions.
Here you are.
#include <stdio.h>
int main( void )
{
while ( 1 )
{
const unsigned int Base = 10;
printf( "Enter a non-negative number (0 - exit): " );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
unsigned int divisor = 1;
for ( unsigned int tmp = n; !( tmp < Base ); tmp /= Base )
{
divisor *= Base;;
}
printf( "%u\n", n / divisor );
for ( ; divisor != 1; divisor /= Base )
{
printf( "%u\n", n % divisor / ( divisor / Base ) );
}
putchar( '\n' );
}
return 0;
}
The program output might look like
Enter a non-negative number (0 - exit): 1
1
Enter a non-negative number (0 - exit): 10
1
0
Enter a non-negative number (0 - exit): 123456789
1
2
3
4
5
6
7
8
9
Enter a non-negative number (0 - exit): 4294967295
4
2
9
4
9
6
7
2
9
5
Enter a non-negative number (0 - exit): 0
Another approach is to use a recursive function as shown here
#include <stdio.h>
void output_digits( unsigned int n )
{
const unsigned int Base = 10;
unsigned int digit = n % Base;
if ( ( n /= Base ) != 0 ) output_digits( n );
printf( "%u\n", digit );
}
int main( void )
{
while ( 1 )
{
printf( "Enter a non-negative number (0 - exit): " );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
output_digits( n );
putchar( '\n' );
}
return 0;
}
Upvotes: 2
Reputation: 427
You want to print each digit of a base10 number. The number of digit can be retrieved with this simple formula
int n_digit = (int)log10(x) +1;
To test it: (int)log10(99) +1 = (int)1.9956 +1 = 2
and (int)log10(100) +1 = (int)2 +1 = 3
So you want to print the quotients of each power of 10:
for(int i=n_digit-1; i>=0; i--)
{
// Calc 10^i without pow function
int num_10_pow_i = 1;
for(int j=0; j<i; j++)
num_10_pow_i *=10;
printf("N[%d]=",i);
printf("%d\n", (int)(x/num_10_pow_i));
x = x-((int)(x/num_10_pow_i) * num_10_pow_i);
}
Please don't use the pow
function working with integers.
Read here for more info: Does pow() work for int data type in C?
Upvotes: 1
Reputation: 16223
In your code variable count
has local scope (automatic storage duration).
Local scope variables are not initialized due to their stack allocation.
int count=0;
(C99 standard) section 6.7.8 clause 10:
If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.
If an object that has static storage duration is not initialized explicitly, then:
- if it has pointer type, it is initialized to a null pointer;
- if it has arithmetic type, it is initialized to (positive or unsigned) zero;
- if it is an aggregate, every member is initialized (recursively) according to these rules;
- if it is a union, the first named member is initialized (recursively) according to these rules.
Note: you should avoid to use new
as a name of a variable.
Upvotes: 3