Determining the range of an unsigned int - c

An assignment is requesting that I qualify input number to ensure that it is within range of an unsigned-int on my machine. How do I determine what that range is? it says that the input needs to be taken in at execution time and that no assumptions can be made about what was entered.
I tried using plugging in different ranges (2^7 , 2^15 , 2^31) but they all resulted in overflow.

To test if an string converts to a unsigned
Use strtoul() to convert to a unsigned long. Yet since that function rolls over negative text to positive integers, use strtol() first.
bool valid_unsigned(const char *s, unsigned *uval) {
char *endptr;
errno = 0;
int base = 0; // Use 10 here if only decimal text acceptable.
long lvalue = strtol(s, &endptr, base);
if (s == endptr) {
*uval = 0;
return false; // No conversion
}
if (lvalue < 0) {
errno = ERANGE; // Perhaps calling code would like to test this.
*uval = 0;
return false; // Negative
}
if ((unsigned long) lvalue <= UINT_MAX && errno == 0) {
*uval = (unsigned) lvalue;
return true; // Success
}
#if UINT_MAX > LONG_MAX
// Still could be a value in the LONG_MAX...UINT_MAX range.
errno = 0;
unsigned long uvalue = strtoul(s, &endptr, base);
if (uvalue <= UINT_MAX && errno == 0) {
*uval = (unsigned) uvalue;
return true; // Success
}
#endif
errno = ERANGE;
*uval = UINT_MAX;
return false; // Too big.
}
To do: Test trailing text.

If you are using the function strtoul for converting the string input to an integer, then you don't need to determine yourself whether the input is larger than ULONG_MAX. The function strtoul will report this, by setting the value of errno accordingly. However, depending on the platform, an unsigned long may be able to represent more numbers than an unsigned int. Therefore, after the function strtoul reports that the input range is ok, you should additionally verify that the number is not larger than UINT_MAX.
Another problem with using the function strtoul is that it will accept negative numbers as valid. We must therefore check ourselves whether the first non-whitespace character is a minus sign.
Here is an example program which uses a function get_unsigned_int_from_user which will continue prompting the user for input, until the input is valid and in the range of an unsigned int:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <errno.h>
unsigned int get_unsigned_int_from_user( const char *prompt )
{
for (;;) //loop forever until user enters a valid number
{
char buffer[1024], *p, *q;
unsigned long ul;
//prompt user for input
fputs( prompt, stdout );
//get one line of input from input stream
if ( fgets( buffer, sizeof buffer, stdin ) == NULL )
{
fprintf( stderr, "unrecoverable error reading from input\n" );
exit( EXIT_FAILURE );
}
//make sure that entire line was read in (i.e. that
//the buffer was not too small)
if ( strchr( buffer, '\n' ) == NULL && !feof( stdin ) )
{
int c;
printf( "line input was too long!\n" );
//discard remainder of line
do
{
c = getchar();
if ( c == EOF )
{
fprintf( stderr, "unrecoverable error reading from input\n" );
exit( EXIT_FAILURE );
}
} while ( c != '\n' );
continue;
}
//make "p" point to first non-whitespace character
for ( p = buffer; isspace( (unsigned char)*p ); p++ )
;
//since the function "strtoul" accepts negative
//input as valid input, which we don't want, we must
//first check ourselves whether the input starts
//with a minus sign
if ( *p == '-' )
{
printf( "number must not be negative!\n" );
continue;
}
//attempt to convert string to number
errno = 0;
ul = strtoul( p, &q, 10 );
if ( q == p )
{
printf( "error converting string to number\n" );
continue;
}
//make sure that number is representable as an "unsigned int"
if ( errno == ERANGE || ul > UINT_MAX )
{
printf( "number out of range error\n" );
continue;
}
//make sure that remainder of line contains only whitespace,
//so that input such as "6sdfh4q" gets rejected
for ( ; *q != '\0'; q++ )
{
if ( !isspace( (unsigned char)*q ) )
{
printf( "unexpected input encountered!\n" );
//cannot use `continue` here, because that would go to
//the next iteration of the innermost loop, but we
//want to go to the next iteration of the outer loop
goto continue_outer_loop;
}
}
return ul;
continue_outer_loop:
continue;
}
}
int main( void )
{
unsigned int num;
num = get_unsigned_int_from_user( "Please enter a number: " );
printf( "Input is valid, you entered: %u\n", num );
}
This program has the following behavior:
Please enter a number: -1
number must not be negative!
Please enter a number: -5000000000
number must not be negative!
Please enter a number: 5000000000
number out of range error
Please enter a number: 4000000000
Input is valid, you entered: 4000000000
The program behaves this way because UINT_MAX has the value 4294967295 on most common platforms.
The function get_unsigned_int_from_user is a modified version of my function get_int_from_user from this answer of mine. See that answer for further information on how that function works.

Each implementation of the C Standard made choices about the size of fundamental (here integer) types. So an int is guaranteed to be at least 16 bits wide, but it is possible to be 32 or 64 bits wide. See: Integer types and Data models.
For simplicity, we assume that an int is 32 bits. Then the value of UINT_MAX would be: 4294967295. This token is 10 digits long. So your input is definitively out of range if it is longer than 10 digits (#chux-ReinstateMonica made a good point in the comments below, e.g. an input of "+000000000000000001" would be longer than 10 digits but still in the range of an unsigned int).
Even if your input has 10 digits, it still could be greater than UINT_MAX. Therefore, parse the string into an unsigned long (strtoul) and test if the value is less than or equal to UINT_MAX.

Related

c programming: Error handling. How do I stop characters from being entered into program

How I can exclude characters from being entered as a value in my programs?
Is there a way through the scanf function to recognize the input as a character and then write a printf to show an invalid value message? It would be more recognizing the character then printing the message I'm concerned with.
Edit:
So as asked, the below is my code for a program that first reads five numbers(each between 1 and 30).For each number read, the program should print a line containing that number of adjacent asterisks.
For this, if I enter a number value it causes the program to stop working. So if i could add a way to create "Try again" message or something similar when they are entered, this will stop it from having errors.
#include <stdio.h>
int main(void)
{
int number1 = 0; int counter;
int sentinelcount = 1;
printf("Please enter 5 values, between 1 and 30");
while (sentinelcount <= 5) {
printf("\n\nEnter number: \n"); /*prompt*/
scanf_s("%d", &number1); /*read an interger*/
sentinelcount++;
if (number1 < 1 || number1 > 30)
{
sentinelcount--;
printf("\nWrong Value\n");
}
if (number1 < 1 || number1 > 30)
{
printf("Enter within correct value range: 1 - 30! ");
}
else if (number1 >= 1 || number1 <= 30)
{
printf("Number of asterisks:\n");
for (counter = 1; counter <= number1;
counter++)
{
printf("*");
}
}
}
return 0;
How do I stop characters from being entered into program
Short of some magic hand that prevents the user from typing in non-numeric or a limited key board, the better approach is not to stop characters from being entered, but accept them as input and then detect invalid input.
I recommend to consume invalid input and alert the user of the issue.
A good first attempt is to read a line of input into a string with fgets().
Test for input, conversion success, extra junk and range.
char buf[80];
if (fgets(buf, sizeof buf, stdin)) { // TBD deal with lines longer than 79
If a line was read, process it with strtol(), sscanf(), etc. Use "%n" to detect where scanning ended. Perform error checking.
int num;
int n;
// If an integer was parsed with no trailing junk and in range ...
if (sscanf(buf, "%4d %n", &num, &n) == 1 && buf[n] == 0 &&
(num >= 1 && num <= 30)) {
Oh_Happy_Day(); // TBD code
} else {
Invalid_input(): // TBD code
}
One way to determine whether the user entered a character or a number is to call scanf with the %d conversion format specifier, and check the return value of scanf. If it returns 1, then the conversion format specifier was successfully matched. Otherwise, you print an error message and prompt the user again to enter input. For example:
#include <stdio.h>
int main( void )
{
int i;
//infinite loop, equivalent to while(1)
//repeat the loop until the input is valid
for (;;)
{
//prompt user for input
printf( "Please enter a number: " );
//attempt to read and convert user input
if ( scanf( "%d", &i ) == 1 )
{
//input was valid
break;
}
//print error message
printf( "Input was invalid, please try again!\n" );
//discard remainder of line
for ( int c; c = getchar(), c != '\n' && c != EOF; )
;
}
printf( "Input was successfully converted to the number %d.", i );
}
This is the output of the program:
Please enter a number: sjdfk
Input was invalid, please try again!
Please enter a number: erlh89
Input was invalid, please try again!
Please enter a number: 34
Input was successfully converted to the number 34.
However, this code has one problem: It will accept input such as "6sdfj23jlj" as valid input for the number 6:
Please enter a number: 6sdfj23jlj
Input was successfully converted to the number 6.
You would probably want to reject the input instead, in this case.
The function scanf will do this, because it is not line-based; it only processes as much input as it can to match the %d conversion format specifier.
One thing you could do to detect such invalid input would be to look at the remainder of the line, and verify that it is empty, apart from the newline character:
#include <stdio.h>
#include <stdbool.h>
int main( void )
{
bool successfully_matched = false;
bool found_newline = false;
int i;
//infinite loop, equivalent to while(1)
//repeat the loop until the input is valid
for (;;)
{
//prompt user for input
printf( "Please enter a number: " );
//attempt to read and convert user input
if ( scanf( "%d", &i ) == 1 )
successfully_matched = true;
//verify that remainder of line is empty
if ( getchar() == '\n' )
found_newline = true;
//break loop if everything was ok
if ( successfully_matched && found_newline )
break;
//print error message
printf( "Input was invalid, please try again!\n" );
//discard remainder of line, if necessary
if ( !found_newline )
{
for ( int c; c = getchar(), c != '\n' && c != EOF; )
;
}
}
printf( "Input was successfully converted to the number %d.", i );
}
Now, the program correctly rejects 6sdfj23jlj as invalid input:
Please enter a number: 6sdfj23jlj
Input was invalid, please try again!
Please enter a number:
However, this code will reject input such as 21 (note the space character after the number). I'm not sure if you want to reject input simply because of a trailing space. If you want to allow spaces and other whitespace characters, then this is possible too, but would require a bit more coding.
In your question, you stated that even if the input is a valid number, you want to additionally check whether the number is in a certain range. This can be accomplished too. However, since the code is starting to get complicated, it seems better to create a new function get_int_from_user to actually get a number from the user. After calling that function, we can then perform the range check, and if the number is not in the desired range, we print an error message and then call the function again.
#include <stdio.h>
#include <stdbool.h>
int get_int_from_user( const char *prompt )
{
bool successfully_matched = false;
bool found_newline = false;
int i;
//infinite loop, equivalent to while(1)
//repeat the loop until the input is valid
for (;;)
{
//prompt user for input
printf( "%s", prompt );
//attempt to read and convert user input
if ( scanf( "%d", &i ) == 1 )
successfully_matched = true;
//verify that remainder of line is empty
if ( getchar() == '\n' )
found_newline = true;
//break loop if everything was ok
if ( successfully_matched && found_newline )
break;
//print error message
printf( "Input was invalid, please try again!\n" );
//discard remainder of line, if necessary
if ( !found_newline )
{
for ( int c; c = getchar(), c != '\n' && c != EOF; )
;
}
}
return i;
}
int main( void )
{
int i;
//repeat until input is in the desired range
for (;;)
{
//read number from user
i = get_int_from_user( "Please enter a number: " );
//perform range check on number
if ( 1 <= i && i <= 30 )
{
//input is in the desired range
break;
}
//print error message
printf( "Number is not in the desired range, please try again!\n" );
}
printf( "The number %d is in the desired range.", i );
}
This program has the following output:
Please enter a number: 342
Number is not in the desired range, please try again!
Please enter a number: 27
The number 27 is in the desired range.
However, I generally do not recommend using scanf for line-based user input. As previously stated, the function scanf does not read one line of input at a time. This means that is can leave leftovers of the line on the input stream, which can be confusing to the programmer, and can lead to bugs, such as this one. See this question for more information on the disadvantages of using scanf.
For line-based user input, it is generally better to use the function fgets. Here is a very robust implementation of the function get_int_from_user which I copied from this previous answer of mine to another question. This function uses fgets and strtol instead of scanf, and performs extensive input validation and error checking:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <errno.h>
int get_int_from_user( const char *prompt )
{
for (;;) //loop forever until user enters a valid number
{
char buffer[1024], *p;
long l;
fputs( prompt, stdout );
//get one line of input from input stream
if ( fgets( buffer, sizeof buffer, stdin ) == NULL )
{
fprintf( stderr, "unrecoverable error reading from input\n" );
exit( EXIT_FAILURE );
}
//make sure that entire line was read in (i.e. that
//the buffer was not too small)
if ( strchr( buffer, '\n' ) == NULL && !feof( stdin ) )
{
int c;
printf( "line input was too long!\n" );
//discard remainder of line
do
{
c = getchar();
if ( c == EOF )
{
fprintf( stderr, "unrecoverable error reading from input\n" );
exit( EXIT_FAILURE );
}
} while ( c != '\n' );
continue;
}
//attempt to convert string to number
errno = 0;
l = strtol( buffer, &p, 10 );
if ( p == buffer )
{
printf( "error converting string to number\n" );
continue;
}
//make sure that number is representable as an "int"
if ( errno == ERANGE || l < INT_MIN || l > INT_MAX )
{
printf( "number out of range error\n" );
continue;
}
//make sure that remainder of line contains only whitespace,
//so that input such as "6sdfj23jlj" gets rejected
for ( ; *p != '\0'; p++ )
{
if ( !isspace( (unsigned char)*p ) )
{
printf( "unexpected input encountered!\n" );
//cannot use `continue` here, because that would go to
//the next iteration of the innermost loop, but we
//want to go to the next iteration of the outer loop
goto continue_outer_loop;
}
}
return l;
continue_outer_loop:
continue;
}
}
If you want to exclude characters from being in your code you can use something like this:
unsigned long long answer1,answer2,answer3,c;
scanf("%*[^0123456789]%llu%*[^0123456789]%llu%*[^0123456789]%llu",&answer1,&answer2,&answer3);
printf("%lld %lld %lld",answer1,answer2,answer3);
return 0;
and if you want to print characters you shouldn't scan characters like this: scanf("%d"&a) instead you scan them with this: scanf("%c",&a) and the same point stands in print. but with this you can only scan one character at a time and if you want to scan more than that use more %c in the scanf and printf.

Is there a way to get numbers as characters in C and validate them?

I want to get a string of numbers up to 50(i.e. the limit on characters is 50) and validate them. For example - 123456789 is valid but 123sadfd456789 is not.
I could only figure out a way to get integer numbers through char.
Here's the code:
#include<stdio.h>
#include<stdlib.h>
int conToInteger(int n){
char ch[50];
printf("Enter a string: ");
gets(ch);
n=atoi(ch);
printf("Answer is: %d",n);
}
int main(){
int n;
conToInteger(n);
return 0;
}
Can anybody help me out?? Thanks.
If all you care about is validating that an arbitrarily long sequence of characters contains only decimal digits, then chux's answer is the right one.
However, based on your code, it looks like you don't just want to validate the string, you want to convert it to the corresponding integer value and save it. If that's the case, 50 digits is way beyond what you can store in any of the standard integer types1; you'd have to use a multi-precision library like GMP. The best you can do with native types is unsigned long long, which on my platform can support values with up to 20 decimal digits (ULLONG_MAX == 18446744073709551615).
Instead of using atoi, you can use something from the strto* family of functions. The neat thing about these functions is you can pass a pointer argument that will point to the first character in the string that wasn't converted - if that character isn't whitespace or the 0 terminator, then you know the input string isn't valid.
In this case we'll use strtoll, which converts strings to long long:
#define LLONG_DIGITS 19 // set this for whatever your platform supports;
// mine supports up to 19 for long long
char buf[LLONG_DIGITS+3] = {0}; // digits plus sign, newline, and terminator
long long val = 0;
if ( fgets( buf, sizeof buf, stdin ) )
{
/**
* Step 1. Check for a newline character. If one isn't present,
* then the input was too long for the buffer. Reject
* the input and clear out the input stream.
*/
char *newline = strchr( buf, '\n' );
if ( !newline )
{
fputs( "Input too long, rejecting\n", stderr );
while ( getchar() != '\n' )
; // empty loop
}
else
{
/**
* Step 2. Replace newline with terminator
*/
*newline = 0;
char *check;
/**
* Step 3. Use strtoll to convert the string to a long long.
* check will point to the first character in the string
* that was *not* converted - if this character isn't
* whitespace or the string terminator, then the input
* was not a valid integer string
*/
long long tmp = strtoll( buf, &check, 10 );
if ( check > buf && !isspace( *check ) && *check != 0 )
fputs( "Input value is empty or contains non-numeric characters!\n", stderr );
else
val = tmp;
}
}
else
{
fputs( "Error on input\n", stderr );
}
Now, when I say a type can support decimal values up to N digits, that doesn't mean it can support all N-digit values. An 8 bit byte can represent 256 distinct values, so it can represent some 3-digit decimals, but it can't represent things like 999. LLONG_MAX is 9223372036854775807, so if you input a value higher than that you will have signed integer overflow, which invokes undefined behavior.
50 decimal digits corresponds to at least 160 bits; I'm not aware of any system with a native integer type that large. An int is only guaranteed to store values in the range [-32767..32767], which is at most 5 digits. Most platforms use 32-bit int which can store values in the range [-2147483648..2147483647] which is 10 digits. A 64-bit `unsigned long long` can store a value up to 18446744073709551615 which is 21 digits. No native type, not even long double, is going to be able to handle 50 digits.
Read the line of user input as a string with fgets(). Use an ample sized buffer. Suggest 2x expected input size.
#define LENGTH_GOOD_MAX 50
#define BUF_N (LENGTH_GOOD_MAX * 2 + 2) // + 2 for the \n \0
char buf[BUF_N];
if (fgets(buf, sizeof buf, stdin)) {
Then test the characters with isdigit() from <ctype.h>.
unsigned char *s = buf; // Change to unsigned type for correct `isdigit()` usage.
while (isdigit(*s)) {
s++;
}
if (s == buf || s-buf > LENGTH_GOOD_MAX) puts("Too short or too long");
else if (*s != '\0' && *s != '\n') puts("Not numeric");
else puts("Just right!");
}
You could use getchar() in a loop, but check #chux answer for best practices.
#include <stdio.h>
#include <ctype.h>
int main() {
char s[50] = { 0 };
for(int i = 0, c; i < 49 && isdigit(c = getchar()); s[i++] = c);
printf("%s", s);
return 0;
}

How can i make a string that only accepts numbers and the character "." in c?

im trying to make a program in c that detects if a number is an integrer or a float, and if it is a float counts the number of decimal places.
But im having an issue here, when i insert a float number, because of the "." the program says it is a "word" and not a number since i made it accept only numbers and i get stuck in the while loop.
My code:
#include<stdio.h>
#include <string.h>
#define BASE 10
main()
{
int length, number;
char str[10];
char ch = '.';
char *ret;
char *endptr;
do{
printf("Enter string: ");
scanf("%s", str);
number = strtol(str, &endptr, BASE);
}while (*endptr != '\0' || endptr == str);
ret = strchr(str, ch);
if(ret > 0){
length = strlen(ret);
printf("decimal places: %d\n", length - 1);
}
else {
printf("the number is integrer\n");
}
return 0;
}
Using strtol is only meaningful once you know that the input is an integer. If you know that the input is a floating-point number, then you can use strtod instead. There is no function that can handle both types, unless you want to handle integers as floating-point numbers.
In order to determine whether the input is an integer, floating-point or invalid input, it would probably be best to examine the input string yourself, for example like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
int main( void )
{
char str[100];
retry_input:
//prompt user for input
printf( "Enter number: " );
//read one line of input
if ( fgets( str, sizeof str, stdin ) == NULL )
{
fprintf( stderr, "input error!\n" );
exit( EXIT_FAILURE );
}
//if newline character exists, remove it
str[strcspn(str,"\n")] = '\0';
//this variable keeps track of the number of digits encountered
int num_digits = 0;
//this variable specifies whether we have encountered a decimal point yet
bool found_decimal_point = false;
//inspect the string one character at a time
for ( char *p = str; *p!='\0'; p++ )
{
if ( *p == '.' )
{
if ( found_decimal_point )
{
printf( "encountered multiple decimal points!\n" );
goto retry_input;
}
found_decimal_point = true;
}
else if ( isdigit( (unsigned char)*p ) )
{
num_digits++;
}
else if ( *p != '+' && *p != '-' )
{
printf( "encountered unexpected character in input!\n" );
goto retry_input;
}
else if ( p - str != 0 )
{
printf(
"sign characters (+/-) are only permitted at the start "
"of the string!\n"
);
goto retry_input;
}
}
if ( found_decimal_point )
{
//input is floating-point
printf( "The input is float and has %d digits.\n", num_digits );
}
else
{
//input is integer
printf( "The input is integer and has %d digits.\n", num_digits );
}
return EXIT_SUCCESS;
}
Note that this program will also count the number of digits encountered, and print the total number at the end of the program. Since you stated in the question that you wanted to count the "number of decimal places", this may be what you want.
Also note that this program won't accept floating point numbers in exponential notation.

scan arbitrary number of chars in c

I want to read an unknown number of characters, which are not greater than 10.
char word[10];
for( i=0;i<10;i++){
if( !scanf("%c",&word[i])){ //terminate with 0
getchar();
break;
}
}
The problem is that number is also an character, so the if statement won't be executed. Is there any other solution to terminate the input of characters for example with 0.
Suggest:
char word[10];
if( scanf("%9s",word) != 1 )
{
fprintf( stderr, "scanf for (max 9 char word) failed\n" );
exit( EXIT_FAILURE );
}
using %9s because the %s input format conversion specifier always appends a NUL byte to the input.
If the input is less than 9 characters, that is properly handled.
If the input is greater than 9 characters, the 9 modifier will stop the input, so the input buffer is not overflowed. Such an overflow would result in undefined behavior.
you can check the character that you just read (at word[i]) and if not valid (e.g not alpabetical) than break.
You can use a do..while loop. Something like (pseudo-code)
int keep_looping = 1;
int counter = 0;
do {
ret = scanf(" %c",&word[counter]);
if (!ret) continue; //can use more cleanup and error check
if (word[counter] == '0') keep_looping =0;
counter++;
}
while (keep_looping && counter < 10)

Why does the printf function print the value input even when the input value in scanf is not equal to 1

I do not understand why the condition in does not reflect the results. I input values that were not equal to 1 as specified by the condition and it still printed. Can someone please explain to me why this is the case.
#include<stdio.h>
int main() {
int n;
while ( scanf( "%d", &n) == 1 )
printf("%d\n",n);
return 0;
}
scanf returns the number of inputs read and assigned, not the value of the input itself. In this particular case you are only expecting a single input, so scanf will return 1 on success, 0 on a matching failure (i.e., the input doesn’t start with a decimal digit), or EOF if it sees end-of-file or an error.
If you want to test against the value of the input, you’d do something like
while( scanf( “%d”, &n ) == 1 && n == EXPECTED_VALUE )
{
printf( “%d”, n );
}
Edit
Actually, a better way to do that would be something like this:
int n;
int itemsRead;
/**
* Read from standard input until we see an end-of-file
* indication.
*/
while( (itemsRead = scanf( "%d", &n )) != EOF )
{
/**
* If itemsRead is 0, that means we had a matching failure;
* the first non-whitespace character in the input stream was
* not a decimal digit character. scanf() doesn't remove non-
* matching characters from the input stream, so we use getchar()
* to read and discard characters until we see the next whitespace
* character.
*/
if ( itemsRead == 0 )
{
printf( "Bad input - clearing out bad characters...\n" );
while ( !isspace( getchar() ) )
// empty loop
;
}
else if ( n == EXPECTED_VALUE )
{
printf( "%d\n", n );
}
}
if ( feof( stdin ) )
{
printf( "Saw EOF on standard input\n" );
}
else
{
printf( "Error while reading from standard input\n" );
}
The problem is that you are not comparing the value of n which is the input read, but the value returned by the scanf function which is the number of inputs you have, in your case is always 1.
More details: Value returned by scanf function in c
This code should works in your case:
#include<stdio.h>
int main() {
int n;
scanf("%d", &n);
while(n == 1){
printf("%d\n",n);
scanf("%d", &n);
}
return 0;
}
I think that you are not comparing correctly n variable with 1.
So if I dont be wrong. Try to compare n with 1.
int main() {
int n;
while ( scanf( "%d", &n) == 1){
if(n!=1){
break;
}
printf("%d\n",n);
}
return 0;
}
This can be a sloppy answer, but it is an example.

Resources