Decision Making and Branching - IF-ELSE C Programs
This article is about the Programs of Decision making and branching (if else statements)
//C program to print even numbers up to the user entered limit #include<stdio.h> #include<conio.h> void main() { int i=1,n; clrscr(); printf("Enter a number up to which u want the program to print even numbers"); scanf("%d',&n); label: /*label for goto statement /*(here looping can be used but its not yet covered in the topics if( (i%2==0) && (i<=n) ) { printf("%d",i); i++; goto label; } getch(); }
Simple If-else Statement Program
//C program for simple if-else statement #include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf(" \t\t--------C PROGRAM TO CHECK WEATHER THE NUMBER IS EVEN OR ODD--------"); printf( " Enter a number:"); scanf(" %d",&n); if(n%2==0) { printf( "\t\t******THE ENTERED NUMBER IS EVEN******"); } else { printf("\t\t******THE ENTERED NUMBER IS ODD******"); } getch(); }
Nested If- Else statement Program
//C program to find maximum of three numbers #include<stdio.h> #include<conio.h> void main() { int x,y,z,max; clrscr(); printf(" ENTER THREE NUMBER TO FIND MAXIMUM OF THOSE THREE\n"); printf(" X:\n"); scanf("%d",&x); printf(" Y:\n"); scanf("%d",&y); printf(" Z:\n"); scanf("%d",&z); if( x>y) { if(x>z) max=x; //Nested statement } else if(y>z) { max=y; } else max=z;
Else-If ladder programs
//C program to compare the two entered number #include<stdio.h> #include<conio.h> void main() { int i,j; printf(" ENTER TWO NUMBERS:\n" ); printf(" X:"); scanf("%d",&i); printf(" Y:"); scanf("%d",&j); if( i>j) { printf(" \nX IS GREATER THAN Y "); } else if (j<i) { printf(" \nY IS GREATER THAN X"); } else { printf(" \nBOTH ARE EQUAL"); } }
IF ANY OF THE ABOVE PROGRAMS HAVE ANY ERROR PLEASE TELL ME IN COMMENT.
FEEL FREE TO COMMENT IF YOU FACE ANY PROBLEM.
If you want to know about IF-else statement in detail go to : http://youthhworld.blogspot.com/2015/03/if-else-statement.html
Comments
Post a Comment