Saturday, 12 May 2012

If and else in c

If you want to compare two or more options, this time you can implement if.. else . the general syntex for if..else is

if ( condition ) {
   expr_set1;
}
else  {
   expr_set2;
}

you can remember from spreadsheet program, whre we used if function for TRUE & FALSE.

The code block of our last solved proram is given below

#include<stdio.h>
int main(){
        int i,j, result;
        printf("Write the two numbers you want compare:");
        scanf("%d %d",&i, &j);
        result = i-j;
        if ( result > 0 )
                printf("%d is greater than %d\n",i,j);
        
        else 
          if ( result < 0 )
             printf("%d is greater than %d\n",i,j);        
       
        else
            if ( result = 0 )
               printf("Both the numbers are equal\n");

     system ("PAUSE");       
     return 0;
}
Here is the screenshot of the program run with the two numbers 45 &90

No comments:

Post a Comment