0

The source code is below.

#include <stdio.h>
int main()
 {
    long long n;
    int count;
    lable:
    count = 0;

    printf("Enter your contact number : ");
    scanf("%lld", &n);

    //know that enterd interger(contact number) have how many numbers in it
    while (n != 0) {
        n /= 10;     
        ++count;
    }

    //if numbers are 10 then your contact number is right
    if(count==10)
    {
    printf("contact number enterd sucessfully\n");
    goto exit;
    }
    else
    //else numbers are not 10 then invalid contact number and enter again 
    {
    printf("In valid contact number enter again...........\n");
    goto lable;
    }

    exit:
    printf("contact number :  %lld ",n);
    return 0;
}

In this code, I try to enter the contact number from the user and it is checked how many numbers in your contact number, then there is 10 numbers in your contact number so it is right, but the numbers are not 10 It is showing an error and enter the number from the user again. when the number is right program is print user contact number 0. Please guide me what is the problem with this code?

  • `n /= 10;` changes the value of `n`. So when you print it at the end it will of course be `0`. – kaylum Feb 06 '21 at 04:10

3 Answers3

2

what you have done is you are changing the value of n to 0 in the while loop and printing the same n at the end. That's why it is printing 0. what you can do is, you can take a temporary variable to count number of characters. Refer the code below:

#include <stdio.h>
int main()
 {
    long long n,temp;    //taking a temporary variable along with n;
    int count;
    lable:
    count = 0;

    printf("Enter your contact number : ");
    scanf("%lld", &n);
    temp = n;   // assigning n to temp variable
    //know that enterd interger(contact number) have how many numbers in it
    while (temp != 0) {   //counting the number of digits using temp variable.
        temp /= 10;     
        ++count;
    }

    //if numbers are 10 then your contact number is right
    if(count==10)
    {
    printf("contact number enterd sucessfully\n");
    goto exit;
    }
    else
    //else numbers are not 10 then invalid contact number and enter again 
    {
    printf("In valid contact number enter again...........\n");
    goto lable;
    }

    exit:
    printf("contact number :  %lld ",n);
    return 0;
}
2

The problem is with the statement n /= 10; Here you are using assignment operator so it changes the value of n. For example, if you entered n as 9876543210, then after 1st iteration, the value of n will be 9876543210/=10 i.e. 987654321. In this way after all iterations the value of n will be 0.

You can use temporary variable to store the n value and print it.

#include <stdio.h>
int main()
 {
    long long n, m;

    int count;
    lable:
    count = 0;

    printf("Enter your contact number : ");
    scanf("%lld", &n);
    m = n;
    //know that enterd interger(contact number) have how many numbers in it
    while (n != 0) {
        n /= 10;     
        ++count;
    }

    //if numbers are 10 then your contact number is right
    if(count==10)
    {
    printf("contact number enterd sucessfully\n");
    goto exit;
    }
    else
    //else numbers are not 10 then invalid contact number and enter again 
    {
    printf("In valid contact number enter again...........\n");
    goto lable;
    }

    exit:
    printf("contact number :  %lld ",m);
    return 0;
}
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
0

Ponder over this portion:

while (n != 0) {
    n /= 10;     
    ++count;
}

What you're doing here changing the value of n that you've scanned from the user. So what you can do is not using the variable n rather make a copy of it and use it. A good practice here is to make the variable constant that you don't want to edit accidentally.

Mateen
  • 1,455
  • 10
  • 17