Home >> Sem 3 >> 630007 - Programming Skills-V (SS)
Last Update : 03 / Aug / 2010
1. Write a program to identify whether a given no is valid or not......
Shared By : dizy (College - SVICS ) Show/Hide Program
#include<stdio.h>
char str[20];
int l,i;
int dot,plus,min;
int digit;
void chk();
void main()
{
a:
clrscr();
printf("\nEnter d for exit");
printf("\nEnter any no:");
fflush(stdin);
gets(str);
if(str[0]=='d')
exit(0);
if(str[0]=='\0')
{
printf("\nInvalid no...");
getch();
goto a;
}
dot=i=plus=min=l=digit=0;
l=strlen(str);
for(i=0;i<l;i++)
{
if(str[i] == '\t'||str[i]==' '||isdigit(str[i]) || str[i]=='.' || str[i]=='+' || str[i]=='-')
{
if(str[i]==' ' || str[i]=='\t')
{
if(digit>1 || dot>1 || plus>1 || min>1 )
{
printf("\nInvalid digit...");
getch();
goto a;
}
}
if(str[i]=='.')
dot++;
if(str[i]=='+')
plus++;
if(str[i]=='-')
min++;
}
else
{
printf("\n%s is not valid digit...",str);
getch();
goto a;
}
if(isdigit(str[i]))
digit++;
}
if(digit==0)
{
printf("\nInvalid no...");
getch();
goto a;
}
if((str[0]=='.' || str[0]=='+' || str[0]=='-') &&(l==1))
{
printf("\ninvalid no");
getch();
goto a;
}
else if(str[0]=='+' && plus==1 && min==0)
{
chk();
printf(" and positive no..");
}
else if(str[0]=='-' && min==1 && plus==0)
{
chk();
printf(" and negative no..");
}
else if(min>=1 || plus>=1)
{
printf("\n%s is not a valid digit",str);
getch();
goto a;
}
else
{
chk();
}
getch();
goto a;
}
void chk()
{
if(dot==0)
{
printf("\n%s is valid integer",str);
}
else if(dot==1)
{
printf("\n%s is valid float value",str);
}
else
{
printf("\n %s is not valid float value",str);
}
}