0

I'm new in C programming language.
I need to get every digit separately that user have entered.
Here is my code:

#include <stdio.h>

int main()
{
    int n[100];
    printf("Enter a number: ");
    scanf("%d",&n);
    printf("%d %d %d",n[1],n[2],n[3]);

   return 0;
} //i know that my code is not assigning like i want.

and now for example user entered a number like 123, i want the output like 1 2 3,
How can i assign every digit to n[i] ?
Without using string to int or int to string like atoi?
Here is what Im going to do:
User will enter a number and the program will search from Matrix 100x100 in row or column. i think i need to get the every digit separately to search.

5
  • 2
    Use getchar function. Commented Jun 9, 2014 at 13:54
  • 1
    Don't use scanf Commented Jun 9, 2014 at 13:54
  • i need to get the number(user have entered) as integer bcuz user might enter minus or the number starts with 0. at that time the program will do another operation! Commented Jun 9, 2014 at 14:01
  • It's unclear exactly what you need. Please specify some sample input and the corresponding output. If you claim that getchar doesn't work, provide sample input for that as well. Commented Jun 9, 2014 at 14:03
  • The answer that is posted below is right, it's up to you to figure out how to store the negative-flag of the number. Commented Jun 9, 2014 at 14:04

2 Answers 2

3

No need to go to character array. The lats digit of a number n can be computed using n%10. Then you can remove the last digit using n /= 10. So this cycle would print the digits in reverse order:

void print_rev_digits(int n) {
  while (n) {
    printf("%d\n", n%10); 
    n /= 10;
  }
}

And using a stack you can print the digits in the correct order. You can also use recursion for this(which will use stack for you). I am deliberately not posting a complete solution.

Sign up to request clarification or add additional context in comments.

4 Comments

its not just for printing out. I`ll use it to search in big code. and also i would be glad if u explain whats that %10 and in while loop why there is only one 'n' ? as i said im new in programming
@user3722549 % is binary operator and it returns the remainder of its first operand after integer division by its second operand. There is only one n in the function - the function argument.
n[100] user might enter a larg number at that time this code can work fast enough ?
@user3722549 you are reading a single integer and the maximum value an integer can possibly have is about 2^32(for most modern computers). Thus you will not be able to enter a 100 digit number this way. You will have to read in a 100 character string to be able to read such input.
0

In this case you should read the user input character by character:

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int main()
{
    char input[100];
    int n[100];
    printf("Enter a number: ");
    if (fgets(input, sizeof(input), stdin)) { // attempt to read a line
        int i;
        for (i = 0; input[i]; i++) { // for each entered character
            if (input[i] >= '0' && input[i] <= '9') { // is a digit
                n[i] = input[i] - '0';
                printf("%d ", input[i] - '0');
            } 
            else if (isspace(input[i])) // end of entered integer
                break;
            else {
                printf(stderr, "Input is not a number\n");
                return -1;
            }
        }
        printf("\n");
    } else {
        fprintf(stderr, "User did not enter valid input.\n");
    }

   return 0;
}

2 Comments

i think its no difference than getchar and what if user entered -123 ? hey but i want to know that return -1 , is it difference than return 0 ?
The return value from main will be passed as an exit code to the parent who called. As a convention 0 indicates that the program exited normally and any other value that it exited abnormally. Sometimes a non-zero exit code will allow the parent to determine the cause of the abnormal exit. In this case it just indicates that the program exited abnormally with an error coded -1, whatever that means is up to you to interpret :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.