Friday, October 28, 2011

if else statement condition

Condition - is the state of an expression or a variable; for example, when a result can be either true or
                false, or not equal.
if else statement – this is a control statement that executes a block of code.
                Syntax:
                                if (condition)
                                                statement1;
                                else
                                                statement2;

scanf(); used to read information from the keyboard
scanf(“%f”)  float, scanf(“%c”) character & symbol
clrscr(); -is used to clear the text mode window or to clean the output screen before running a program.

=============================================================================
This program will input 2 numbers, using the ”if else statement”  you can use this following
operators ( +, -, /, *)

//Sample Program1:
#include<stdio.h>
#include<conio.h>

main()
{
int invalid_operator=0;
char oprtor;
float num1, num2, result;
printf(Enter two numbers and an operator?\n");
printf("number1 operator number2\n");
scanf("%f %c %f",&num1,&oprtor,&num2);
                 if(oprtor=='*')
                                                result=num1 *num2;
                 else if (oprtor=='/')
                                                result=num1/num2;
                 else if (oprtor=='+')
                                                result=num1+num2;
                 else if (oprtor=='-')
                                                result=num1-num2;
                 else
                                invalid_operator=1;
                                if(invalid_operator!=1)
                                printf("%f %c %f is %f\n",num1,oprtor,num2, result);
                                else printf("invalid operator");
getche();
return 0;
}

Sample output:

Enter two numbers and an operator
number1 operator number2
4.0000 + 2.0000 is 6.0000
Enter two numbers and an operator
number1 operator number2
4.0000 * 2.0000 is 8.0000

//