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
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
This program inputs a positive integer from user, checks whether it is a complete square or not and outputs the result to the user.
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
I have used three variables in this program namely number, counter and decider. All of them are of integer type. number is used to store the number entered by the user which is to be checked. counter is initialized at 2 and then it is incremented by 1 in each loop and it is used to test the division of number against all integers between 2 and the number itself but excluding itself also. The so called variable decider is used to make decision during the last steps of the program while it is being told to the user that the number he enetered is a prime number.
The number entered by the user is also tested once to make sure that it is a positive number number. If it is not hten the user is promted to enter the number a second time. Hoewver, if the user again enters a negative number then now he will get a wrong output. So it is important to enter only positive numbers while using this program.
If number entered by user is either 0 or 1 then he simply gets a message that the number entered by him is neither prime nor composite.
However, if the number entered is greater than or equal to 2 then testing will start from the while loop. In each iteration of this loop, remainder will be calculated when number is divided by counter. If this remainder equals zero then user will be told that the number entered is not a prime number, the variable decider will be incrimented by 1 and the loop will exit after the break statement is encountered. But if the remainder number % counter is not equal to zero then the variable counter will be incremented by 1 and the loop will start again. As soon as the the variable counter will become equal to the variable number, the execution of the loop will stop. After the loop, their is an if statement. It tests the variable decider if it is equal to zero or not. If it is, then, user will be told that the entered number is a prime number. Please not that decider will not eual zero (i.e. it will be greater than zero) only if it has been tested and told to the user that number is not prime. In this case, it ofcourse makes sense to skip the statements inside this last if statement. If you still have any confusion about any step of this program or you have some alternative way of doing any of these steps then please feel free to share it with me via comments. I shall be very thankful to you. Continue reading C++ program to check whether a number is prime or not
I am just beginning in the world of computer programming with python as my first language. Yesterday, I wrote a very simple program which, when executed, ask us for a number whose table we want to print, then a number from where we want to start the table and finally a number up to which the table will be printed. The source code of the program is:
def table(a, b, l):
while a<=l and b>1:
result = b*a
print b, "X", a , "=", result
b = input("Tell me the number whose table you want to print... ")
a = input("Start the table from? ")
l = input("Print up to? ")
table(a, b, l)
A sample execution of this program in my python shell looks something like this:
>>> execfile('D:Python Practicepract.py')
Tell me the number whose table you want to print... 7
Start the table from? 1
Print up to? 10
7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70