Sample C program showing if – else constructs
March 7, 2020
Below are simple C programs for beginners to learn if-else decision construct.
Example 1: Program to check if a number is odd or even
main (){ int n; printf("Please enter any number>> "); scanf("%d", &n); if(n % 2 == 0) printf("\n%d is EVEN \n", n); else printf("\n%d is ODD \n", n); }
Example 2: Program to check which number is the largest
main (){ int a, b, c; printf("Please enter 3 numbers of your choice>> "); scanf("%d %d %d", &a, &b, &c); if(a > b && a > c) printf("\n%d is Largest\n", a); else if(b > c) printf("\n%d is Largest\n", b); else printf("\n%d is Largest\n", c); }