Professional Documents
Culture Documents
Programming1 3
Programming1 3
Integer
These are whole numbers, both positive and
negative. Unsigned integers (positive values only)
are supported. In addition, there are short and long
integers.
int sum;
sum = 20;
sum != Sum
Lesson 3
Examples:
Given the following variables, which are
VALID and INVALID.
1. num1
2. 123name
3. 2age
4. status
5. salary
Lesson 3
Syntax:
File Inclusion
void main(){
data_type identifier_lists;
Program Statements;
}
Lesson 3
Global Variables
Syntax:
File Inclusion
data_type identifier_lists;
void main(){
Program Statements;
}
Lesson 3
Where:
data_type can be one of the four basic data types:
int, float, double, or char.
identifier_lists is the lists of valid variable names.
Examples:
int sum; int is the data type and sum is
the variable name.
float amount; float is the data type and
amount is the variable name.
Lesson 3
Which is Correct?
Variable Formatters
%d decimal integer
%c character
%s string or character array
%f float
%e double
Lesson 3
C/C++ Operators and Input
Functions
C/C++ has three groups of operators:
Arithmetic, Relational and Logical
operators. It has also some predefined
input functions such as scanf() in stdio.h
and cin inside iostream.h header file.
Lesson 3
The C/C++ Operators
Arithmetic Operators
* Multiplication
/ Division
+ Addition
- Subtraction
% Modulo (Integer Remainder)
Lesson 3
The C/C++ Operators
Additional
++ Increment
-- Decrement
Precedence Rule:
( ), *, /, % Higher Priority
+, - Lower Priority
Lesson 3
Examples:
Values : 10 15
_
Lesson 3
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
main(){
int sum, value;
sum = 5;
value = 10;
printf("Value 1 : %d\nValue 2 : %d\n", sum, value);
}
Lesson 3
Screen Output:
Value 1 : 5
Value 2 : 10
_
Lesson 3
Relational Operators
Logical Operators
EXERCISES
Simulate/ trace the following programs
and find the screen output.
Lesson 3
Program 1
#include <stdio.h>
#include <conio.h>
#include <iostream>
Using namespace std;
void main(){
int a = 20, b = 5;
cprintf(“Series 1\n”);
a = a + b;
printf(“%d %d %d”, a, b, a * b);
}
Lesson 3
Program 2
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
main(){
char l = ‘c’;
int X = 12, Y = X + 3;
Y = Y + 5;
printf(“Series 2\n”);
printf(“Values : %d and %d”, X * X, Y );
}
Lesson 3
Program 3
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
main(){
int c= 2, d = 4, e= 5;
cprintf(“Series %d”,d);
d = c + e; c = d + c;
cprintf(“Lists : %d %d %d”, c, d, e);
}
Lesson 3
The Input Functions
The scanf() function
-- it is a function in C language used to accept input
from the user.
Syntax:
scanf(“<formatter>”,&variable_name);
Example:
scanf(“%d”,&num); scanf(“%d”,&x);
Example Program 1:
Examples:
cout<<“Hello World!”;
cout<<x;
Example Program:
#include <stdio.h>
#include<conio.h>
#include<iostream.h>
void main(){
clrscr();
cout<<“Welcome to C/C++”<<endl;
cout<<“\”Programming is easy”<<endl;
cout<<“but it needs patience and”<<endl;
cout<<“determination...\””;
}
cout<<“Welcome to C/C++”<<endl;
cout<<“\”Programming is easy”<<endl;
cout<<“but it needs patience and”<<endl;
cout<<“determination...\””;
Output:
Welcome to C/C++
“Programming is easy
but it needs patience and
determination…”
The cin function
It is another way to accept input from
a user aside from the scanf() function.
cin function is inside the iostream.h
header file, therefore before using it
make sure that iostream.h header file
is included in the program.
Syntax:
cin>>variable_lists;
Examples:
cin>>x;
cin>>n1>>n2;
Example Program 1:
LABORATORY
EXERCISES
Solve the following machine problems.
Save your programs in a USB following
the filenames indicated in each of the
problems.
Lesson 3
SAMPLE OUTPUT:
SAMPLE OUTPUT:
EX2.CPP
Lesson 3
3. Create a program that computes the area of a triangle. Inputs of the program
is the base and the height.
Formula: Area of Triangle = ½ Base Height
SAMPLE OUTPUT:
COMPUTATION OF AREA OF TRIANGLE
EX3.CPP