if - else statement in C / C++ with example
In this tutorial, we will learn about if - else
statement in C / C++ with examples. These are top examples of how to use if - else
statement in C / C++. If you are learning about the if - else
statement, it is impossible to ignore it.
First, you need to learn about if - else
statement in C/C++. These are the basics to use in the examples.
Top 10 if - else statement in C / C++ with examples
Here, we will give 10 examples of if - else
statement in C / C++. These examples are written in two different languages, C and C++.
Example 1: Finding the largest of three numbers with if - else statement in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int a, b, c, max; cout << "Enter number a = "; cin >> a; cout << "Enter number b = "; cin >> b; cout << "Enter number c = "; cin >> c; max = a; if(max < b) { max = b; } if(max < c) { max=c; } cout << "The largest of the three numbers " << a << ", " << b << ", " << c << " is: " << max; cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { int a, b, c, max; printf("Enter number a = "); scanf("%d", &a); printf("Enter number b = "); scanf("%d", &b); printf("Enter number c = "); scanf("%d", &c); max = a; if(max < b) { max = b; } if(max < c) { max=c; } printf("The largest of the three numbers %d, %d, %d is: %d",a,b,c,max); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 2: Finding the smallest of three numbers with if - else statement in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int a, b, c, min; cout << "Enter number a = "; cin >> a; cout << "Enter number b = "; cin >> b; cout << "Enter number c = "; cin >> c; min = a; if(min > b) { min = b; } if(min > c) { min=c; } cout << "The smallest of the three numbers " << a << ", " << b << ", " << c << " is: " << min; cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { int a, b, c, min; printf("Enter number a = "); scanf("%d", &a); printf("Enter number b = "); scanf("%d", &b); printf("Enter number c = "); scanf("%d", &c); min = a; if(min > b) { min = b; } if(min > c) { min = c; } printf("The smallest of the three numbers %d, %d, %d is: %d",a,b,c,min); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 3: Checking Even or Odd number with if - else statement in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int a; cout<<"Enter number a = "; cin>>a; if(a % 2 == 0) cout<<"\n"<<a<<" is the Even number."; else cout<<"\n"<<a<<" is the Odd number."; cout<<"\n\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { int a; printf("Enter number a = "); scanf("%d", &a); if(a % 2 == 0) printf("\n%d is the Even number.", a); else printf("\n%d is the Odd number.", a); printf("\n\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 4: Checking for Negative or Positive numbers with if - else statement in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int a; cout<<"Enter number a = "; cin>>a; if(a > 0) cout<<"\n"<<a<<" is the Positive numbers."; else if(a < 0) cout<<"\n"<<a<<" is the Negative number."; else cout<<"\nThis is zero."; cout<<"\n\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { int a; printf("Enter number a = "); scanf("%d", &a); if(a > 0) printf("\n%d is the Positive numbers.", a); else if(a < 0) printf("\n%d is the Negative number.", a); else printf("\nThis is zero."); printf("\n\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 5: Checking for Primes with if - else statement in C / C++
C++ programming:
#include <iostream> using namespace std; bool isPrimes(int n) { if (n < 2){ return false; } if (n == 2){ return true; } if (n % 2 == 0){ return false; } for (int i = 3; i < (n - 1); i += 2){ if (n % i == 0){ return false; } } return true; } int main() { int a; cout<<"Enter number a: "; cin>>a; if(isPrimes(a)) cout<<endl<<a<<" is the Primes."; else cout<<endl<<a<<" is not the Primes."; cout<<"\n\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> bool isPrimes(int n) { if (n < 2){ return false; } if (n == 2){ return true; } if (n % 2 == 0){ return false; } for (int i = 3; i < (n - 1); i += 2){ if (n % i == 0){ return false; } } return true; } int main(void) { int a; printf("Enter number a: "); scanf("%d", &a); if(isPrimes(a)) printf("\n%d is the Primes.",a); else printf("\n%d is not the Primes.",a); printf("\n\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 6: Checking for Perfect Squares with if - else statement in C / C++
C++ programming:
#include <iostream> using namespace std; bool isPerfectSquares(int n){ int i = 0; while(i*i <= n){ if(i*i == n){ return true; } ++i; } return false; } int main() { int a; cout<<"Enter number a: "; cin>>a; if(isPerfectSquares(a)) cout<<endl<<a<<" is the Perfect Squares number."; else cout<<endl<<a<<" is not the Perfect Squares number."; cout<<"\n\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> bool isPerfectSquares(int n){ int i = 0; while(i*i <= n){ if(i*i == n){ return true; } ++i; } return false; } int main(void) { int a; printf("Enter number a: "); scanf("%d", &a); if(isPerfectSquares(a)) printf("\n%d is the Perfect Squares number.",a); else printf("\n%d is not the Perfect Squares number.",a); printf("\n\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 7: Checking for Perfect number with if - else statement in C / C++
C++ programming:
#include <iostream> using namespace std; bool isPerfectNumber(int a){ int sum = 0; for(int i=1;i<=a/2;i++){ if(a%i==0) sum+=i; } if(sum==a) return true; return false; } int main() { int a; cout<<"Enter number a: "; cin>>a; if(isPerfectNumber(a)) cout<<endl<<a<<" is the Perfect number."; else cout<<endl<<a<<" is not the Perfect number."; cout<<"\n\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> bool isPerfectNumber(int a){ int sum = 0; for(int i=1;i<=a/2;i++){ if(a%i==0) sum+=i; } if(sum==a) return true; return false; } int main(void) { int a; printf("Enter number a: "); scanf("%d", &a); if(isPerfectNumber(a)) printf("\n%d is the Perfect number.",a); else printf("\n%d is not the Perfect number.",a); printf("\n\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 8: Displaying Even numbers in array with if - else statement in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7}; int i = 0, j = 0; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ cout<<"Elements of array: "; while(i<9){ cout<<arr[i]<<" "; i++; } cout<<"\nEven numbers in array: "; while(j<9){ if(arr[j] % 2 == 0) cout<<arr[j]<<" "; j++; } cout<<"\n\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7}; int i = 0, j = 0; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ printf("Elements of array: "); while(i<9){ printf("%d ",arr[i]); i++; } printf("\nEven numbers in array: "); while(j<9){ if(arr[j] % 2 == 0) printf("%d ",arr[j]); j++; } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 9: Displaying Odd numbers in array with if - else statement in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7}; int i = 0, j = 0; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ cout<<"Elements of array: "; while(i<9){ cout<<arr[i]<<" "; i++; } cout<<"\nOdd numbers in array: "; while(j<9){ if(arr[j] % 2 == 1) cout<<arr[j]<<" "; j++; } cout<<"\n\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7}; int i = 0, j = 0; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ printf("Elements of array: "); while(i<9){ printf("%d ",arr[i]); i++; } printf("\nOdd numbers in array: "); while(j<9){ if(arr[j] % 2 == 1) printf("%d ",arr[j]); j++; } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 10: Displaying Primes in array with if - else statement in C / C++
C++ programming:
#include <iostream> using namespace std; bool isPrimes(int n) { if (n < 2){ return false; } if (n == 2){ return true; } if (n % 2 == 0){ return false; } for (int i = 3; i < (n - 1); i += 2){ if (n % i == 0){ return false; } } return true; } int main() { int arr[]={1, 5, 9, 7, 11}; int i = 0, j = 0; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ cout<<"Elements of array: "; while(i<5){ cout<<arr[i]<<" "; i++; } cout<<"\nPrimes in array: "; while(j<5){ if(isPrimes(arr[j])) cout<<arr[j]<<" "; j++; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> bool isPrimes(int n) { if (n < 2){ return false; } if (n == 2){ return true; } if (n % 2 == 0){ return false; } for (int i = 3; i < (n - 1); i += 2){ if (n % i == 0){ return false; } } return true; } int main(void) { int arr[]={1, 5, 9, 7, 11}; int i = 0, j = 0; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ printf("Elements of array: "); while(i<5){ printf("%d ",arr[i]); i++; } printf("\nPrimes in array: "); while(j<5){ if(isPrimes(arr[j])) printf("%d ",arr[j]); j++; } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output: