Welocome to the Cyber Console. The world of Cyber Geeks. Cyber Console is a community forum of GeeTech Inc.


You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1 Conditional Statement on Sun Sep 18, 2011 12:35 am

Kannan

avatar
Admin
Admin
Hi guys,
There is something i wanna share with you!

if else statement
The general format for these are,

Code:
if( condition 1 )
statement1;
else if( condition 2 )
statement2;
else if( condition 3 )
statement3;
else
statement4;

The else clause allows action to be taken where the condition evaluates as false (zero).

The following program uses an if else statement to validate the users input to be in the range 1-10.

Code:
/* Illustates nested if else and multiple arguments to the scanf function. */
#include <stdio.h>

void main()
{
int invalid_operator = 0;
char operator;
float number1, number2, result;

printf("Enter two numbers and an operator in the format\n");
printf(" number1 operator number2\n");
scanf("%f %c %f", &number1, &operator, &number2);

if(operator == '*')
result = number1 * number2;
else if(operator == '/')
result = number1 / number2;
else if(operator == '+')
result = number1 + number2;
else if(operator == '-')
result = number1 - number2;
else
invalid_operator = 1;

if( invalid_operator != 1 )
printf("%f %c %f is %f\n", number1, operator, number2, result );
else
printf("Invalid operator.\n");
}
Sample Program Output
Enter two numbers and an operator in the format
number1 operator number2
23.2 + 12
23.2 + 12 is 35.2

The above program acts as a simple calculator.
hope this would be useful for you guys!


___________________________________
http://cyberconsole.finddiscussion.com

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum