nikhil arora Ответов: 2

Как присвоить 4 значный номер каждому элементу массива на языке Си


я хочу назначить 4 значное число массиву no должно быть только 4 значным и это не так Затем игнорировать число и снова принимать входные данные

[edit]добавлен блок кода-OriginalGriff [/edit]

Что я уже пробовал:

#include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int a[5]; //array
        int i=0;
        int temp,rem=0;
        for(i=0;i<5;i++)
        {
            scanf("%d",&a[i]); //taking input from user
            while(a[i]/10!=0)
            {
                    rem=a%10;  //condition check but this part is not working
                    temp++;   //increasing temp one by one
                    a[i]=a[i]/10; //dividing the no by 10
            }
    
                if(temp==4)
                {
                        printf("no is acceptable "); //if digit is of 4 then no //is acceptable
                        temp=0;
                }
                else
                {
                    printf("please again enter the 4 digit no");
                    scanf("%d",&a[i]); //again scan the no and again check the //condition
                    while(a[i]/10!=0)
                    {
                            rem=a%10;
                            temp++;
                            a[i]=a[i]/10;
                    }
    
                        if(temp==4)
                        {
                                printf("no is acceptable "); //if count is 4 //then only no is acceptable
                                temp=0;
                        }
                }
        }
        for(i=0;i<5;i++)
        {
            printf("%d",a[i]); //printing the no
        }
        return 0;
    }

2 Ответов

Рейтинг:
1

OriginalGriff

Почему вы проверяете, что число имеет четыре цифры с модулем в цикле?

if (a[i] >= 1000 && a[i] <= 9999)
   {
   // It has four digits
   ...
   }


nikhil arora

Спасибо! много

OriginalGriff

Пожалуйста!

Рейтинг:
0

Patrice T

Ваш код выглядит сложным !
Любое целое число с 4 цифрами находится между 1000 и 9999.

if (a[i]>=1000 && a[i]<=9999) {
    ...
}