Posts

Showing posts from 2017

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

Image
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. 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 ,

9.C++ Program to Find Largest Number Among Three Numbers

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. 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

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

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. 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'

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

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. 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." ;

6.C++ Program to Swap Two Numbers

C++ Program to Swap Two Numbers This example contains two different techniques to swap numbers in C programming. The first program uses temporary variable to swap numbers, whereas the second program doesn't use temporary variables. Example 1: Swap Numbers (Using Temporary Variable) #include <iostream> using namespace std ; int main () { int a = 5 , b = 10 , temp ; cout << "Before swapping." << endl ; cout << "a = " << a << ", b = " << b << endl ; temp = a ; a = b ; b = temp ; cout << "\nAfter swapping." << endl ; cout << "a = " << a << ", b = " << b << endl ; return 0 ; } Output Before swapping. a = 5, b = 10 After swapping. a = 10, b = 5 To perform swapping in above example, three variables are used. The conten

5.C++ Program to Find Size of int, float, double and char in Your System

C++ Program to Find Size of int, float, double and char in Your System his program declares 4 variables of type int, float, double and char. Then, the size of each variable is evaluated using sizeof operator. To find the size of variable,   sizeof  operator is used. sizeof(dataType); Example: Find Size of a Variable #include <iostream> using namespace std ; int main () { cout << "Size of char: " << sizeof ( char ) << " byte" << endl ; cout << "Size of int: " << sizeof ( int ) << " bytes" << endl ; cout << "Size of float: " << sizeof ( float ) << " bytes" << endl ; cout << "Size of double: " << sizeof ( double ) << " bytes" << endl ; return 0 ; } Output Size of char: 1 byte Size of int: 4 bytes Size of f