Wednesday, August 17, 2011

QUES2 BASIC CONTINUE AND SUM AND AVG


C LANGUAGE BOOK


 Conditional Operators

The conditional operators  ? and  : are sometimes called ternary
operators since they take three arguments. In fact, they form a kind
of foreshortened if-then-else. Their general form is,

expression 1 ? expression 2 : expression 3
What this expression says is: “if expression 1 is true (that is, if its
value is non-zero), then the value returned will be  expression 2,
otherwise the value returned will be  expression 3”.

Control Instructions in C

As the name suggests the ‘Control Instructions’ enable us to
specify the order in which the various instructions in a program are
to be executed by the computer. In other words the control
instructions determine the ‘flow of control’ in a program. There
are four types of control instructions in C. They are:

(a)  Sequence Control Instruction
(b)  Selection or Decision Control Instruction
(c)  Repetition or Loop Control Instruction
(d)  Case Control Instruction

The Sequence control instruction ensures that the instructions are
executed in the same order in which they appear in the program.
Decision and Case control instructions allow the computer to take
a decision as to which instruction is to be executed next. The Loop
control instruction helps computer to execute a group of statements
repeatedly.

 Decision
a  decision control instruction
can be implemented in C using:

(a) The if statement 
(b) The if-else statement
(c) The conditional operators


The if Statement
 The general form of if statement looks
like this:

if ( this condition is true )
 execute this statement ;












The if-else Statement  

the general form of if-else statement is like this

if ( this condition is true )
 execute this statement1 ;
else
 execute this statement2 ;

 statement1 is executed if condition evaluates to true.
 if condition evaluates to false then  statement2 is executed.



Nested if-elses 

 It is perfectly all right if we write an entire if-else construct within
either the body of the if statement or the body of an else statement.
This is called ‘nesting’of  ifs.


Forms of if
The if statement can take any of the following forms:

(a)
if ( condition )
 do this ;

(b)
if ( condition )
{
 do this ;
 and this ;
}

(c)
if ( condition )
 do this ;
else
 do this ;

(d)
if ( condition )
{
 do this ;
  and this ;
}
else
{
 do this ;
 and this ;
}

(e)  if ( condition )
 do this ;
else
{
 if ( condition )
  do this ;
 else
 {
  do this ;
  and this ;
 }
}

 (f)
if ( condition )
{
 if ( condition )
  do this ;
 else
 {
  do this ;
  and this ;
 }
}
else
 do this ;
 


The else if Clause 
the general form of else if clause is like this

if ( this condition1 is true )
 execute this statement1 ;
else if ( this condition2 is true )
 execute this statement2 ;
else if ( this condition3 is true )
 execute this statement3 ;
else
execute this statement3 ;

In this Case every else is associated with its previous if.
The last else goes to work only if all the conditions evaluates to false. Even in
else if ladder the last else is optional.



WAP TO PRINT SUM AND AVERAGE OF 5 NUMBER THAT IS ENTERED BY USER.

#include<stdio.h>
int main(void)
{
int n, h, j, r, m;
float e,t;

scanf("%i%i%i%i%i", &h, &j, &n, &r, &m);
printf("\n%i%s%i%s%i%s%i%s%i", h," ", j, " ", n," ", r, " ",m);
e = h + j + n + r + m;

printf("\nthe sum is%f", e);
t = e / 5;

printf("\nthe average is%f", t);

getch();
}


C LANGUAGE BOOK

No comments:

Post a Comment