Category Archives: C++ Programs

c plus plus programs

C++ Program to perform addition, subtraction, multiplication and division using Switch

C++ Program to Perform Addition, Subtraction, Multiplication, and Division using Switch

In this tutorial, we will learn to create a C++ program. After that will perform Addition, Subtraction, Multiplication, and Division 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 for performing the Addition, Subtraction, Multiplication, and Division:- 

Every Programming language is having many predefined functions for arithmetic operations. In the C++ programming language, we will do the Addition, Subtraction, Multiplication, and Division operations with the help of Arithmetic operators using the switch case.

Algorithm for procedure:-

 

Program for Addition, Subtraction, Multiplication and Division:-

Output:-

 

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

  • no1= it will hold the input number value from the user.
  • no2 = it will hold the input number value from the user.
  • op = it will hold the character value for the operator.

Taking Input operator from the user.

Taking input number from the user.

Program code for operations.

 

C++ Program to Perform Addition, Subtraction, Multiplication and Division

C++ Program to Perform Addition, Subtraction, Multiplication, and Division

In this tutorial, we will learn to create a C++ program. After that will perform Addition, Subtraction, Multiplication, and Division 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 for performing the Addition, Subtraction, Multiplication, and Division:- 

In today’s tutorial, we will create a program. That will do the Addition, Subtraction, Multiplication, and Division operations with the help of Arithmetic operators in C++ programming.

Algorithm for Addition, Subtraction, Multiplication, and Division:-

 

Program for Addition, Subtraction, Multiplication, and Division:-

Output:-

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

  • fno = it will hold the input number value from the user.
  • sno = it will hold the input number value from the user.
  • addd = it will hold the added value of the numbers.
  • subb= it will hold the subtraction value of the numbers.

Taking Input numbers from the user.

Perform Addition, Subtraction, Multiplication, and Division of two numbers.

Printing the output.

 

 

C++ Programs to Find LCM of Two Numbers

In this tutorial, we will learn to create a c++ program that will find LCM of two numbers using c++ programming.

Prerequisites

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

  • Operators.
  • Looping statements
  • While loop.
  • if else Statement.
  • Basic input/output.
  • Basic c++ programming.

What Is LCM?

LCM is a method to finding the smallest possible multiplication of two or more numbers. LCM of a numbers is divisible by both or more numbers. For example, LCM of 5 ,4 and 20 is 20.

C++ Programs to Find LCM of Two Numbers.

In this program you will learn  to find LCM of two given number using while loop. We would first declared and initialized the required variables. Next, we would prompt user to input two numbers.Later we will find the or given number.

Output
C++ Programs to Find LCM of Two Numbers

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.
  • max=This will find biggest among the number.

Let see the step by step method of finding LCM.

LCM of two given number, is as follows:

1: user  will be prompted to enter two the positive integer values no1 and no2.


2: Storing the biggest value of num1 & num2 in  max variable.3: On checking condition whether the max is divisible by both variables num1 and num2  loop  will iterate until it will find a value which divisible by both number.


4: If variable max is divisible,Then display max as the LCM of these two numbers.

5: Else, the value of max is increased, and go to step 3 again and again.
6: Stop the program.

LCM stands for Least Common Multiple (LCM). It’s the smallest possible number that is completely divisible by both integer’s num1 and num2, without leaving any remainder means zero(0). It is also known as the Lowest Common Multiple.

C++ Program to Find GCD

In this tutorial, we will learn to create a C++ program that  will find GCD or HCF of two number  using C++ programming.

Prerequisites

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

  • Operators.
  • General C programming.
  • Basic input/output.
  • Looping statements
  • While loop.
  • if else Statement.

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.

In this program we will find GCD of two given number using For loop. 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.

Output

C++ Program to Find GCD

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.
  • gcd= it will hold Greatest common divisor.

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: Initializethe value of  gcd to one gcd=1.


4:Within the loop we check if  i  divide completely these two numbers num1 and num2.
i.e. i exactly divides the given numbers and remainder is equal to zero, then the value of i is assigned to gcd variable.
5:When the loop is completed, the greatest common divisor of two numbers is stored in variable gcd.
6: At lst we will print the value of gcd .

The   of two integers is the greatest integer that can exactly divide both numbers (with a remainder ==0) .

C++ Programs to Write BuzzFizz Program

In this tutorial, we will learn to create a C++ program that  will find BUZZ FIZZ numbers using C++ programming.

Prerequisites

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

  • Operators
  • Basic C++ programming.
  • Basic input/output.
  • Looping statements.
  • For Loop.
  • If else statement.

What Is Buzz Fizz?

The concept of Buzz and Fizz is that if integers value between 1 to given number,we will print “Fizz” if an integer number is divisible by 3, “Buzz” if an integer number is divisible by 5, and “FizzBuzz” if an integer number  is divisible by both 3 and 5.

Example:

In a program that shows the numbers from 1 to number(suppose 15) .If number is a multiple of ‘3 ’ we print “Fizz” instead of if multiples of ‘5’ we print “Buzz” and if multiple of both 3 and 5 then we print “Fizz Buzz”.Following sereis will shoe Fizz anf Buzz in place or multiples.Have a look.
1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14,
Fizz Buzz.

Buzz Fizz Program in C++.

In this program we will print find  Buzz Fizz  using for loop. We would first declared and initialized the required variables. Next, we would prompt user to input the number of terms. Later we will  find  Buzz Fizz series of specified number of terms.

Output

C++ Programs to Write BuzzFizz Program

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

  • i     = for iteration of loop.

In our  program we will print  number from 1 to 20. If number with in series is multiple of 3 and 5, we print “FizzBuzz” instead of the number. as you can see in image below.

Andif number is multiple of 3, print “Fizz” instead of the number.

 

And in final case if numbers which are multiples of  5, print “Buzz” instead of the number.

And in else condition when number is not multiple of 3 and 5 we simply print the number .So in this program we  run loop from 1- 20.the final out come  series  is look like this.

C++ Program to Calculate Sum of Natural Numbers

In this tutorial, we will learn to create a C++ program that  will find Sum of N Natural Numbers  using C++ programming.

Prerequisites

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

  • Operators.
  • looping statements.
  • Basic input/output.
  • Basic c++ programming.

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 Calculate Sum of Natural Numbers

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.

Output

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

  • i= for iteration of loop.
  • num=to take value from user.
  • sum = for calculating sum of natural number.

In this program first we take a value from user,

After that in this Program we add all natural number from 1 to 10.

    • The first we will create  an integer variable i,num and sum.
    • then, we use For Loop to iterate between i=1 to i<=10 and

    • Within the For loop, we add all i values in variable sum. sum+=i.
    • In our  program we calculate sum of natural value from 1- 10.
    • At last we print the sum of 10 natural values.

    that’s all fro this post thank-you.

C++ Programs to Calculate Sum Of Digits

C++ Program to Print Sum of Digits

In this tutorial, we will learn to create a C++ program that will Print Sum of digits in 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 in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ programming.
  • While loop in C++ programming.
  • Conditional Statements in C++ programming.
  • Arithmetic operations in C++ Programming.

Program to Print Sum of digits in a given Number

In c++ programming, we will make a program in today’s tutorial. We will take integer input from the user and find Sum of digits in a given number with the help of a program.

With the help of this program, we can Print Sum of digits in a given number.

Algorithm:-

Program to Print Sum of digits in a given number:-

Output:-

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

  • n = it will hold the integer value of the input.
  • sum = it will hold the integer value for sum.
  • m = it will hold the integer value.

Input number from the user.

Program Logic Code.

Printing output for sum the digits.

C++ Program to Find ASCII Value of a Character

C++ Program to Find ASCII Value of a Character

In this tutorial, we will learn to create a C++ program that will find the ASCII Value of a Character 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 Find ASCII Value of a Character:-

American Standard Code For Information Interchange stands for ASCII. The ASCII is an encoded format. In c++ programming, we can take the input from the user in character format. In today’s program, we will take input in character format. Secondly, we will find the ASCII Value of a Character the input with the help of a small program. The ASCII values are the most useful code in any programing language to know the characters.

Algorithm:-

 

Program:-

Output:-

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

  • c = it will hold the integer value of the Remainder.

Taking Input divisor and dividend from the user.

Calculating the quotient and the remainder.

Printing the output.

C++ Programs to Find Cube Root of Number

In this tutorial, we will learn to create a C++ program that will find Cube Root of 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.
  • Basic input/output.
  • In-built functions.
  • mathematical functions.

What Is cube root?

Cube root , is a factor that we multiply by itself three times to get that number (2*2*2=8).A number which produces  a number when cubed.

C++ Programs to Find Cube Root of Number.

Firstly we declare required header file and variable and also initiates required values in variable. Here in this program we are going find cube root of a given number first of all we take a value from user and pass this value to in-built function to find cube root of a number.

Output

C++ Programs to Find Cube Root of Number

Function cbrt() takes only single  parameter(num) and returns the cube root.which we hold in cube_root variable.

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

  • num = it will hold  given number.

with the help of cbrt() in-built function we can find the cube of given number of user.

Step by step explanation of this program.

1:First we declare  variable num.
2 :Then  take a value from user and assign to num.


3 : Pass no variable to cube_root function (cbrt(num)).
4 : This function returns value to cube root .
5 : Print the value of cube_root.


Explanation of Cube root, cube root of a number is a value ,when multiplied by itself 3 times . For example, the cube root of 27, denoted as 3√27, is 3, because when we multiply 3 by itself thrice times we get 3 x 3 x 3 = 27 = 33.

C++ Programs to Find Square Root of Number

In this tutorial, we will learn to create a C++ program that will find Square Root of 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.
  • Basic input/output.
  • In-built functions.
  • mathematical functions.

What Is Square root?

Squares is found when numbers multiplying by itself Twice 5 * 5= 25. Where a square root of a number on getting multiplied by itself gives the original √25 = 5.
A square root of a number a is a number b such that =a.

C++ Programs to Find Square Root of Number.

Firstly we declare required header file and variable and also initiates required values in variable. Here in this program we are going find Square root of a given number first of all we take a value from user and pass this value to in-built function to find Square root of a number.

Output

C++ Programs to Find Square Root of Number

Function sqrt() takes a single  argument(num) and returns the square  root.

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

  • num = it will hold  given number.

with the help of sqrt() in-built function we can find the square root of given number of user.

Step by step explanation of this program.

1:First we declare  variable num.
2 :Then  take a value from user and assign to num.


3 : Pass no variable to sqrt_root function (sqrt(num)).
4 : This function will find the square of given number .
5 : Print the value of sqrt(num).

Explanation of Square root.

So square root of a number is getting by multiplied itself gives the original as we can see in here √36 =6 .
so square root of 36 is 6. square root are written always in radical symbol (√).

C++ Program to Display Factors of a Number

In this tutorial, we will learn to create a C++ program that will find Factors 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.
  • If else statement.
  • For Loop.
  • Basic input/output.
  • Basic c++ Programming.

What Is Factor of number is?

Factors of a number are numbers that divide the number completely and remainder of that number(Divisor) is equal to zero .

Example:->

The number 6 has four factors: 1, 2, 3,  and 6 because all this number divide the number six completely.

C++ Program to Display Factors of a Number

In this program we find factor of given number using if -else statement and for loop. First we would declared and initialized the required variables. Next, we would prompt user to input the number . Later we will find factors of a given number.

Output

C++ Program to Display Factors of a Number

In this program  we will take a positive integer from user by prompting user

and with the help of loop and if else statement  we will displays all the positive factors of that number.

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

  • num = It will hols number given by user.
  • = for iteration of loop and finding  factor.

So finding the factors of a number  we will run a loop from 1 to num, incrementing  the loop value by 1 in each iteration.

The loop structure should look like this

Within the loop  in each iteration we check the condition that i is a factor of num or not. To check factor we use divisibility of number by using  modulo division.

i.e. if( num % i == 0) then i is a factor of num If i is a factor of num then we will print the value of i.So we will get the required factors of given number.

C++ Program to Multiply two Numbers

C++ Program to Multiply Two Numbers

In this tutorial, we will learn to create a C++ program that will Multiply 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.

Program to Multiply Two Numbers:- 

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 integer format. In today’s program, we will take input in number format. Secondly, we will Multiply the number with the help of a small program and the arithmetic operator “*”. The numbers are the most useful terminology in any programing language. For 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 C++ programming.

With the help of this program, we can take input and Multiply Two Numbers.

Algorithm to Multiply the numbers:-

 

Program to Multiply Two Numbers:-

Output:-

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

  • fno = it will hold the input number value from the user.
  • sno = it will hold the input number value from the user.
  • multiply = it will hold the sum value of the numbers.

Taking Input numbers from the user.

Calculating the multiply of two numbers.

Printing the output.