Tag: C++ examples

C++ Program to Calculate Arithmetic Mean

Problem Write a program in C++ to calculate arithmetic mean for the numbers entered by the user. Solution We have a formula to find the arithmetic mean if there are n sets of numbers: am = (n1+n2+n3+…+nn)/n The letters am stand for arithmetic mean, whereas n1, n2, n3, and nn stand for first, second, third, and fourth numbers, respectively. Here’s a sample code snippet demonstrating…

C++ Program to Find if a Number is Odd or Even

Problem Write a program in C++ language to find if the given number is odd or even. Solution A number is even if it is a positive number and if it is fully divisible by 2. Here’s a sample code snippet demonstrating how to find out if the number is odd or even in C++. Output

C++ Program to find Square Root of a number 

Problem Write a program in C++ to find the square root of a number in C++. Solution To find the square root of a number in C++, you can use the sqrt() function. The sqrt() function defined in the math.h header file can be used to calculate the square root in C++. This function takes a number as an argument and returns its square root….

C++ Program to Calculate Compound Interest

Problem Write a program in C++ programming language to Calculate Compound Interest. Solution Interest on interest is the process of adding interest to the principal amount of a loan or deposit. It is the outcome of reinvesting interest rather than paying it out, so that interest is received on the principle amount plus previously collected interest in the next period. Here’s a sample code snippet…

C++ Program to Calculate Multiplication of two Numbers

Problem Write a C++ Program to Multiply two Numbers. Solution The user is prompted to enter two numbers in this program. The product of those two numbers is then stored in a variable and shown on the screen. Here’s a sample code snippet demonstrating how to calculate Calculate Multiplication of two Numbers in C++. Output

C++ Program to Display ASCII Value of a Character

Problem Write a C++ Program to Display ASCII Value of a Character. Solution In C programming, a character variable stores an ASCII value (an integer between 0 and 127) rather than the character itself. For example, ASCII value of ‘B’ is 66. This means that if you assign ‘B’ to a character variable, 66, rather than ‘B,’ is stored in that variable. Here’s a sample…

C++ Program to Swap Two Numbers without using third variable

Problem Write a C++ Program to Swap Two Numbers without using third variable. Solution Below is sample source code in C++ to Swap Two Numbers without using third variable.  Output Enter 1st number:Enter 2nd number:Before swapping, Numbers are:a = 2, b = 1 After swapping, Numbers are:a = 1, b = 2