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?
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.
Comments