CPE 112-Chapter2 - Updated2019
CPE 112-Chapter2 - Updated2019
CPE 112-Chapter2 - Updated2019
CMPE112,CTPR112,ITEC112,MISY112,IT112,COMP112,CPE112
Computer Programming
return 0;
}
C Program Output
return 0;
}
Variables
A variable is a storage location which contains some
known or unknown quantity or information, particularly a
value generated during the run-time of the program.
Correct Incorrect
ali74 74Ali
monthly_Salary Monthly salary
firstNameOfCustomer first-name-of-customer
multiply *
Variable definition
In order to use a variable in C, we must first declare it specifying
which data type we want it to be.
Examples Examples
int num; num=2; int x=85, n=5;
int num=2;
double y=0.5, z=3.14;
char word; word=‘a’;
char word=‘a’; char w=‘k’; key=‘C’;
...
1230H
1231H 5 x
1232H
1233H
1234H
...
Examples
C Program C Program Output
return 0; return 0;
} }
C Program Output
return 0;
}
Standard Input
cin is our standard input function and the keyboard of a
computer is the standard input device.
Arithmetic operators
Operation Operator Expression
Multiplication * b*m
Division / x/y
Modulus % r%s
Addition + f+7
Subtraction - p-c
a=b+3 Operands: a b 3
Operators: = +
Comparison Operators
Equality & relational operators
a <= 3 Operands: a 3
Operators: <=
Expressions
Assignment operators
Assume: int c = 3, d = 5, e = 4, f = 6, g = 12;
Operator Expression Explanation Assigns
+= c += 7 c=c+7 10 to c
-= d -= 4 d=d-4 1 to d
*= e *= 5 e=e*5 20 to e
/= f /= 3 f=f/3 2 to f
%= g %= 9 g=g%9 3 to g
a += 3 Operands: a 3
Operators: +=
Expressions
Increment & decrement operators
Assume: int x = 3, y = 3, a = 9, b = 9
Operator Expression Explanation Firstly Secondly
assigns assigns
++ c= x++ + 7 c= (x++) + 7 10 to c 4 to x
++ c= ++y + 7 c= (++y) + 7 4 to y 11 to c
-- c= a-- + 7 c= (a--) + 7 16 to c 8 to a
-- c= --b + 7 c= (--b) + 7 8 to b 15 to c
Exercise 1:
Write a complete C program for the following problem; Get
two numbers from the user, and calculate and display the
average of numbers.
Exercise 2:
Write a complete C program for the following problem; Get
three numbers from the user, and calculate and display the
product (multiplication) of numbers.
Exercise 3:
Write a complete C program for the following problem; Get
a number from the user, and calculate and display the
square of that number.
𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁 = 5 => 𝑆𝑆𝑆𝑆𝑆𝑆𝑆𝑆𝑆𝑆𝑆𝑆 𝑜𝑜𝑜𝑜 𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁 = 52 = 5 ∗ 5 = 25
#include <iostream>
using namespace std;
int main(void)
{ Steps of trace x dg output
int x,dg;
cin>>x; 1 863
2 863/100=8
dg = x / 100; 3 863%100=63
x = x % 100;
cout<< dg ; 4 8
5 63/10=6
dg = x / 10; 6 63%10=3
x = x % 10;
7 6
cout<< dg ;
8 3/1=3
dg = x / 1; 9 3%3=0
x = x % 1; 10 3
cout<< dg ;
return 0;
}
Exercise 6:
Write a complete C language program to evaluate the
following equation. Users should be able to input x and z
from the keyboard. Then, calculate and display the value
of y. The formula is defined for floating point numbers.
𝑥𝑥−2 ×4
a) 𝑦𝑦 =
𝑧𝑧+8
𝑥𝑥 − 𝑧𝑧/3
b) 𝑦𝑦 =
2𝑧𝑧−7
Trace & Output Type
Questions
Throughout the course, you will come across to a point
where you will be given the source code and asked to
find the output.
OUTPUT:
Exercise 8:
Write a C program that will ask the user to enter his/her
name, surname and student number. Accept up to 41
characters for name and surname.
The program will display the entered name, surname
and student number on the screen
Exercise 8 - Sln:
Exercise 9:
Write a C program to receive 3 numbers from
keyboard and print them in reverse order on the
screen.
Exercise 9 - Sln:
Exercise 10:
What is the output of the following C program?
#include <iostream>
= i + j * 2 –i / 3
using namespace std;
int main(void)
{
= 14 + 5 * 2 –14/3
int i=14, j=5, k=3;
int answer=0;
= 14 + 10 –4
answer=i+j*2-i/3;
= 24 –4
cout<<”answer = ”<< answer;
return 0;
= 20
}
Revision
First C program
Variables
Expressions
Arithmetic operators
Assignment operators
Exercises