//ok i need to write a programme that asks the user to input student numbers as an array and check the initial number and allocate them into different classrooms.. my code looks lik ths:

#include <stdio.h>

int number;
int i;
int student_number[10];
int total;
int first[0];

int main (void)
   {
               

 printf("Enter the total number of students ");
 scanf("%d", &total);
            
                  
                 for (i=1; i<=total; i++)
                  {
                        printf("Enter the student number\n: "); 
                        scanf("%d", &student_number);
                        printf("first digit is %d\n", student_number[0]);
                       
                       if (student_number[0] < 5)                         
                          { 
                          printf(" Classroom A\n");
                          }
                        else if (student_number[0] >5)                         
                          { 
                          printf(" Classroom B\n");
                          }

                        
                        
                  }   
                   
system ("pause");
return 0;
}

the code only works if there is a space between the digits, ie. if input is 23 3, i get the first digit as 23. what should i do so that it extracts the first digit from the number with no spaces in between?

Recommended Answers

All 3 Replies

Change student_number to an array of char. Employ the transformation first = (int)(student_number[0]-'0'); to get the first character back to a digit (you don't need to declare first as first[0] as an array with zero elements is just a regular variable). You'll need to change your scanf also to reflect these changes.

Also, please use the code tags (code) your code goes between these (/code) next time.

instead of asking the first digit,
make a formula/calculations to get only the first digit

I had thought about suggesting that too, but OP wasn't doing anything strictly numerical with the data anyway. All that division and modding couldn't be all that much fun... ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.