Friday 30 August 2013

“if-else” Statement | C Programming Language Tutorial pdf



“if-else” Statement:

=> It is observed that the if statement executes only when the condition following if is true.
=> It does nothing when the condition is false.
=> In if-else either True-Block or False – Block will be executed and not both.
=> The “else” Statement cannot be used without “if”.

Syntax:
if ( Test Expression or Condition )
{
Statements; /*true block (or) if block */
}
else
{
Statements; /* false block (or) else block */
}


Flow chart:
Example 1:
Write a program to print the given number is even or odd.
# include<stdio.h>
# include<conio.h>
main( )
{
int n;
clrscr( );
printf(“Enter a number:”);
scanf(“%d”, &n);
if( (n%2)==0 )
printf(“\n The given number is EVEN ”);
else
printf(“\n The given number is ODD ”);
getch( );
}

Output:
Run 1:
Enter a number: 24
The given number is EVEN
Run 2: /* that means one more time we run the program */
Enter a number: 17
The given number is ODD

Example 2:
Develop a program accept two numbers and find largest number and print.
# include<stdio.h>
# include<conio.h>
main( )
{
int a,b;
clrscr( );
printf(“Enter Two numbers:”);
scanf(“%d%d”, &a,&b);
if( a>b )
printf(“\n %d is largest number”,a);
else
printf(“\n %d is largest number”,b);
getch( );
}

Output:
Run 1:
Enter Two numbers: 13 30
30 is largest number
Run 2: /* that means one more time we run the program */
Enter Two numbers: 235 174
235 is largest number

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved