There is a question: I should scan a number with 300 digits and print the sum the digits but I cant scan it with long long int
and I dont know what to do.
CodePudding user response:
You can scan the number as a string with fgets()
or even simply one byte at a time with getchar()
:
#include <stdio.h>
int main() {
int c;
int sum = 0;
while ((c = getchar()) != EOF) {
if (c >= '0' && c <= '9')
sum = c - '0';
else
break;
}
printf("sum: %d\n", sum);
return 0;
}
I you must use scanf()
, here is an alternative:
#include <stdio.h>
int main() {
char buf[2];
int sum = 0;
while (scanf("%1[0-9]", buf) == 1) {
sum = *buf - '0';
}
printf("sum: %d\n", sum);
return 0;
}
CodePudding user response:
#include <stdio.h>
int main()
{
char digit;
int sum = 0;
while ( scanf("%c", &digit) != EOF)
{
sum = digit - '0';
}
printf("sum: %d\n", sum);
return 0;
}
CodePudding user response:
In such a case you need to enter a number as a string.
To enter a number you should use the standard function fgets
. The corresponding character array must have 302 characters: 300 characters for digits, one character for the new line character '\n'
that is appended by the function fgets
to the entered string and one character for the terminating zero character '\0'
of the string.
If the new line character '\n'
is not present in the string it means that the user entered more than 300 characters.
Also the user can enter a number with leading or trailing spaces.
You need to check that the entered string contains a valid number.
Here is a demonstration program.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int is_valid_number( const char *number )
{
while ( isblank( ( unsigned char )*number ) ) number;
int valid = *number != '\0';
if ( valid )
{
while ( *number && '0' <= *number && *number <= '9' ) number;
while ( isblank( ( unsigned char )*number ) ) number;
valid = *number == '\0';
}
return valid;
}
int main( void )
{
enum { N = 300 };
char number[N 2] = { 0 };
printf( "Enter a non-negative number (no greater than %d digits): ", N );
fgets( number, sizeof( number ), stdin );
int valid = strchr( number, '\n' ) != NULL;
char *p = number;
if ( valid )
{
number[ strcspn( number, "\n" ) ] = '\0';
while ( isblank( ( unsigned char )*p ) ) p;
valid = is_valid_number( p );
}
if ( !valid )
{
puts( "Invalid number." );
}
else
{
unsigned int sum = 0;
for ( char *digit = p; *digit; digit )
{
sum = *digit - '0';
}
printf( "The sum of digits = %u\n", sum );
}
}
Its output might look like
Enter a non-negative number (no greater than 300 digits): 1234567890123456789012345678901234567890
The sum of digits = 180
CodePudding user response:
The most straight forward solution seems to be to repeatedly read in 1-digit ints and sum them up while that works.
One digit numbers can easily be read into an int (no long needed) and even the sum of 300 digits will not exceed an int.
#include <stdio.h>
int main()
{
int digit=0;
int sum=0;
while(1==scanf("",&digit))sum =digit;
printf("sum:%d\n", sum);
return 0;
}
This admittedly (thanks chqrlie for pointing out) expect an end of line, as it comes with e.g. test input at online compilers or judges. In an interactive prompt
... a single line of digit will not suffice, an explicit end of file with ^D is needed.
CodePudding user response:
you can use char to scan each digit. also in this way you do not need string.enter image description here