Have a Read!

Hi Everyone.
The idea for this page is to provide new c users simple c tutorials to learn how to develop the logic and more importantly correct way to write syntax.
ENJOY !!!

Wednesday, January 26, 2011

C PROGRAM FOR NUMBER VALIDITY FOR DIFFERENT CONDITIONS

This program checks the validity of two numbers.a valid input would satisfy following conditions:
a)inputs should be integers
b)each no. is between 1 and 100
c)two no.s are not divisible to each other
d)difference of two is not less than 1
e)sum of these should not exceed 100

#include<stdio.h>

int main()
{
float no1,no2;
int x,y,z;
z=0; /*this variable is introduced so that in the end we can print inputs are valid or not.for each condition satisfying
      its value will increase by 1.if its value becomes 5 at the end then all the conditions are satisfying otherwise not*/
printf("Enter two numbers\n");
scanf("%f",&no1);            
scanf("%f",&no2);
/*this procedure will calculate that the number is an integer or not.Any integer devided by 2 will always give remainder 0,1 or -1.(no1-2*x) in this formula x is not a floating point no. so it will neglect no.s after decimal.in this way remainder will always be 0,1 or -1 if no1 is an integer*/
x=no1/2;
if((no1-2*x)==0 || (no1-2*x)==1 || (no1-2*x)==-1) // the || is used for or
{
x=no2/2;
if((no2-2*x)==0 || (no2-2*x)==1 || (no2-2*x)==-1)
{
printf("\nthe numbers are integers\n");
z=z+1;
}
else
printf("one or more numbers are not integers\n");
}
else
printf("one or more numbers are not integers\n");
/*in this procedure it is calculated that the numbers are between 1 and 100 or not.1/no1 will always be greater than 1/100
//or 0.01 if no1>0 and no1<100 and if no1 is between 1 and 100 than no1 cannot be equl to 0,1 or 100*/
if((1/no1)>0.01 && (1/no2)>0.01 && no1!=100 && no2!=100 && no1!=0 && no2!=0 && no1!=1 && no2!=1)
{
printf("numbers enterd are between 1 and 100\n");
z=z+1;
}
else
printf("one or more numbers enterd are not between 1 and 100\n");
//y is not afloating point number so it will neglect any no.s after decimal.according to formula in //the if statement if the two no.s are not divisible the formula will not give 0 resultant
y=no1/no2;
if(no1-no2*y!=0)
{
y=no2/no1;
if(no2-no1*y!=0)
{
printf("numbers are not divisible to each other\n");
z=z+1;
}
}
else
printf("numbers are divisible to each other\n");
//if difference of two numbers is not less then one then one number is atleast +1 greater tnan the other
if(no1>=no2+1 || no2>=no1+1)
{
printf("difference is not less than 1\n");
z=z+1;
}
else
printf("difference is less than 1\n");
//the sum of numbers should not be greater than 100
if((no1+no2)<=100)
{
printf("sum of numbers does not exceed 100\n");
z=z+1;
}
else
printf("sum of numbers exceeds 100\n");
//as mentioned earlier if z becomes 5 at the end it will be printed that inputs are valid otherwise not
if(z==5)
printf("\nall conditions are satisfied inputs are valid\n");
else
printf("\none or more conditions are not satisfied inputs are not valid\n");

return 0;

}

No comments:

Post a Comment

Related Posts with Thumbnails