Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 24

Computer Programming(cp-1)

Lecture# 3-4
Use of Asthmatic Operators
Reading Material
Deitel & Deitel C++ How to
Program

chapter 1
1.22
In the Previous Lecture
Basic structure of C program
Variables and Data types
Operators
cout and cin for output and
input
Precedence

Highest: ()
Next: *,/,%
Lowest: +,-
Proper use of ()
What is the result of 10 + 10 * 5 ?
a*b%c +d
a*(b%c) = a*b%c

?
Examples of
Expressions

x = ax + by + cz2 (in Algebra)


x=a*x+b*y+c*z*z (in C)

(use of parenthesis)

x = (a * x) + (b * y) + c * ( z * z)
Quadratic
Equation
In algebra
y = ax2 + bx + c

In C
y = a*x*x + b*x + c
Discriminant
b2 - 2a
4c
= b*b - 4*a*c /2 *a Incorrect
answer
Solution

= (b*b - 4*a*c) /(2 *a) Correct answer


No expression on the left
hand side of the assignment
Integer division truncates
fractional part
Liberal use of
brackets/parenthesis
Interesting
Problem

Given a four-digit
integer, separate and
print the digits on the
screen
Solution
Lets first analyze the problem
and find out the way how to
program i
Analysis
Number = 1234

Take the remainder of the above number after dividing by 10


Eg 1234 / 10 gives remainder 4
1234 % 10 = 4
Remove last digit
1234/10 = 123.4
123 (Truncation due to Integer Division)
123 %10 gives 3
Remove last digit
123/10 = 12.3
12 (Truncation due to Integer Division)
12 % 10 gives remainder 2
Remove last digit
12/10 = 1.2
1 (Truncation due to Integer Division)
Final digit remains
#include <iostream.h>
Code
main ( )
{
int a;
int digit;
cout << Please enter a 4 digit integer : ;
cin >> number;
digit = number %10;
cout <<The digit is: << digit << \n; // first digit; and then << \n
number = number / 10;
digit = number % 10;
cout <<The digit is: << digit << \n;
number = number / 10;
digit = number % 10;
cout <<The digit is: << digit << \n;
number = number / 10;
digit = number % 10;
cout <<The digit is: << digit;

}
Special Character
Newline

\n
TIPS
Dont forget semicolon at the end
of each statement

C Language is case sensitive so


variable names x and X are two
different variables
TIPS
Write one statement per line

Type parentheses ( ) and braces


{ } in pairs

Use parentheses for clarification


in arithmetic expressions
TIPS
Use spaces in the coding to make
it easy to read and understand

Reserved words can not be used


as variable names

There is always a main( ) in a C


program that is the starting point
of execution
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.
#include <iostream.h>
main ()
{
// declare variables
float radius, diameter, circumference, area;
// prompt the user for radius of a circle
cout << "Please enter the radius of the circle " ;
cin >> radius ;
// calculate the diameter, circumference and area of the circle
// implementing formula i.e. diameter = 2 r circumference = 2 pi r and area = pir2
diameter = radius * 2 ;
circumference = 2 * 3.14 * radius ; // 3.14 is the value of (Pi)
area = 3.14 * radius * radius ;
// display the results
cout << "The diameter of the circle is : " << diameter ;
cout << "The circumference of the circle is : " << circumference ;
cout << "The area of the circle is : " << area ;
}
A sample output of the above
program is given below
Please enter the radius of the
circle 5
The diameter of the circle is : 10
The circumference of the circle
is : 31.4
The area of the circle is : 78.5
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
Tips
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)

You might also like