Category Archives: C++ Programs

c plus plus programs

C++ Program to Print Number Pattern

C++ Program to Print Number Pattern

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

The C++ language is a very powerful programming language. In C++ programming we can perform many operations with the help of codings. The C++ language is very easy to create any pattern. In this program, we will learn to Print Number Patterns with the help below code.

Algorithm:-

 

Program:-

To Print Number 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.
  • rw = it will hold the integer value.

Taking the input rows from the users.

Code for the pattern of the number.

Printing the rows of patterns.

 

C++ Program to Print Triangle of Star

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

Triangle of Star

To print Triangle Star patterns in C++ , we will use two nested loops. The outermost loop is responsible for the number of rows in star (*) pyramid triangle,whereas inner one  loop will print the required number of stars * in each row.

C++ Program to Creating a Triangle of Star.

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

Output

C++ Program to Print Triangle of Star

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 number of stars 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 check number positive or negative

In this tutorial,you will learn to create a C++ Program to check whether a number is positive or negative using c++ programming.

Prerequisites

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

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

What is Positive and Negative Number.

A Number which is  greater than zero it is a positive number (Number>0).
And a Number which is  less  than zero it is negative number (Number<0).
Or A number which is  equivalent  to zero it is Not positive nor negative  number (Number==0).

C++ Program to check whether a number is positive or negative

In this program we will find whether a number is positive, negative or zero series using if else statement. We would first declared and initialized the required variables. Next, we would prompt user to input any number. Later we will find that number is positive,negative or zero.

Output

Positive Value.

C++ Program to check number positive or negative

Negative Value.


Zero Value.

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

  • num = it will hold the value of number.

In the next statement user will be prompt to enter any number terms which will be assigned to variable ‘num’.

For finding given integer is positive ,negative or zero we will use We will use nested if else statement.

in above image within the  if condition we check  if(num > 0), number is greater than zero then number is Positive.

if the given number is less then Zero else if(num < 0) then number is negative.

else the number is zero.

In this program we  find that  a number is positive, negative or zero.The above approach is easiest way find the problem. First we check condition for positive number, then for negative or and later we find for zero.

C++ Program to Check Leap Year

In this tutorial, we will learn to create a c++ program that will find the given year is Leap year of not using c++ programming.

Prerequisites

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

  • C++ Operators
  • Conditional statement.
  • Basic input output .

What is Leap Year?

Example:

So logic behind to check whether a year is leap or not is:
Logic of Leap Year:
If the given value is completely divisible by 4, 100 and 400 then it is a leap year.
If the given value is divisible by 4 but not  by 100 then it is a leap year.

Logic for  not a Leap Year:
If the given value is not divisible by 4 then it is not a leap year
If the given value is divisible by 4 and 100 but not divisible by 400 then it is not a leap year.

For example,

  • 1221 is not a leap year.
  • 2000 is a leap year.
  • 1890 is not  a leap year.

Program to Check given year is a Leap Year or not.

Here fist we take a year from user and with help of logical operator(&& and ,|| or) we will check multiple condition in a single if statement and get the required result.

Output

C++ Program to Check Leap Year

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

  • year =taking year value from user.

 

First of all take a value from user to check whether a year is leap or not is,

then we check if the given value is completely divisible by 4, 100 and 400 then it is a leap year.


If the given value is divisible by 4 but not  by 100 then it is a leap year. As shown in above .So this the logic behind of finding given year is a leap year or not.

C++ Program to Find Perfect Number

In this tutorial, we will learn to create a c++ program that will find what is A Prefect Number 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.
    • C++ While Loop.
    • Basic input/output.

What Is Perfect Number?

Perfect number is a positive integer number,that equals to sum of its positive divisors not include the number itself is equal to that number.

Example

Divisor of  28 are 1 + 2 + 4 + 7 + 14. and sum of these  are (1+2+4+7+14=28). So we can say that 28 is a perfect number in which we will not induced 28 i.e exclude 28 .So number is  called perfect when divisor of number added and that create the number itself called Perfect number.

C++ Program to Find Perfect Number.

So here we are going to create a program in which we take a value from user and we will find the given number is perfect or not? By method using adding all divisors of a number.

Output

C++ Program to Find Perfect Number

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

  • num  =  holding given number
  • sum= adding all divisor. 
  • i       = for iteration of a loop.

Explanation of program, First we take the integer value in ‘num’ variable. So the condition is that A Perfect number is a number which is equal to sum of its divisor.

Example, 6’s divisor  are 1, 2 and 3. and there sum is (1+2+3=6) ,the divisors of  6. So the number 6 is called as perfect number.

From the above image of a perfect number in sum,Suppose  user entered value is: no = 6
Step by step explanation for loop is here.

In 1st Iteration…
Number is num= 6, sum = 0 and loop start at i = 1.
If (no % i == 0)  // if( 6%1== 0 ) true.
here number is complete divisible So,
sum = sum + i  // sum= 0 + 1 =1.

Now,Second Iteration..
Values of  sum and i has altered as sum = 1 and i = 2.

If (no % i == 0) // if( 6 %2 ==0 ) true.
sum = sum + i // sum = 1 + 2 = 3.

Third Iteration.
Again the values of both sum and i has altered as sum = 3 and i = 3

If (6 % 3 == 0)  // if( 6%3==0) true.
sum = sum + i //sum = 3 + 3 = 6.

Next fourth and fifth iterations ,condition inside the if will fail
6 % 4 == 0 (FALSE)
6 % 5 == 0 (FALSE)

In next iteration, the value of i becomes 6, So (6 < 6) false . So, the compiler will terminate the for loop.
Then, we check whether the value of sum  is exactly equal to the no or Not.

If it is TRUE, the below cout<<” is a perfect number”  statement will execute. else cout<<” is not a perfect number” statement will execute.

C++ Program to Display Armstrong Number Between Two Numbers

C++ Program to Display Armstrong Number Between Two Numbers

In this tutorial, we will learn to create a C++ program that will Display Armstrong Number Between 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.
  • While loop in C++ programming.

Program to Display Armstrong Number Between Two Numbers:-

In today’s tutorial, we will create a program that will find the Armstrong numbers between the given number range. First will take the number frame from the user and then will find the Armstrong Number.

With the help of this program, we can Display Armstrong Number Between Two Numbers.

Algorithm:-

 

Program to Display Armstrong Number Between Two Numbers:-

 

Output:-

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

  • num1 = it will hold the integer value.
  • num2 = it will hold the integer value.
  • flag = it will hold the integer value.
  • digit = it will hold the integer value.
  • num = it will hold the integer value.
  • sum = it will hold the integer value.
  • i = it will hold the integer value.

Taking the input integer number interval from the user.

Calculating the Armstrong numbers.
Printing the output numbers.

C++ Program to Check Armstrong Number

C++ Program to Check Armstrong Number

In this tutorial, we will learn to create a C++ program that will Check Armstrong 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.
  • While loop in C++ programming.

Program to Check Armstrong Number:

In today’s tutorial, we will create a program that will find the Armstrong numbers from the given number. First will take the number from the user and then will find the Armstrong Number.

With the help of this program, we can Check Armstrong Number.

Algorithm:-

Program to Check Armstrong Number:-

 

Output:-

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

  • ognum = it will hold the integer value.
  • rmdr = it will hold the integer value.
  • result = it will hold the integer value.
  • num = it will hold the integer value.

Taking the input integer number from the user.

Checking the Armstrong numbers.
Printing the output.

C++ Program to Display Prime Numbers Between Two Numbers

In this tutorial, we will learn to create a C++ program that will check that provided number is Prime or Not 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.
  • Looping statements.

What Is Prime number?

Any positive integer number which is greater than 1 (Num > 1 ) and only it can be divisible by itself and by 1 and it has max two factors is called as prime number.Rather we can say that a prime number is a number which only divided by himself or 1.

Example:->

Note:  2  is the only positive(even) and smallest Prime number.

primeno

In our program we will find Prime numbers between two numbers using for loop.

C++ Program to Display Prime Numbers Between Two 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 that the given value is prime number or not.Let’s take a look to the program.

Output

C++ Program to Display Prime Numbers Between Two Numbers

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

  • l = it will hold lower value.
  • u=it will hold upper limit.
  • i and j= for iteration.
  • count = count will print prime numbers is count=1.

In the next statement user will enter two number which will be assigned in variables ‘l’ and ‘u’.

lower and upper limit of loop Now loop start from l to given number(u).In ever iteration we will check  the numbers between the interval  l and u . For each number with in the for loop, will be checked if this number is prime value of count =1 if not the value of count remain zero(0).

If count=1 it is a prime then print the number.

Then the next number in the loop is checked, till all numbers  between  l and u are checked.

And finally we print all the number between given numbers.

C++ Program to Check Whether a Number is Prime or Not

C++ Program to Check Whether a Number is Prime or Not

In this tutorial, we will learn to create a C++ program that will Check Whether a Number is Prime or Not 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 Whether a Number is Prime or Not:-

In c++ programming, it is possible to take numerical input from the user and check if that number is Prime Number or not. The prime number means a number that is only divisible by itself.

With the help of this program, we can Check Whether a Number is Prime or Not.

Algorithm:-

Program to Check Whether a Number is Prime or Not:-

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 Check character is Vowel or Consonant

In this tutorial, we will learn to create a C++ Program to Check character is Vowel or Consonant. In this example we will check whether the entered character by the user is a vowel or a consonant using C++ programming.

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

  • Operators
  • Condition statement.
  • logical operator.
  • Basic input/output.
  • Basic C++ programming.

What Is Vowel and consonant?

Vowel :=> Sound that allowing breath to flow out of mouth, without closing any part of the mouth is called as vowel.
Consonant:=> Sound  made by blocking air from flowing out of the mouth with the teeth or throat is called as Consonant.

Example

As we know A E I O U are called vowels. And renaming alphabets except these 5 vowels are called consonants.

Program to Check the given value is Vowel or consonant.

So here we creating a program in which we will find the given character is vowel or consonant.
As we know a,e,i,o,u are only vowel and rest are consonant.So simply we will check these and if given character belongs to them  then it is a vowel  and if not its consonant simple isn’t it .let’s have a look at program.

Output

C++ Program to Check character is Vowel or Consonant.

Note: Here we assumes that the user will enter an alphabet. If not so then non-alphabetic character, it displays the character is neither a vowel nor a consonant.

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

  • ch= hold given character.

First of all user prompted to enter any character which will be assign in ch variable.

Then with the help of if-else statement we will check the inputted character is a vowel or consonant, using following logic

As user inputs any character we will check whether it’s a vowel both in lower-case and upper-case condition we check if  belongs to in vowel or consonant. if it is vowel we will show following message.

If a character doesn’t belong to this condition then it is consonant ,

and if  doesn’t meet any of them vowel and consonant it might be a digit or a special symbol.

So in this tutorial we have learn to find given character is vowel or consonant.

C++ Programs to Check Palindrome String

In this tutorial, we will learn to create a c++ program that  checks Palindrome String  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.
  • String
  • mathematical functions.
  • in-built functions.

What Is Palindrome of String?

A palindrome of a String that reads the same backward and forward.

Example : dad , nayan , pop, abcba and  etc…

C++ Programs to Check Palindrome String.

Output

Palindrome string.

C++ Programs to Check Palindrome String

Not a Palindrome string.

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

  • strng =it will hold given string.
  • i= for iteration.
  • cmp= for holding true false value.
  • len = it will hold length of string.

In the next statement user will be prompted to enter a string which will be assigned to variable ‘strng’.

after that with the help of strlen() function we find the length of string.

Now for loop iterator upto ‘len’ number of terms. Inside the loop we compare string.

First character in the strng is the same as last character in the same string.
is the first character is the same as ‘len-i-1’th character and  so on .This process continues till length of a string .

If any above condition fails, the cmp varibale is set to  cmp=1 which is  true(1), which implies that the string is not a palindrome.

If above condition satisfied, the cmp variable is all ready  set to  cmp=0 which is  false(0), which implies that the string is a palindrome.

C++ Programs to Check Given Number is Palindrome or not

C++ Programs to Check Given Number is Palindrome or not

In this tutorial, we will learn to create a C++ program that will Check Given Number is Palindrome or not 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.

What is a palindrome number?

The Palindrome number means a number that is the same when we read it from forward and reverse.

For example:-

  1. 1221 this is a palindrome number is the same when we write it in reverse order 1221.
  2. 123 this is not a palindrome number it is not the same when we write it in reverse order 321.

Algorithm:-

Program to Check Whether a Number is Palindrome or Not:-

To check whether a number is a Palindrome Number or not. In today’s C++ program we will write code for checking. First, we will take a number in input and then pass it into the program to Check Whether a Number is Palindrome or Not.

With the help of the below program, we can Check Palindrome or Not.

Program  Code:-

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 Palindrome number or not.