Computer Concept & Programming In C - Unit III - 2

Q.2. Write a program in C to determine whether a given number is ‘odd’ or ‘even’ and print the message:
NUMBER IS EVEN
or
NUMBER IS ODD
without using else option.                                                (AKTU. 2011 - 12)
Ans.
# include<stdio.h>
# include<conio.h>
  void main ( )
      {
int n;
clrscr ( );
if ((n%2)==0)
printf(“\n NUMBER IS EVEN”);
if ((n%2)==1)
printf(“n\ NUMBER IS ODD”);
getch ( );
    }


Q.3. What will be the output of following code:
main ( )
{
float me = 1.1;
double you = 1.1;
if(me = = you)
printf(“I LIKE C”);
else
printf(“I LIKE JAVA”);
}                                                    (AKTU. 2011 - 12)   
Ans. I LIKE C is the output.

Q.4 Describe if-else structure with example in detail.
Ans. The complete if-else structure: -
You can include the two results of the condition (TRUE and FALSE) in one construct by using the complete if-else structure, which takes the form:
if (condition)
     statement 1;
else 
    statement 2;
in this form the body of the condition structure is repeated from the rest of the program. When the condition is evaluated one of the two result statements will be executed, then the program resume its original flow.
The example below shows if else statement
# include <stdio.h>
# include <conio.h>
void main ( )
{
int amount;
clrscr ( );
printf(“\ n Enter the amount”);
scanf (“%d”, & amount);
if (amount > = 1000)
printf (“\ n Your charge is accepted”);
else
printf (“\ n The amount exceeds your credit limit”);
getch ( );
}
Using blocks makes it possible to use many statements as a result for either the TRUE or FALSE conditions. The if-else structure will then take the form:
if (condition)
{
statement(s);
}
else
{
statement(s);
}
Look at the following example.
#include <stdio.h>
# include <conio.h>
void main ( )
{
float amount;
clrscr ( );
printf (“\ n Enter the amount”);
scanf (“%f ”, & amount);
if (amount < = 1000)
{
printf (“| n Your charge is accepted”);
printf (“\ n Your price plus taxes is $ % .2f”, amount*1.05);