Dijlah College University: First Class ةلحرملا ا لا ىلو
Dijlah College University: First Class ةلحرملا ا لا ىلو
LAB Course 1
PROGRAMMING
Fundamentals
اساسيات البرمجة ا ل ج ا ن ب ا ل ع م ل ي
First Class
المرحلة االولى
2024-2023
1
احمد صبيح.د Programming Fundamentals/ LAB / First Class
return 0 : it return a value tells C++ program that the user has ended the program because the
program started with (int main ())
if we started with( voin main (void)) rather than (in main … return 0), the program will not
return any value
C++ Variables
int - stores integers (whole numbers), without decimals, such as 123 or -123.
double - stores floating point numbers, with decimals, such as 19.99 or -19.99.
لتطبيق البرامج مباشر على شبكة االنترنت يمكن الدخول على الرابط التالي
https://www.programiz.com/cpp-programming/online-compiler/
بدل عبارة
#include <iostream>
ستكون العبارة
#include <iostream.h>
2
احمد صبيح.د Programming Fundamentals/ LAB / First Class
int main()
{
double radius =5, PI= 3.4, area;
return 0;
}
iostream.h is used for input and output.
math.h is used for the M_PI constant and pow function.
cout and cin are used for displaying output and reading input, respectively.
double is used to declare variables to store the radius and area.
int main()
{
double radius, area;
return 0;
}
iostream.h is used for input and output.
math.h is used for the M_PI constant and pow function.
cout and cin are used for displaying output and reading input, respectively.
double is used to declare variables to store the radius and area.
3
احمد صبيح.د Programming Fundamentals/ LAB / First Class
perimeter = 4 * side;
4
احمد صبيح.د Programming Fundamentals/ LAB / First Class
Q4 Example 4 Write C++ programto find the greater number between two
numbers.
#include <iostream.h>
int main() {
double num1, num2;
cin >> num1;
cin >> num2;
if (num1 > num2)
cout << num1 << " is greater." << endl;
else if (num2 > num1)
cout << num2 << " is greater." << endl;
else
cout << "Both numbers are equal." << endl;
return 0;
}
5
احمد صبيح.د Programming Fundamentals/ LAB / First Class
F(x) =
−x, x < 0
#include <iostream.h>
int main() {
double x, result;
cin >> x;
if (x >= 0) {
if (x < 0)
cout<<endl;
cout << "F (" << x << ") = " << result << endl;
// return 0;
6
احمد صبيح.د Programming Fundamentals/ LAB / First Class
7
احمد صبيح.د Programming Fundamentals/ LAB / First Class
Q6 Example 6 Write C++ program to find the average of any three numbers.
#include <iostream.h>
int main() {
double num1, num2, num3, average;
cin >> num1;
cin >> num2;
cin >> num3;
8
احمد صبيح.د Programming Fundamentals/ LAB / First Class
Q7 Example 7 Write C++ to find the largest value of any three numbers:
#include <iostream.h>
int main() {
double num1, num2, num3, largest;
cin >> num1;
cin >> num2;
cin >> num3;
if (num1 >= num2 && num1 >= num3)
largest = num1;
if (num2 >= num1 && num2 >= num3)
largest = num2;
if (num3 >= num1 && num3 >= num2)
largest = num3;
cout << "The largest of the three numbers is: " << largest << endl;
return 0;
}
else … if الحل الثاني باستعمال
#include <iostream.h>
int main() {
double num1, num2, num3, largest;
cin >> num1;
cin >> num2;
cin >> num3;
if (num1 >= num2 && num1 >= num3)
largest = num1;
else if (num2 >= num1 && num2 >= num3)
largest = num2;
else
largest = num3;
cout << "The largest of the three numbers is: " << largest << endl;
return 0;
}
9
احمد صبيح.د Programming Fundamentals/ LAB / First Class
Q8 Example 8: Write C++ to calculate and print even numbers between 0 and 99
#include <iostream.h>
int main() {
for (int i = 0; i <= 99; i += 2) {
cout << i << " ";
}
return 0;
}
H.W
Write C++ to calculate and print Odd numbers between 0 and 99
10
احمد صبيح.د Programming Fundamentals/ LAB / First Class
Q9 Example 9 Write C++ which generates even numbers between 1000 and 2000
and then prints them in the standard output. It should also print total sum.
#include <iostream.h>
int main() {
int sum = 0;
for (int i = 1000; i <= 2000; i += 2) {
cout << i << " ";
sum += i;
}
cout << "\nTotal sum: " << sum << endl;
return 0;
}
do … while باستعمال
#include <iostream.h>
int main() {
int sum = 0;
int number = 1000;
do {
cout << number << " ";
sum += number;
number += 2;
} while (number <= 2000);
return 0; }
11
احمد صبيح.د Programming Fundamentals/ LAB / First Class
Q10 Example 10 Write C++ which to read a natural number, n, as its input to
calculates the following formula and print the result of s. s = 1⁄2 + 1⁄4 + ⋯ + 1⁄n
#include <iostream.h>
int main() {
int n;
double s = 0.0;
cout << "Enter a natural number (n): ";
cin >> n;
for (int i = 2; i <= n; i++) {
s += 1.0 / i;
}
cout << "The result of the formula is: " << s + 0.5 << endl;
return 0;
}
H.W Exercises
Q1: Write an algorithm to calculate even numbers between 9 and 100.
Q2: Write an algorithm to calculate and print odd numbers between 1 and 95.
Q3: Write an algorithm to find the sum of 50 numbers.
Q4: Write an algorithm to find the value of A, B, C.
A=X +6Y
B=2X-A
C=A +XB
Q5: Write an algorithm to print the series. 2,4,8,16,32 1024.
12
احمد صبيح.د Programming Fundamentals/ LAB / First Class
Q11: "Write a C++ program using switch case to print the name of the week days by entering
number between 1 to 7"
#include <iostream.h>
int main() {
int choice;
switch (choice) {
case 1:
cout << "Saturday" << endl;
break;
case 2:
cout << "Sunday" << endl;
break;
case 3:
cout << "Monday" << endl;
break;
case 4:
cout << "Tuesday" << endl;
break;
case 5:
cout << "Wednesday" << endl;
break;
case 6:
cout << "Thursday" << endl;
break;
case 7:
cout << "Fridsy" << endl;
break;
default:
cout << "Invalid choice." << endl;
}
return 0;
}
13