Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 24-11-2009
Member
 
Join Date: Nov 2009
Posts: 57
How to check input if it is number or not?

I had written a program to check whether input number is even or not. But I don't know how to write a program to check if the input is valid number or not(like character or string)? I tried various method but it didn't work out.So if any one have any program like this or have any idea how to write program please give it to me.
Thanks in advanced.
Reply With Quote
  #2  
Old 24-11-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: How to check input if it is number or not?

I just write this program for you just try to compile in your computer and let me know whether it work or not.



#include<stdio.h>
#include<ctype.h>


int is_number(char * pchar, int lnum)
{
int i, is_num=0;
for(i=0; i<lnum; i++)
{
if(!isalnum(*pchar)) break;
// printf("%c - ",*pchar);
if(!isdigit(*pchar))
{
is_num++;
break;
}
pchar++;
}
// printf("\nis_num = %i\n",is_num);
return is_num;
}


int main(void)
{
char num[50];

printf("\n number: ");
scanf("%s",num);

if(!is_number(num,50))
{
printf("number input correct\n");
}
else
{
printf("invalid input\n");
}
return 0;
}



I think this will help you.
Reply With Quote
  #3  
Old 24-11-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: How to check input if it is number or not?

I think this is a simplest code in "c" to check input is number or not.



#include <stdio.h>


int main(void)
{
float num1;
printf("Please enter a number:");
while(! scanf("%f", &num1))
{
printf("\n\tInvalid entry, please enter correct entry: ");
fflush(stdin);
}
printf("The number you entered was %.2f\n", num1);
getChar();
}
Reply With Quote
  #4  
Old 24-11-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: How to check input if it is number or not?

There is an option in "c" to check if the input is an integer or not through the return value of scanf. If the return value is not equal to 1 than the input is not an integer.I just write program for you, check whether it works or not.


int main(void)
{
int number, status;
int p = 1;

do
{
printf(" Enter your number:");
status = scanf("%d", &number);
while(status != 1)
{
printf("The input is not numeric. Try again:");
status = scanf("%d", &number);

}

for(p=1;p<= number; p++)
{
printf("*");
}
printf("\n");

}
while(number >= 1);


printf("End\n");

Last edited by kelfro : 24-11-2009 at 04:49 PM.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to check input if it is number or not?"
Thread Thread Starter Forum Replies Last Post
Check Odd or Even Number Without Using Any Condition and Loops CheckMeNot Software Development 5 28-03-2010 03:08 PM
How to check IMEI number of iPhone Elbanco Portable Devices 6 23-01-2010 03:49 AM
How to check China mobile IMEI number ? depesh Portable Devices 9 01-01-2010 06:45 PM
Check multiple text box input GlassFish Software Development 2 24-11-2009 01:23 PM
Check whether an integer is a prime number in C Projectkmo Software Development 2 08-10-2008 06:27 PM


All times are GMT +5.5. The time now is 08:02 PM.