C++ Program to Separate Digits of a Positive Integer

Seperate digits of a positive integers Suppose you want to write a program in C++ which will take a positive integer from the user and output its individual digits. For example, if the input provided is 12345 then it will print 1,2,3,4 and 5 separately as integers. In this article we shall look at a few different ways of accomplishing exactly this.

First of all, look at the following program. It is probably the simplest way of separating digits of an integer. It makes use of the fact that when the division operator (/) is used with two integer operands then it gives us only the whole part of the quotient and the remainder operator (%) gives us the remainder when dividend is divided by the divisor. In this particular case, we use 10 with both these operators as divisor. When we use it with the remainder operator we get the last digit of the number entered by the user and when we use it with the division operator then we get the original number without that last digit. In this way, repeating these two steps until the original number becomes zero, we get each individual digit of the original number form right to left i.e. from least significant to most significant digit. Continue reading C++ Program to Separate Digits of a Positive Integer

Recursive C++ Program to Convert a Decimal Integer into Binary Number

Here is a very simple and recursively written C++ program to convert a decimal integer number into its binary equivalent. However it returns the binary equivalent in the form of a string of characters but you can very easily write an other function to convert this string into an actual binary number as well. Continue reading Recursive C++ Program to Convert a Decimal Integer into Binary Number

C++ program to check if a sentence is a Palindrome or not

Wikipedia defines Palindrome as, “A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction (the adjustment of punctuation and spaces between words is generally permitted).”

There are two different kinds of Palindrome. One is on a character by character basis while the other is on a word by word basis. Examples of these two type of palindromes are “Was it a rat I saw?” and “Fall leaves after leaves fall” respectively.

I have written a program in C++ which can check a word, phrase or sentence if it is a palindrome on a character by character basis. It is capable of handling complex sentences with punctuation marks such as “Dammit, I’m mad!”.

The logic I used in this program is almost self evident but still I have written a short explanation of how it works in the following lines. Continue reading C++ program to check if a sentence is a Palindrome or not

Revised Version: C++ program to check whether a number is prime or not

About two days ago, I published a post namely C++ program to check whether a number is prime or not. One of my kind readers, Jaume commented on that program and gave me some very useful suggestions and tips on improving it. I am very thankful to him for taking the time to improve my understanding. So, here is a revised version of this program. Changes made in this revision include:

  • Use of a while loop instead of the if statement to validate input numbers and accept only non negative integers.
  • Type of the variable decider has been changed from int to bool and a not operator is used to change its value instead of the increment operator.
  • Variable counter has been started from 3 instead of 2 and is incremented by 2 instead of one during each loop and devision by 2 has been made a special case.
  • and … some minor improvements in the prompts given using cout statements.

Source code of this revised program is given below. Continue reading Revised Version: C++ program to check whether a number is prime or not

C++ program to check whether a number is complete square or not

This program inputs a positive integer from user, checks whether it is a complete square or not and outputs the result to the user.

Explanation:

We use three variables in this program. n is used to store the number under check which is input by the user. i is initialized from 0 and then incremented by one during the execusion of a while loop. m is used to store the square of i during each loop. The loop executes as long as square of i i. e m is less than the number n. After this loop, an if selection statement is used to check whether this square which we have obtained as a result of execution of this loop is equal to the number n or not? if, it is, then we tell the user that the number he entered is a complete square. Otherwise we display a message on the screen stating that this number is not a complete square. Continue reading C++ program to check whether a number is complete square or not