When I am trying to take a contact number from a user and I am print it shows always 0

smit Prajapati

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?

Abhishek Kumar Saw

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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

I am trying to take a user input and display

From Dev

When I am trying to push code on Git it shows errors

From Dev

I am trying to find the prime numbers before a given number from the user

From Dev

I am trying to make a GUI for a app I am working on but when I try print a global variable I get an error. Why?

From Dev

when i am cloning in to website it shows error

From Dev

when i am cloning in to website it shows error

From Dev

I am trying to depict what this java print statement “ (((0xFF << (i * 8)) & test)))) “ means

From Dev

I am trying to display the number of occarances of a letter, but the output only shows the last letter

From Dev

I am trying to code a function which can take two arrays of number and sorted in ascending order

From Dev

I am trying to code a function which can take two arrays of number and sorted in ascending order

From Dev

I am new to JS and I am stuck when trying to bring the user back to the beginning of the program after user clicks OK in confirm

From Dev

i am getting error when i am fetching user data from database in laravel through API

From Dev

Error when i am trying to remove an element

From Dev

Where am I going wrong? Trying to print no of times number is added before returning single digit

From Dev

I am trying to take input in specific form with separated spaces

From Dev

With CTE why i am not able to print 1 to 1000 number or more but 1 to 100 i am able to print

From Dev

What is exactly happening when I am using setTimeout within another function and trying to print the value of the argument passed?

From Dev

I am writing a program to find number of 3's in factorial of a given number. The input from the user is the number

From Dev

Why am I getting "FROM expression expected" from SQLAlchemy when I'm trying to update a row in a table?

From Dev

I am trying to print a table with a double method..yet not working

From Dev

I am getting null pointer exception when I an trying to putExtra from intent

From Dev

I am trying to reset password in parse through email i am not finding correct email id for user and i am using this code

From Dev

Why am I getting an Invalid Object Name error when trying to create a user?

From Dev

I am getting a Cannot modify header information when trying to redirect a user after form submission

From Dev

Why am I getting garbage values when I print the value?

From Dev

Why am I getting an error when I try to print the contents of a file I am searching for?

From Java

I am trying to load all images from a folder if the filename contains a specific number

From Dev

what is wrong in my program ? i am trying to find the list of prime number from n to m

From Dev

I am trying to subtract the number entered into the textfield from the database but not sure how to do it

Related Related

  1. 1

    I am trying to take a user input and display

  2. 2

    When I am trying to push code on Git it shows errors

  3. 3

    I am trying to find the prime numbers before a given number from the user

  4. 4

    I am trying to make a GUI for a app I am working on but when I try print a global variable I get an error. Why?

  5. 5

    when i am cloning in to website it shows error

  6. 6

    when i am cloning in to website it shows error

  7. 7

    I am trying to depict what this java print statement “ (((0xFF << (i * 8)) & test)))) “ means

  8. 8

    I am trying to display the number of occarances of a letter, but the output only shows the last letter

  9. 9

    I am trying to code a function which can take two arrays of number and sorted in ascending order

  10. 10

    I am trying to code a function which can take two arrays of number and sorted in ascending order

  11. 11

    I am new to JS and I am stuck when trying to bring the user back to the beginning of the program after user clicks OK in confirm

  12. 12

    i am getting error when i am fetching user data from database in laravel through API

  13. 13

    Error when i am trying to remove an element

  14. 14

    Where am I going wrong? Trying to print no of times number is added before returning single digit

  15. 15

    I am trying to take input in specific form with separated spaces

  16. 16

    With CTE why i am not able to print 1 to 1000 number or more but 1 to 100 i am able to print

  17. 17

    What is exactly happening when I am using setTimeout within another function and trying to print the value of the argument passed?

  18. 18

    I am writing a program to find number of 3's in factorial of a given number. The input from the user is the number

  19. 19

    Why am I getting "FROM expression expected" from SQLAlchemy when I'm trying to update a row in a table?

  20. 20

    I am trying to print a table with a double method..yet not working

  21. 21

    I am getting null pointer exception when I an trying to putExtra from intent

  22. 22

    I am trying to reset password in parse through email i am not finding correct email id for user and i am using this code

  23. 23

    Why am I getting an Invalid Object Name error when trying to create a user?

  24. 24

    I am getting a Cannot modify header information when trying to redirect a user after form submission

  25. 25

    Why am I getting garbage values when I print the value?

  26. 26

    Why am I getting an error when I try to print the contents of a file I am searching for?

  27. 27

    I am trying to load all images from a folder if the filename contains a specific number

  28. 28

    what is wrong in my program ? i am trying to find the list of prime number from n to m

  29. 29

    I am trying to subtract the number entered into the textfield from the database but not sure how to do it

HotTag

Archive