Category Archives: C++ Programs

c plus plus programs

C++ Program to Check Prime Number Using Function

C++ Program to Check Prime Number Using Function

In this tutorial, we will learn to create a C++ program that will Check Prime Number Using functions 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 Check Prime Number Using Function:-

A prime number is a number that is only divisible by only one number or by itself only. In c++ programming, it is possible to take numerical input from the user and check if that number is Prime Number or not with the help of a very small amount of code.

In this program, we will take a number in the input from the user to check if it’s prime or not.

With the help of this program, we can Check Prime Number Using Function.

Algorithm:-

Program to Check Prime Number Using Function:-

Output:-

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

  • no= it will hold the integer value of the input.
  • i = it will hold the integer value.
  • flag = it will hold the integer value.

Input message for the user for the integer value.

Program Logic Code.

Printing output prime number or not.

C++ Program to Display Prime Numbers Between Two Numbers Using Functions

C++ Program to Display Prime Numbers Between Two Numbers Using Functions

In this tutorial, we will learn to create a C++ program that will Display Prime Numbers Between Two Numbers Using Functions 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.

Display Prime Numbers Between Two Numbers Using Functions:-

In today’s program, we will take the input range from the user to print the prime numbers between them. The prime numbers are those numbers that are only divisible by themselves. In c++ programming, we will check if that range has Prime Number or not and print those prime numbers.

With the help of this program, we can Display Prime Numbers Between Two Numbers Using Functions.

Algorithm:-

Program to Display Prime Numbers Between Two Numbers Using Functions:-

 

Output:-

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

  • n1= it will hold the integer value of the input.
  • n2 = it will hold the integer value of the input.
  • flag = it will hold the boolean value.

Input message for the user for the integer value range.

Program Logic Code user-defined function.

Printing output prime number or not.

C++ program to Reverse a Sentence Using Recursion

C++ program to Reverse a Sentence Using Recursion

In this tutorial, we will learn to create a C++ program that will C++ Program to Reverse a Sentence Using Recursion 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.
  • Usage of Conditional statements in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ programming.

What is Recursion? Reverse a Sentence Using it?

The recursion means a function or program that repeats itself until the condition is satisfied. The recursion repeats the program execution in a controlled format. Today we will reverse a sentence using the recursion.

Algorithm:-

Reverse a Sentence Using Recursion

In this program of recursion, we will take the input sentence from the user and store it in the string variable. Then will find the length of that string and at last, we will reverse that string using a recursion program.

With the help of the below, code we will Reverse a Sentence Using Recursion.

Program Code:-

 

Output:-

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

  • str = string type variable that will hold the value of the input sentence.

Input strings from the user for program.

Reverse printing function for the sentence.

Main Function of the program.

C++ Program to Calculate Power Using Recursion

C++ program to Calculate Power Using Recursion

In this tutorial, we will learn to create a C++ program that will Calculate Power Using Recursion 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 Power?

The power of any number is shown as an exponent value of any number. This value shows exactly how many times the number is going to multiply by itself.

Algorithm:-

Program to Calculate Power:-

In this tutorial, we will create a program that will take the base number and exponential number in input from the user and will calculate the answer using the recursion program.

The recursion is a program that repeats its execution again and again till a condition is satisfied.

Below is the program to Calculate Power Using Recursion.

Program :-

Output:-

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

  • bse = it will hold the integer value.
  • poR = it will hold the integer value.
  • rslt = it will hold the integer value.
  • j = it will hold the integer value.
  • k = it will hold the integer value.
Taking input from the user the base and the power.

Calling the function.
Body of the function.

Program Logic Code to print.

C++ Program to Find GCD Using Recursion

In this tutorial, we will learn to create a c++ program that will find GCD using recursion in c++ programming.

Prerequisites

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

  • Operators.
  • If-else statements.
  • Basic input/output.
  • Basic c++ programming
  • Recursion.
  • Nested if else statement.
  • User Define Functions.

What Is  Recursion?

Recursion is a process of repeating itself in a similar way. if a function call itself inside the same function, then it is called a recursive calling of a function.
Recursion means a  function is called itself the same function, it is known as recursion in C++.

What Is GCD?

GCD(Greatest Common Divisor) or HCF (Highest Common Factor) . When  we finding the factor of two numbers, Highest Common Factor of  numbers is the Largest factor that divides these two numbers. The greatest factor found from the common factors is called the HCF. It is also called the Greatest common factor.

C++ Program to Find GCD Using Recursion.

In this program we will find GCD of two given number using Recursion . We would first declared and initialized the required variables. Next, we would prompt user to input two numbers.Later we will find the GCD of given number using Recursion and user define function.

Output

C++ Program to Find GCD Using Recursion

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

  • num1 =It will hold first number given by user.
  • num2 =It will hold second number given by user.
  • no1= It will hold first number given by user in function.
  • no2=It will hold second number given by user in function.

Let see the step by step method of finding GCD.

GCD of two number is :
1: user will be prompted to enter two the positive integer values num1 and num2.
2: Storing the given value in variable  num1 & num2  as shown below.

3:Now pass these two value to function GCD(num1,num2) .

Where we will calculate GCD using recursion method .

As we can see in above image function GCD calling itself until it satisfied the condition and returns the Greatest common Divisor of two numbers.

C++ program to Calculate Factorial of a Number Using Recursion

C++ program to Calculate Factorial of a Number Using Recursion

In this tutorial, we will learn to create a C++ program that will Calculate Factorial of a Number Using Recursion 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 factorial?

The factorial means the product of the input number and its below integer value up to 1.

Algorithm:-

Program to Calculate Factorial Using Recursion

Today, we will create a C++ program that will take the input number from the user and then will find the factorial for the input integer value using a recursion program.

Program Code:

Output:-

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

  • no = it will hold the integer value.
Taking input from the user the base number for finding factorial.

Declaring the function.
Body of the function.

Program Logic Code to print.

C++ program to Find Sum of Natural Numbers using Recursion

In this tutorial, we will learn to create a c++ program that will Find Sum of Natural Numbers using Recursion using c++ programming

Prerequisites

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

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

What Is  Recursion?

Recursion is a process of repeating itself in a similar way. if a function call itself inside the same function, then it is called a recursive calling of a function.
Recursion means a  function is called itself the same function, it is known as recursion in C++.

What Is Natural  Number?

Simply all the counting numbers {…-3,-2-1,0,1, 2, 3, …} are commonly called natural numbers.

Natural numbers As such, it is a whole, non-negative are all number 1, 2, 3, 4,5,6,7,8,9,10… They are usually counting  number and they will continue to infinity.while Whole number are all natural numbers including 0.

C++ program to Find Sum of Natural Numbers using Recursion.

Firstly we declare required header file and variable and also initiates required values in variable. Next we take value from user at run time and then after we will find the sum of all natural number to given value using Recursion..

Output

C++ program to Find Sum of Natural Numbers using Recursion

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

  • num=to take value from user.

In this program first we take a value from user.

After that in this Program we will pass the given number to function where we add all number using Recursion (adding all natural number from 1 to 10).

Within the Function, we add all values and return num + Sum_Natural(num – 1).
After calculating values using recursion function returns sum or natural number back to main function where we print sum of all natural number.

C++ Program to Creating a Pyramid

In this tutorial, we will learn to create a c++ program that will create Pyramid using c++ programming

Prerequisites

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

  • if-else Statements.
  • Looping statement.
  • Basic input/output.
  • Operators.

What is Pyramid?

A Pyramid is a polygon structure and formed by connecting a polygonal base and a point. Each base edge and apex form a triangle.To print pyramid patterns in C++  we  will use  two nested for loops.The outermost loop is responsible for rows (*) of pyramid triangle where inner one  will print the required number of stars * in each row.

C++ Program to Creating a Pyramid.

In this program we will print Pyramid using nested  for loop. We would first declared and initialized the required variables. Then we will create the Pyramid using below program.

Output

C++ Program to Creating a Pyramid

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

  • i & j= for  iteration.
  • rows = fro number of rows.
  • k = for printing pattern.

In above program  first we declare variables i ,j ,k and rows , here we use 2 for loops.The outer loop will run for number of rows or pyramid and the inner loop print the number of stars to be displayed in each row.This is displayed using the following code.

The outermost for loop here is responsible for the number of rows to be printed for star (*) pyramid triangle whereas inner one loop will print the required number of stars * pattern for each row.

And the final output of our program is like that.

C++ Program to Create Floyd’s Triangle

In this tutorial, we will learn to create a c++ program that will create a Floyd’s Triangle using c++ programming.

Prerequisites

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

  • if-else Statements.
  • Looping statement.
  • Basic input/output.
  • Operators.

What is Floyd’s Triangle ?

It is named after Robert Floyd who created this Triangular shape. Floyd’s triangle is a triangular array of  numbers.It is a method of showing ,filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner.

C++ Program to Creating a Floyd’s Triangle

In this program we will print Floyd’s Triangle using nested  for loop. We would first declared and initialized the required variables. Then we will create the Pyramid using below program.

Output

C++ Program to Create Floyd's Triangle

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

  • i & j= for  iteration.

In above program  first we declare variables i and j, here we use 2 for loops.The outer loop will run for number of rows or pyramid and the inner loop print the Floyd’s Triangle to be displayed in each row.This is displayed using the following code.

And the final output of our program is like that.

C++ Program to Print Pascal Triangle

In this tutorial, we will learn to create a c++ program that  will create Pascal’s Triangle using c++ programming.

Prerequisites

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

  • if-else Statements.
  • Looping statement.
  • Basic input/output.
  • Operators.

What Pascal Triangle?

The most interesting Numbers Pattern ,named after Blaise Pascal, a French Mathematician.Triangular arrangement of numbers that gives the coefficients in the expansion of any binomial expression.

The triangle has some interesting patterns.Let us see an example, drawing parallel “shallow diagonals” and adding the numbers on each line together produces the Fibonacci series.

C++ Program to Creating  Pascal Triangle.

In this program we will print  Pascal Triangle using nested  for loop. We would first declared and initialized the required variables. Then we will create  Pascal Triangle using below program.

Output

C++ Program to Print Pascal Triangle

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

  • i & j= for  iteration.
  • rows= for number of rows.
  • coef= for Coefficient .
  • space= for printing space.

Pascals triangle is a triangular array of the binomial coefficients.All values outside the triangle are zero (0). Program takes number from user to input total number of rows and using nested for loop to print pascal’s triangle.As shown below

Here  the inner loop creates the  space and the second inner loop computes the value of binomial coefficient, creates indentation space and prints the binomial coefficient for that particular column.

Let see the concept of creating a pascal triangle in step-by-step .

1 – Take value for number of rows to be printed in rows.
2 – Outer loop  runs for n times to print rows.
3 – Inner loop run for j to (rows – 1)
4 – That will print blank space ” “.
5 – ending the inner loop.
6 – Than another loop iterate form  j to i.
7 – Where we check condition=>
if (j == 0 || i == 0)
coef = 1
else
coef = coef*(i-j+1)/j.
8 – in each loop if condition satisfied we will print  coefficient value till the total number of rows.

The program output is come  out as shown.

C++ Program to Print Diamond of Star

In this tutorial, we will learn to create a c++ program that will create Diamond of Stars (*) using c++ programming.

Prerequisites

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

  • if-else Statements.
  • Looping statement.
  • Basic input/output.
  • Operators.

Diamond of Star.

Using C++ language to print diamond of stars, here we need to print two triangle one in upward direction and one in reverse, simply print first triangle and second triangle is reverse of first triangle.

C++ Program to Creating Diamond of Star.

In this program we will print Diamond of Star.Using nested  for loop. We would first declared and initialized the required variables. Then we will create Diamond of Star using below program.

Output

C++ Program to Print Diamond of Star

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

  • i & j= for  iteration.
  • rows= for number of rows.
  • space= for printing space.

Here  we will use two nested for loop First nested loop creates  upper triangular part  up-to 3 lines

whereas its lower triangular part  up-to 2 (one less than row size) lines.

To print diamond pattern in our program, user will be prompted to enter the number of rows. Now using the row size we will print diamond pattern as shown in the program  below.

C++ Program to Print Alphabet Pattern

C++ Program to Print Alphabet Pattern

In this tutorial, we will learn to create a C++ program that will Print Alphabet Pattern 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.
  • Basic arithmetic operations.
  • For loop in C++ Programming.

In C++ programming we can print many patterns with the help of codings. In today’s program, we will create the pattern using the alphabet. First, we will take the number of rows in input and then we will print the triangle of the alphabets using the for loop.

Algorithm:-

 

Program:-

To Print Alphabet Pattern.

 

Output:-

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

  • i = it will hold the integer value to control parent for loop.
  • j = it will hold the integer value to control child for loop.
  • incr  = it will hold the integer value.
  • count = it will hold the integer value.

Taking the input rows from the users.

Printing the patterns using the alphabet.