Examples
Examples
Summary
1. A Simple C++ Program:
Let us begin with a simple example of C++ program that prints the message “h” on the screen.
Output:
Hello World
2.Variables:
In order to use a variable in C++, firstly should specifying which data type the program need to solve the
problem. The syntax to declare a new variable with specific data type (such as integer, Boolean, float, char
… etc) with a valid variable identifier is:
datatype VariableName;
For examples
int a;
float b;
int c,d,e;
Moreover, the initialization of variable can be used at the beginning of the C++ program after defining the
variable to be used with arithmetic operation using the following formula:
For example, to declare an integer variable (i.e. a) with initial value 0 at the moment in which it is
declared, it will be as:
int a = 0;
1
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
3.Arithmetic Operator:
The arithmetic operators can be used with any numeric type, except modulus can be only used with an
integer. An operand is a number or variable used by the operator. The result of an operator depends on the
types of operands. If both operands are integers, the result is an integer. If one or both operands are
double, the result is double. To perform a floating-point calculation with integers, a temporary floating-
point values must be created. This is achieved using a unary cast operator. The arithmetic opiates are
defined in the following table:
Int x,y;
x=7;
y=x%2;
cout<<y;
output: 1
For example:
In sum, count;
double average; //float average;
average = (sum)/count;
Program2: Write a C++ program to calculate the summation of a and b integer variable when a=10 and
b=20. Then print the result.
#include <iostream>
#include<stdio.h>
using namespace std;
int main()
{
int a,b,sum;
a=10;
b=20;
sum=a+b;
cout << sum;
return 0;
}
Output:
30
2
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
Example:F(x,y)=x3+2x-y
Int x=10;
Float y=3.5;
Float F;
F=(x*x*x)+(2*x)-y;
Cout<<F;
4. Input Statement:
The input statement cin is an input statement and causes the program to wait for the user to type in a
number. The number keyed in is placed in the variable based on its data types. Which are defined as:
Program3: Write a C++ program that input two integer numbers and find the multiplication between
them and print the result.
#include <iostream>
#include<stdio.h>
using namespace std;
int main()
{ int a, b; int c=0;
cin>>”enter the value of a”>>a;
cin>>”enter the value of b”>>b;
c=a*b;
cout << c;
return 0;}
Output;
enter the value of a 4
enter the value of b 2
8
3
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
Program4: Write a C++ program that reads two float numbers from the keyboard and display their
summation and average on the screen.
#include <iostream>
#include<stdio.h>
using namespace std;
int main (){
float number1, number2, sum, average;
cout << "Enter two numbers: “;
cin >> number1; // Reads numbers
cin >> number2; // from keyboard
sum = number1 + number2;
average = sum/2;
cout << "Sum = " << sum << "\n";
cout << "Average = " << average << "\n";
return (0);}
Output:
Enter two numbers:
10
20
Sum=30
Average=15
Program5: Write a program that enter two integer numbers a and b, then and swapping between these two values.
#include <iostream>
#include<stdio.h>
using namespace std;
int main (){
int a,b;
int temp;
cin>> a>>b;
temp=a;
a=b;
b=temp;
cout<<"swap"<<a<<b;
return (0);}
Output:
35
Swap 5 3
4
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
5. Conditional statement:
It is used for checking of one value stored by a variable against another value to determine whether one is
larger, smaller, or equal to the other. In other words, it allow programs to branch in a particular direction
according to the result of a test condition. It has the ability to control whether a list of statements is executed.
The conditional statements can be either:
1. If statement
• if
• if-else
• if-else-if
2. Switch statement
The simplest form of an if statement is this:
if (expression)
statement;
while if..else is used when Sometimes the condition in an if statement evaluates to false, it would be nice
to execute some code instead of the code executed when the statement evaluates to true. The "else"
statement effectively says that whatever code after it (whether a single line or code between brackets) is
executed if the if statement is FALSE. The if.. else statement use the formula
if (expression)
statement1
else
statement2
In fact, there are six relational operators: equals (==), less than (<), greater than (>), less than or equal to
(<=), greater than or equal to (>=), and not equals (!=) as explain below
Moreover, Boolean operations are used in if statement to combine more than one condition to get either 1
(TRUE) or 0 (FALSE) result, that are explains in the following table:
5
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
Such as:
For examples:
1) if ( (num >= 0) && (num <= 10) ) True only if num is between 0 and 10
2) if ( (num1 == num2) || (num1 > 0) )True only if num1 greater than 0 or equal num2,
3) if !(num1 == num2) True if num1 NOT equal to num2
Program6: Write a C++ program to check if the variable x is greater than or equal 100.
#include <iostream>
#include<stdio.h>
using namespace std;
int main (){
int x;
cin>>x;
if (x == 100)
cout << "x is 100";
else if (x>100)
cout << "x is greater than 100";
else
cout << "x is smaller than 100";
return 0;}
Output:
60
X is greater than 100
6
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
Program7: Write program to enter the day number then print the name of this day.
#include <iostream>
#include<stdio.h>
int main ()
{ int day
cin>>day;
if day==1
cout << day << “ : Sunday” ;
else
if day==2
cout << day << “ : Monday” ;
else
if day==3
cout << day << “ : Tuesday” ;
else
if day==4
cout << day << “ : Wednesday”;
else
if day==5
cout << day << “ : Thursday”;
else
cout << day << “ : Weekend day” ;
return 0;}
Program8: Write a C++ program to check if the input variable x is positive or negative.
#include <iostream>
#include<stdio.h>
int main (){
int x;
cin>>x;
if (x > 0)
cout << "x is positive";
else if(x < 0)
cout << "x is negative";
else
cout << "x is 0";
return 0;}
7
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
Output:
-30
X is negative
Program9: Write a program to enter three integer values x, y, and z. Then check largest number between
them.
#include <iostream>
#include<stdio.h>
int main (){
int x;
cin>>x>>y>>z;
if (x > y)&&(x>z)
cout << "x is larger"<<x;
else if(y > x)&&(y>z)
cout << "y is larger"<<y;
else
cout << "z is larger"<<z;
return 0;}
Output:
30 150 50
y is larger
Program10: Write a program to find the average value of each even number between the input numbers
a, b and c.
#include <iostream>
#include<stdio.h>
int main (){
int a,b,c; int sum=0, i=0;
cin>>a>>b>>c;
if (a%2==0)
{sum=sum+a; i=i+1;}
if (b%2==0)
{ sum=sum+b; i=i+1;}
if (c%2==0)
{ sum=sum+c; i=i+1;}
cout << sum/i;
return 0;}
Output:
3 4 10
7
8
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
Program11: Write a C++ program to check if the input value x is even or odd.
#include <iostream>
#include<stdio.h>
int main (){
int x;
cin>>x;
if (x%2==0)
cout << "even";
else
cout << "odd";
return 0;}
Output:
10
even
#include <iostream>
#include<stdio.h>
int main (){
for (int i = 0; i<4; i++)
cout << "Hello! "<<”\n”; 9
Asst.return
prf. Dr.0;Lahieb
} Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
Output:
Hello!
Hello!
Hello!
Hello!
#include <iostream>
#include<stdio.h>
int main (){
for (int i = 0; i<10; i++)
cout << "the value of i is: " << i << \n;
return 0; }
Output:
the value of i is: 1
the value of i is: 2
the value of i is: 3
the value of i is: 4
the value of i is: 5
the value of i is: 6
the value of i is: 7
the value of i is: 8
the value of i is: 9
the value of i is: 10
Program14: Write a program to read 10 numbers from the keyboard and find their sum and average.
#include <iostream>
#include<stdio.h>
int main (){
int x; float y; int sum=0;
for (int i=0;i<10;i++){
cin>>x;
sum=sum+x;}
y=sum/10;
cout << sum<<y;
return 0; }
10
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
Program15: Write a program to find and print the max number between 10 different numbers.
#include <iostream>
#include<stdio.h>
int main (){
int x; int max;
cin>>max;
for (int i=1;i<=9;i++)
{ cin>>x;
If x>max
max=x;}
cout << max;
return 0; }
7. While… loop:
A while loop causes your program to repeat a sequence of statements as long as the starting condition
remains true. The general formula of the while statement is:
initialization;
while ( condition )
statement;
Condition is any C++ expression, and statement is any valid C++ statement or block of statements. When
the condition evaluates to TRUE (1), statement is executed, and then the condition is tested again. This
continues until condition tests FALSE, at which time the while loop terminates and execution continues
on the first line below the statement.
8. Do..While loop:
On the other hand, the do...while loop statement executes the body of the loop before its condition is
tested and ensures that the body always executes at least one time. The syntax for the do...while statement
is as follows:
Initialization
do
statement
while (condition);
11
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
Program16: Write a program to print the count to 3 (Using while loop statement)
#include <iostream>
#include<stdio.h>
int main (){
int x = 1;
while (x <= 3) {
// cout << "x: " << x++;
cout << "x: " << x;
x=x+1; }
return 0; }
Output:
X:1
X:2
X:3
#include <iostream>
#include<stdio.h>
int main (){
int x = 1;
do
cout << "X: " << x++;
while (x <=3)
return 0; }
Output:
X:1
X:2
X:3
12
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
Program18: Write a program to evaluate the sum of first 5 positive integer numbers.
#include <iostream>
#include<stdio.h>
int main()
{int x;
int I;
int sum=0;
i=1;
While (i<=5){
Cin>>x;
If (x>=0)
{sum=sum+x;
i=i+1; } }
Cout<<sum;
Return 0;}
Program19: Write a program that input 10 integer numbers and find the summation of all even numbers
and the multiplication of all odd umbers.
#include <iostream>
#include<stdio.h>
int main (){
Int x;
Int sum=0;
Int mult=1;
Int i=1;
While (i<=10){
Cin>>x;
If (x%2==0)
sum=sum+x;
else
mult=mult*x;
i=i+1;}
Cout<<sum<<”\n”<<nul
Return 0;}
13
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
14
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
#include <iostream>
#include<stdio.h>
using namespace std;
int main (){
int x,n;
int i=0,p=1;
cin >>x>n;
if(x==0)
p=1;
else
for(i=1;i<=n;i++)
{ p=p*x; }
cout <<p << endl;
return(0);}
15
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
9. Array:
where each blank panel represents an element of the array, that in this case are integer values of type int.
These elements are numbered from 0 to 4 since in arrays the first index is always 0, independently of its
length. Like a regular variable, an array must be declared before it is used. A typical declaration for an
array in C++ is:
where type is a valid type (like int, float...), name is a valid identifier and the elements field (which is
always enclosed in square brackets []), specifies how many of these elements the array has to contain.
Therefore, in order to declare an array called billy as the one shown in the above diagram it is as simple
as:
#include <iostream>
int main(){
16
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
return 0;
10. Function
A function is a subprogram that can act on data and return a value. Every C++ program has at least one
function, main(). When your program starts, main() is called automatically. main() might call other
functions, some of which might call still others. Each function has its own name, and when that name is
encountered, the execution of the program branches to the body of that function. When the function
returns, execution resumes on the next line of the calling function.
Function Prototypes
Many of the built-in functions you use will have their function prototypes already written in the files you
include in your program by using #include. For functions you write yourself, you must include the
prototype. The function prototype is a statement, which means it ends with a semicolon. It consists of the
function's return type, name, and the parameter list. The parameter list is a list of all the parameters and
their types, separated by commas. The general Syntax of function prototype is:
Examples:
1) long FindArea(long length, long width); // returns long, has two parameters
2) void PrintMessage(int messageNumber); // returns void, has one parameter
3) int GetChoice(); // returns int, has no parameters
17
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
Program:
long Area(long l, long w) {
long a;
a=l*w;
return a; }
Program23:
void PrintMessage(int whichMsg) {
if (whichMsg == 0)
cout << "Hello.\n";
else if (whichMsg == 1)
cout << "Goodbye.\n";
else //if (whichMsg > 1)
cout << "I'm confused.\n"; }
18
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
Program24: Write a C++ program to create a function name factorial that find x!.
#include<iostream>
using namespace std;
int factorial(int n);
int main (){
int n1,fact;
cout <<"Enter the number whose factorial has to be calculated" ;
cin >> n1;
fact=factorial(n1);
cout << "The factorial of " << n1 << " is : " << fact << endl;
return(0);}
int factorial(int n){
int i=0,f1=1;
if (n<=1)
{return(1);}
else {
for(i=1;i<=n;i++)
{ f1=f1*i;}
return(f1); } }
Program25: write a C++ program to find the average between three different numbers where all variables are
integer. So, create an average function named AVR where it's accepted three integer number.
#include <iostream>
using namespace std;
float AVR (int a, int b, int c)
{ float g;
g=(a+b+c)/3;
return g; } 19
intprf.
Asst. mainDr.()Lahieb
{ Mohammed Jawad
cout << AVR (10,20,30) << '\n';
return 0; } }
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
Program26: Write a program to evaluate the series y=1+2!+3!+4!+…n!, where the factorial done above
and the result of each fact will be used as an input function called fact then.
#include<iostream>
using namespace std;
int factorial(int n);
int main (){
int n1,fact;
cin >> n;
int sum=0;
for (int i=1;i<=n;i++){
sum=sum+factorial(i);
cout<<sum;
return 0;}
int factorial(int n);
{fact1=1;
for(j=1;j<=n;j++)
fact1=fact1*j;
return(fact);}
20
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
Program27: Write a C++ program that define a function to display the array content. When the
Total number of values store in array is 10.
21
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
Assignments
1. Write a program that computes and print the value of Y using the formula.
𝑌 = 1 + 2𝑥 2 + 3𝑥 3 + 𝑛𝑥 𝑛
2. Write a program that reads 5 integer numbers and store in array named A.
That fined the factorial of each cell and store the result in new array named B.
Finally, print the content of array B.
22
Asst. prf. Dr. Lahieb Mohammed Jawad
Al-Nahrain University-Information Engineering College
Networks Engineering Department
1st Class-Computer Programming
5. Write program to enter the degree of student and found an average of degree
90-100 print excellent.
80 -90 print very good
70-80 print good
60-50 prints middle
Less 50 print fail.
𝑛 𝑥𝑖
6. Write a program to find and print the result of Y. 𝑌= ∑𝑖=1
𝑖!
23
Asst. prf. Dr. Lahieb Mohammed Jawad