Posts

Showing posts from July, 2017

C++ Program to Check Leap Year

This program checks whether an year (integer) entered by the user is a leap year or not. To understand this example, you should have the knowledge of following C++ programming topics: C++ if, if...else and Nested if...else All years which are perfectly divisible by 4 are leap years except for century years (years ending with 00) which is leap year only it is perfectly divisible by 400. For example: 2012, 2004, 1968 etc are leap year but, 1971, 2006 etc are not leap year. Similarly, 1200, 1600, 2000, 2400 are leap years but, 1700, 1800, 1900 etc are not. In this program below, user is asked to enter a year and this program checks whether the year entered by user is leap year or not. Read More  C++ Example: Check if a year is leap year or not #include <iostream> using namespace std ; int main () { int year ; cout << "Enter a year: " ; cin >> year ; if ( year % 4 == 0 ) { if (

C++ Program to Calculate Sum of Natural Numbers

In this example, you'll learn to calculate the sum of natural numbers. To understand this example, you should have the knowledge of following C++ programming topics: C++ for Loop Positive integers 1, 2, 3, 4... are known as natural numbers. This program takes a positive integer from user( suppose user entered  n  ) then, this program displays the value of 1+2+3+....+n. Read More  C++ Example: Sum of Natural Numbers using loop #include <iostream> using namespace std ; int main () { int n , sum = 0 ; cout << "Enter a positive integer: " ; cin >> n ; for ( int i = 1 ; i <= n ; ++ i ) { sum += i ; } cout << "Sum = " << sum ; return 0 ; } Output Enter a positive integer: 50 Sum = 1275 This program assumes that user always enters positive number. If user enters negative number,  Sum = 0  is displayed and program is t

C++ Program to Find All Roots of a Quadratic Equation

This program accepts coefficients of a quadratic equation from the user and displays the roots (both real and complex roots depending upon the determinant). To understand this example, you should have the knowledge of following C++ programming topics: C++ if, if...else and Nested if...else For a quadratic equation  ax 2 +bx+c = 0  (where a, b and c are coefficients), it's roots is given by following the formula. The term  b 2 -4ac  is known as the determinant of a quadratic equation. The determinant tells the nature of the roots. Read More  C++ If determinant is greater than 0, the roots are real and different. If determinant is equal to 0, the roots are real and equal. If determinant is less than 0, the roots are complex and different. Example: Roots of a Quadratic Equation #include <iostream> #include <cmath> using namespace std ; int main () { float a , b , c , x1 , x2 , determinant , realPart , imaginaryPa

C++ Program to Find Largest Number Among Three Numbers

In this example, you'll learn to find the largest number among three numbers using if, if else and nested if else statements. To understand this example, you should have the knowledge of following C++ programming topics: C++ if, if...else and Nested if...else In this program, user is asked to enter three numbers. Then this program finds out the largest number among three numbers entered by user and displays it with proper message. Read More  C++ This program can be used in more than one way. Example 1: Find Largest Number Using if Statement #include <iostream> using namespace std ; int main () { float n1 , n2 , n3 ; cout << "Enter three numbers: " ; cin >> n1 >> n2 >> n3 ; if ( n1 >= n2 && n1 >= n3 ) { cout << "Largest number: " << n1 ; } if ( n2 >= n1 && n2 >= n3 ) { cout

C++ Program to Check Whether a character is Vowel or Consonant.

In this example, if...else statement is used to check whether an alphabet entered by the user is a vowel or a constant. To understand this example, you should have the knowledge of following C++ programming topics: C++ if, if...else and Nested if...else Five alphabets a, e, i, o and u are known as vowels. All other alphabets except these 5 alphabets are known are consonants. Read More  C++ This program assumes that the user will always enter an alphabet. Example: Check Vowel or a Consonant Manually #include <iostream> using namespace std ; int main () { char c ; int isLowercaseVowel , isUppercaseVowel ; cout << "Enter an alphabet: " ; cin >> c ; // evaluates to 1 (true) if c is a lowercase vowel isLowercaseVowel = ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ); // evaluates to 1 (true) if c is an uppercase vowe

C++ Program to Check Whether Number is Even or Odd

In this example, if...else statement is used to check whether a number entered by the user is even or odd. To understand this example, you should have the knowledge of following C++ programming topics: C++ if, if...else and Nested if...else Integers which are perfectly divisible by 2 are called even numbers. And those integers which are not perfectly divisible by 2 are not known as odd number. To check whether an integer is even or odd, the remainder is calculated when it is divided by 2 using modulus operator  % . If remainder is zero, that integer is even if not that integer is odd. Read More  C++ Example 1: Check Whether Number is Even or Odd using if else #include <iostream> using namespace std ; int main () { int n ; cout << "Enter an integer: " ; cin >> n ; if ( n % 2 == 0 ) cout << n << " is even." ; else cout << n << &qu