C programming simple if else example
We already discussed c programming control flow statement with various types in our earlier tutorial lets see few examples
C program to find the minimum value of 3 variable
/* simple if statement example */ #include <stdio.h> int main() { int x,y,z,min; x=10; y=5; z=20; if(x<y&&x<z) { min=x; } if(y<x&&y<z) { min=y; } if(z<x&&z<y) { min=z; } printf("Min value is:%d",min); return 0; }
Output:
[quote]Min value is:5[/quote]
C program to find the maximum value of 3 input values
/* Nested if-else example */ #include <stdio.h> int main() { int a,b,c,d,max; printf("Enter 4 values: "); scanf("%d%d%d%d",&a,&b,&c,&d); //a=10; b=20; c=15; d=5; if(a>b&&a>c&&a>d) { max=a; } else { if(b>c&&b>d) { max=b; } else { if(c>d) max=c; else max=d; } } printf("Max value is:%d",max); return 0; }
Output:
[quote]
Enter 4 values: 10 40 30 20
Max value is:40
[/quote]
[review]