Use of Operators

Here are sample programs which will further explain the use of operators in programming.

Problem Statement:

Write a program that takes a four digits integer from user and shows the digits on the screen separately i.e. if user enters 7531, it displays 1,3,5,7 separately.

Solution:

Let’s first analyze the problem and find out the way how to program it.

Analysis:
First of all, we will sort the problem and find out how we can find digits of an integer. We know that when we divide a number by 10, we get the last digit of the number as remainder. For example when we divide 2415 by 10 we get 5 as remainder. Similarly 3476 divided by 10 gives the remainder 6. We will use this logic in our problem to get the digits of the number. First of all, we declare two variables for storing number and the digit. Let’s say that we have a number 1234 to show its digits separately. In our program we will use modulus operator ( % ) to get the remainder. So we get the first digit of the number 1234 by taking its modulus with 10 (i.e. 1234 % 10). This will give us the digit 4. We will show this digit on the screen by using cout statement. After this we have to find the next digit. For this we will divide the number by 10 to remove its last digit. Here for example the answer of 1234 divided by 10 is 123.4, we need only three digits and not the decimal part. In C we know that the integer division truncates the decimal part to give the result in whole number only. We will use integer division in our program and declare our variable for storing the number as int data type. We will divide the number 1234 by 10 (i.e. 1234 / 10). Thus we will get the number with remaining three digits i.e. 123. Here is a point to be noted that how can we deal with this new number (123)? There are two ways, one is that we declare a new variable of type int and assign the value of this new number to it. In this way we have to declare more variables that means more memory will be used. The second way is to reuse the same variable (where number was already stored). As we have seen earlier that we can reassign values to variables like in the statement x = x + 1, which means, add 1 to the value of x and assign this resultant value again to x. In this way we are reusing the variable x. We will do the same but use the division operator instead of addition operator according to our need. For this purpose we will write number = number / 10. After this statement we have value 123 in the variable number.

Again we will get the remainder of this number with the use of modulus operator, dividing the number by 10 (i.e. 123 % 10). Now we will get 3 and display it on the screen. To get the new number with two digits, divide the number by 10. Once again, we get the next digit of the number (i.e. 12) by using the modulus operator with 10, get the digit 2 and display it on the screen. Again get the new number by dividing it by 10 (i.e. 1). We can show it directly, as it is the last digit, or take remainder by using modulus operator with 10. In this way, we get all the digits of the number.
Now let’s write the program in C by following the analysis we have made. The complete C program for the above problem is given below. It is easy to understand as we are already familiar with the statements used in it.


The output of the program will be as following.

Problem Statement:
Write a program that takes radius of a circle from the user and calculates the diameter, circumference and area of the circle and display the result.

Solution:
In this problem we take the input (radius of a circle) from the user. So that we can use cin statement to prompt the user to enter the radius of a circle. We store this radius in a variable. We also need other variables to store diameter, circumference and area of the circle. To obtain the correct result, we declare these variables of type float, instead of int data type, as we know that the int data type stores the whole numbers only. Here in our problem the area or circumference of the circle can be in decimal values. After getting the radius we use the formulae to find the diameter, circumference and area of the circle and then display these results on the screen.

The solution of this program in coding form is given below.

A sample output of the above program is given below.

Tips:
  • Use descriptive names for variables
  • Indent the code for better readability and understanding
  • Use parenthesis for clarity and to force the order of evaluation in an expression
  • Reuse the variables for better usage of memory
  • Take care of division by zero
  • Analyze the problem properly, and then start coding (i.e. first think and then write)

For previous lesson click here: Examples of Expressions
For next lesson click here: Conditional Statements


the easiest way to learn programming
introduction to programming
Use of Operators

0 comments:

Post a Comment

 

introduction to programming Copyright © 2011-2012 | Powered by Blogger