Category Archives: C++ Programs

c plus plus programs

C++ Program to Calculate Power of a Number

In this tutorial, we will learn to create a C++ program that will find the Power of a given number  using C++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators.
  • Precedence.
  • Looping statements.
  • Basic input/output functions.
  • Basic c++ programming.

Calculating Power of a Number.

let’s have a look Logic behind to finding the power of any number  using  C++ programming.

Example

Input:
take  value at  base : 3
take  value at exponent : 3
So power of any number basen given as base*base*…..*base (n-times). Here base is called base and n is called the exponent.Here we will write a C++ program to find the power of a given number.

Program to find power of any number.

In this program we will find power of any given number using for loop. We would first declared and initialized the required variables. Next, we would prompt user to input the number and power of number .Then we will find the power of given number.

Output

C++ Program to Calculate Power of a Number

Point to remember : The above program will works only if the Power is a positive integer (number).

In the above program, we have first declared and initialized a set variables required in the program.

  • base = it hold the value of base 
  • expo = it will hold the power 
  • power= it will hold the result.

Let see the logic of program step by step.

1. Input base and exponents from user, and put  it in two variables  base and expo.


2. Initialize variable that will store power to one (power=1).
3.within the loop value of i start from 1 to expo, increment loop  by 1 in each iteration. The structure  look like.


4.For every iteration the value of  power  multiply with base.


5.Finally after iteration we get the answer in variable Power.

In this tutorial we understand how to write a program without using Power Function In C++ to calculate power of a  given number.

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

In this tutorial, we will learn to create a C++ program that will find Second Order Quadratic Equation using C++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C ++ programming topics:

  • Operators
  • Basic input/output.
  • Basic C Programming
  • Inbuilt library functions.

What Is Second Order Quadratic Equation?

In particular, it is a second-degree polynomial equation, since it has the greatest power is two.In algebra said a quadratic polynomial, a polynomial of degree 2.

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

In this program we will Find All Roots of a Quadratic Equation. First of all we declared and initialized the required variables.Next,we would prompt user to input the values of a,b and c then with the help of sqrt() function we will calculate the first and second root or equations.

Output

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

In the above program, we have first declared and initialized a set variables required in the program.

  • a,b,c = for holding coordinates
  • rt1= shows first root.
  • rt2= shows second root.

In this program first of we prompted user to input values of a,b and c

after getting the values of a,b and c and with the help of inbuilt function sqrt() and mathematical formula we know to calculate all Roots of a Quadratic Equation.

whose value define in math.h header file we going to find the Second Order Quadratic Equation.

as we know the formula of Quadratic Equation. we will use formula to get first and second root of given coordinates using c++ program.

C++ Programs to Find Greatest Number

C++ Program to Find the Greatest Number

In this tutorial, we will learn to create a C++ program that will Find Greatest Number using C++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ programming.
  • Conditional statement in C++ programming.

Program to Find the Greatest Number

In c++ programming, it is possible to take multiple integer inputs from the user and Find the Greatest Number with the help of a very short program. The C++ language has many types of header libraries which has supported function in them with the help of these files the programming is easy. But today we will Find the Greatest Number from the taken numbers from the user using c++ programming.

Algorithm:-

With the help of this program, we can Find the Greatest Number.

Program to Find the Greatest Number:-

 

Output:-

In the above program, we have first initialized the required variable.

  • no1 = it will hold the integer value.
  • no2 = it will hold the integer value.
  • no3 = it will hold the integer value.

Input numbers for the program.

Program Logic Code.

Printing output for the greatest number.

C++ Program to Find Factorial

In this tutorial, we will learn to create a C++ program that  will Find Factorial of a number  using C++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators.
  • Basic input/output.
  • Basic c++ programming
  • If-else Statement.

What is Factorial ?

Factorial of a number n is the product of all the positive integers numbers.

Example: The factorial of 6 is 120.
6! = 6* 5 * 4 * 3 * 2 *1
6! = 720.

Factorial of 0 and 1 is 1.and  factorial of negative number doesn’t exist.

C++ Program to Find Factorial

In this program we will find factorial of a given number . We would first declared and initialized the required variables. Next, we would prompt user to input a number .Later we will Find Factorial.

Output

In the above program, we have first declared and initialized a set variables required in the program.

  • num = it will hold entered number.
  • fact = it will hold a factorial of a number.
  • i =  for iteration.

In our program, we will find factorial of a given number.
First of all we take a number as input from user and store the value in variable num.

And now we will check first that the given number is greater than 1 or not if not we show message in below.

And after that in else we will fin the factorial of a given number.

In our program above, the user is promoted to enter a positive integer.In the our program, the loop runs from 1 to num. In each iteration , fact is multiplied with i. fact=fact*i and final value of fact is the product of all numbers between 1 to num.

C++ Programs to Generate Fibonacci Series

C++ Programs to Generate Fibonacci Series

In this tutorial, we will learn to create a C++ program that will Reverse Array Element Using Function using C++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ programming.
  • For loop in C++ Programming.
  • Functions in C++ Programming.

What is Fibonacci Series?

The Fibonacci series is a series that is strongly related to Binet’s formula (golden ratio). in the Fibonacci series, the series starts from 0, 1 and then the third value is generated by adding those numbers,

Algorithm:-

Programs to Generate Fibonacci Series:-

In this program, we take the number of elements for the series from the user. Then we will generate the Fibonacci series from the below program code.

Program:-

Program to Generate Fibonacci Series

Output:-

In the above program, we have first initialized the required variable

  • t1 = it will hold the integer value.
  • t2 = it will hold the integer value.
  • nterm = it will hold the integer value.
  • n = it will hold the integer value.
  • i = it will hold the integer value to control the array.

Generating the fibonacci series.

Program Code for Printing the series.

C++ Programs to Find Number Of Digits

C++ Program to Find the number of digits.

In this tutorial, we will learn to create a C++ program that will Find the Number Of Digits in C++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ programming.
  • String functions in c++ programming.

Find the number of digits in the number.

As we all know the c++ programming is a very powerful language. In the c++ programming language, we have many pre-defined functions for arithmetic operations. but today we will count the number of digits in a number without using the mod operator.

With the help of this program, we can find the number of digits in numbers.

Algorithm Find the Number Of Digits:-

Program to Find the Number Of Digits:-

 

Output:-

In the above program, we have first initialized the required variable.

  • n = it will hold the long integer.
  • flag = it will hold the integer value for numbers.

Taking input number to the user.

 

Calling function for reverse and printing

Finding the number of digits with the help of using a user-defined function.

C++ Programs to Reverse any Number

C++ Programs to Reverse any Number

In this tutorial, we will learn to create a C++ program that will Reverse any Number using C++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ programming.
  • For loop in C++ programming.
  • Conditional Statements in C++ programming.
  • Arithmetic operations in C++ Programming.

Program to Reverse any Number:-

In c++ programming, it is possible to take integer input from the user and Reverse any Number with the help of a C++ program. The reversing of a number means the digits of a number will be swapped from the first position to the last position. The C++ language has many types of header libraries that have supported functions in them with the help of these files the programming is easy.

With the help of this program, we can Reverse any Number.

Algorithm:-

Program to Reverse any Number:-

Output:-

In the above program, we have first initialized the required variable.

  • num= it will hold the integer value of the input.
  • n = it will hold the integer value.
  • digit = it will hold the integer value.
  • rev = it will hold the integer value.

Input message for the user for the integer value.

Program Logic Code.

Printing output Reverse any Number.

C++ Programs to Print Table of any Number

C++ Program to Print Table of any Number

In this tutorial, we will learn to create a C++ program that will Print a Table of any Number in C++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ Programming.

Program to Print Table of any Number:- 

As we all know the integer, float, or double value in c++ programming. In c++ programming, we can take the input from the user in numbers format. In today’s program, we will take input in number format. Secondly, we will Print a Table of any number with the help of a small program. The numbers are the most useful terminology in any programing language. For number/arithmetic manipulation in c++ programming, there are many predefined functions available.

Integers are the most use-full data types in any programming language. The integer values are used with operators to perform the best operations in programming.

With the help of this program, we can take input and print the Table of any Number.

Algorithm:-

 

Program:-

Output:-

In the above program, we have first initialized the required variable.

  • n = it will hold the input number value from the user.
  • i  = it will hold the integer value.

Taking Input numbers from the user.

Calculating the table of given numbers and Printing the output.

C++ Program to Generate Multiplication Table

C++ Program to Generate Multiplication Table

In this tutorial, we will learn to create a C++ program that will Generate a Multiplication Table in C++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ Programming.

Programfor generating the table:- 

As we all know the integer, float, or double value in c++ programming. In c++ programming, we can take the input from the user in numbers format. In today’s program, we will take input in number format. Secondly, we will generate a Multiplication Table with the help of a small program. The numbers are the most useful terminology in any programing language. For number/arithmetic manipulation in c++ programming, there are many predefined functions available.

Algorithm:-

Program:-

Output:-

In the above program, we have first initialized the required variable.

  • n = it will hold the input number value from the user.
  • i  = it will hold the integer value.

Taking Input numbers from the user.

Calculating the table of given numbers and Printing the output.

C++ Program to Find Largest Number Among Three Numbers

C++ Program to Find Largest Number Among Three Numbers

In this tutorial, we will learn to create a C++ program that will find Largest Number Among Three Numbers using C++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ programming.
  • Conditional statement in C++ programming.

Program to Find Largest Number Among Three Numbers

In c++ programming, it is possible to take numerical input from the user and find Largest Number Among Three Numbers with the help of a very small amount of code. The C++ language has many types of header libraries which has supported function in them with the help of these files the programming is easy. But today we will find Largest Number Among Three Numbers using c++ programming.

Algorithm:-

With the help of this program, we can find Largest Number Among Three Numbers.

Program to find the Smallest of Three Numbers:-

 

Output:-

In the above program, we have first initialized the required variable.

  • no1 = it will hold the integer value.
  • no2 = it will hold the integer value.
  • no3 = it will hold the integer value.

Input numbers for the program.

Program Logic Code.

Printing output for the greatest number.

C++ Programs to Swap two numbers

In this tutorial, we will learn to create a C++ program that will Swap two numbers in C++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ programming.

Swapping of Numbers:-

In swapping we swap the data of two variables. In this program, we will use the third variable to swap the data of the first and second variables.

Let us understand with an example.

Algorithm for the program:-

C++ Programs to Swap two numbers

In every programming language, numbers play a very important role in a programming language. As we all know the c++ is a very powerful language. With the help of the c++ programming language, we can make many programs. We can perform many input-output operations using c++ programming. In today’s tutorial, we take the input numbers from the user. Then we will swap two Numbers with the help of the third variable in the c++ programming language.

With the help of this program, we can Swap two numbers.

Program to swap two Numbers:-

 

Output:-

In the above program, we have first initialized the required variable.

  • first= it will hold the integer value.
  • second = it will hold the integer value.
  • flag = it will hold the integer value.

Printing the numbers before the swapping of values.

Swapping the numbers using a third variable with simple logic.
Printing the output swapped numbers from the program.

 

C++ Programs to Check Even and Odd Number

C++ Program to Check Even and Odd Number

In this tutorial, we will learn to create a C++ program that will Check Even and Odd numbers using C++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ programming.
  • Conditional Statements in C++ programming.
  • Arithmetic operations in C++ Programming.

What is an even and odd number?

Even numbers are those numbers that are completely divisible by 2. which means the reminder should be zero. And odd numbers are those numbers that cannot be completely divisible by 2.

Algorithm:-

Program to Check Even and Odd Number

In this program, we will take an integer input number from the user, and then we will use it in the program with conditional statements to find whether the number is even or odd. As per the program calculations.

With the help of this program, we can Check Even and Odd numbers.

Program:-

Output:-

In the above program, we have first initialized the required variable.

  • n = it will hold the integer value.

Input number from the user.

 

Checking if even number.

Checking for an odd number.