0% found this document useful (0 votes)
4 views13 pages

Dijlah College University: First Class ةلحرملا ا لا ىلو

The document is a programming lab manual for a course on Programming Fundamentals at Dijlah College University. It includes various C++ programming examples, such as printing 'Hello World', calculating the area of a circle, finding the sum of two numbers, and using control structures like if-else and switch-case. Additionally, it provides exercises and homework assignments for students to practice their programming skills.

Uploaded by

mdyctbf4r7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

Dijlah College University: First Class ةلحرملا ا لا ىلو

The document is a programming lab manual for a course on Programming Fundamentals at Dijlah College University. It includes various C++ programming examples, such as printing 'Hello World', calculating the area of a circle, finding the sum of two numbers, and using control structures like if-else and switch-case. Additionally, it provides exercises and homework assignments for students to practice their programming skills.

Uploaded by

mdyctbf4r7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

‫ احمد صبيح‬.

‫د‬ Programming Fundamentals/ LAB / First Class

Dijlah College University


Department of Computer

LAB Course 1
PROGRAMMING
Fundamentals
‫اساسيات البرمجة ا ل ج ا ن ب ا ل ع م ل ي‬

First Class
‫المرحلة االولى‬

‫ احمد صبيح توفيق‬.‫د‬

2024-2023

1
‫ احمد صبيح‬.‫د‬ Programming Fundamentals/ LAB / First Class

Q : Write C++ program to print “ Hello World”


#include <iostream.h>
int main()
{
cout << "Hello World My name is " ;
return 0;
}
This program uses the iostream library to output the "Hello, World!" message to the console. The std::
prefix is used to qualify elements from the standard C++ library. The main function is the entry point of
the program, and return 0; indicates successful execution.

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.

 char - stores single characters, such as 'a' or 'B'. ...

 string - stores text, such as "Hello World". ...

 bool - stores values with two states: true or false.

‫ اتبع الموقع التالي‬C++ ‫لمزيد من المعلومات لالطالع على لغة‬


https://www.programiz.com/cpp-programming

‫لتطبيق البرامج مباشر على شبكة االنترنت يمكن الدخول على الرابط التالي‬
https://www.programiz.com/cpp-programming/online-compiler/

‫بدل عبارة‬
#include <iostream>

‫ستكون العبارة‬
#include <iostream.h>

2
‫ احمد صبيح‬.‫د‬ Programming Fundamentals/ LAB / First Class

Q1 : Example 1: write C++ program to Find the area of a Circle of radius r.


#include <iostream.h>

int main()
{
double radius =5, PI= 3.4, area;

area = radius * radius * PI;

cout << "The area = " << area << endl;

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.

‫ باستخدام الدوال‬Q2 ‫طريقة ثانية لحل السؤال السابق‬


#include <iostream.h>
#include <math.h>

int main()
{
double radius, area;

cin >> radius;

area = M_PI * pow (radius, 2);

cout << "The area = " << area << endl;

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

Q2 Example 2 Write an C++ program to read two numbers and


find their sum.

#include <iostream.h> // For Borland C++ 4.5


int main() {
double num1, num2, sum;
cin >> num1;
cin >> num2;
sum = num1 + num2;
cout << "The sum = " << sum ;
return 0;
}
In this program:
 iostream.h is used for input and output.
 cout and cin are used for displaying output and reading input, respectively.
 double is used to declare variables to store the numbers and their sum.

Q3 Example 3 Write C++ to find the Area and Perimeter of square:


#include <iostream.h> // For Borland C++ 4.5
int main() {
double side, area, perimeter;

cin >> side;

area = side * side;

perimeter = 4 * side;

cout << " area = " << area << endl;


cout << " perimeter = " << perimeter << endl;
return 0;
}
In this program
 The area is calculated as area=side×side. The 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

Q5 Example 5: Write C++ program to find the result of equation


x, x ≥ 0

F(x) =
−x, x < 0

#include <iostream.h>

using namespace std;

int main() {

double x, result;

cout << "Enter a value for x: ";

cin >> x;

if (x >= 0) {

cout<<" x is greater than 0";

if (x < 0)

cout<<" x is less than 0";

cout<<endl;

cout << "F (" << x << ") = " << result << endl;

// return 0;

6
‫ احمد صبيح‬.‫د‬ Programming Fundamentals/ LAB / First Class

if …. else ‫حل السؤال السابق بطريقة‬


#include <iostream.h>
using namespace std;
int main() {
double x, result;
cout << "Enter a value for x: ";
cin >> x;
if (x >= 0) {
cout<<" x is greater or equal than 0";
}
else {
cout<<" x is less than 0";
}
cout << "F (" << x << ") = " << result << endl;
// return 0;
}

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;

average = (num1 + num2 + num3) / 3.0;


cout << "The average of the three numbers is: " << average << endl;
return 0;
}

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);

cout << "\nTotal sum: " << sum << endl;

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;

cout << "Enter a number (1 to 7): ";


cin >> 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

You might also like