prev


The else if ladder

19. One,Two,Three or Four digit number (29.6.2009)

/*
Program# 19
date : 25-12-2008
Single,two,three or four digit number
*/

#include <stdio.h>
main(){
int n;
printf("\nEnter a positive number :");
scanf("%d",&n);
if(n>=0 && n<=9)
printf("Single digit number");
else if(n>=10 && n<=99)
printf("\n %d: Two digit number",n);
else if(n>=100 && n<=999)
printf("\n %d: Three digit number",n);
else if(n>=1000 && n<=9999)
printf("\n %d: Four digit number",n);
else
printf("\n %d: Greater than four digits",n);
}

output:
Enter a positive number : 123
123: Three digit number

Things to ntoe:
else if ladder
The last else will execute only if all the conditions are false.The below given both the forms of if are equal.

if(condition)
do this;
else
{
if(condition)
do this;
}
if(condition)
do this;
else if(condition)
do this;
next
blog comments powered by Disqus