0% found this document useful (0 votes)
28 views22 pages

Homework Ykcmk 2024 2025 203 20241220-20241227-1092-Exam+Examples

The document provides an introduction to programming in C++ with examples of basic programs, including printing text, performing arithmetic operations, and user input. It includes exercises for calculating sums, areas, and perimeters of geometric shapes, as well as comparisons between numbers. Each program is accompanied by comments explaining the code structure and functionality.

Uploaded by

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

Homework Ykcmk 2024 2025 203 20241220-20241227-1092-Exam+Examples

The document provides an introduction to programming in C++ with examples of basic programs, including printing text, performing arithmetic operations, and user input. It includes exercises for calculating sums, areas, and perimeters of geometric shapes, as well as comparisons between numbers. Each program is accompanied by comments explaining the code structure and functionality.

Uploaded by

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

Programming with C++

Yahya KemalChapter
College 1: Introduction
“Hello World!” – First C++ Program

#include<iostream>
using namespace std;

int main()
{
cout<<"Hello World!";

system("pause");
return 0;
}
2
/*
PROG: Hello_World.cpp
DESCRIPTION: Understanding the structure of C++ program.
Printing a line of text.
Using comments.
*/

#include<iostream>
// includes the declarations of the basic standard input-output
// library in C++, and its functionality is going to be used later
// in the program.

using namespace std;


// Namespaces are containers that contain the declarations of all
// the elements of the standard C++ library

int main() // the only function in this program.


{
cout<<"Hello World!"; // print "Hello World!"
// cout is declared in the iostream
// file within the std namespace

system("pause"); // stop and wait until user hits a button


return 0; // the main function ends properly
3
}
Hello World 2
/*
PROG: Hello_World2.cpp
DESCRIPTION: Using endl.
*/

#include<iostream>
using namespace std;

int main()
{
cout<<"Hello World! \n"; // move the cursor to the beginning
// of the next line

cout<<"This is my C++ program."<<endl<<endl;

system("pause");
return 0;
}

4
Sum
/*
PROG: Sum.cpp
*/

#include<iostream>
using namespace std;

int main()
{
// calculate and print the sum
cout<<"5 + 3 = "<< 5 + 3 <<endl;

system("pause");
return 0;
}
5
Problem 1:
• Write a C++ program that reads
two integers, calculates their sum
and then outputs the result.

6
/*
PROG: Sum.cpp
*/

#include<iostream>
using namespace std;

int main()
{
// num1, num2, and sum are three
// variables of type integer
int num1, num2, sum;

cout<<"Enter two integers: "<<endl;


cin>> num1 >> num2;
// cin reads two values for num1 and num2.

// sum gets the value of num1 + num2;


sum = num1 + num2;

cout<<"Sum is: "<<sum<<endl;

return 0; 7
}
Problem 2:
• Write a C++ program that reads two integers
and then calculates their sum, their difference,
their product, their quotient (float casting),
their remainder(modulus).

8
Flowchart 9
/*
PROG: Arithmetic.cpp
DESCRIPTION: Demonstrating arithmetic operators.
*/
#include<iostream>
using namespace std;

int main()
{
// declare two variables
int num1, num2;

// get input from user


cout<<"Enter the two integers: ";
cin>> num1 >> num2;

// calculate sum
cout<<num1<<"+"<<num2<<"="<<num1 + num2<<endl;
cout<<num2<<"+"<<num1<<"="<<num2 + num1<<endl<<endl;

// calculate difference
cout<<num1<<"-"<<num2<<"="<<num1 - num2<<endl;
cout<<num2<<"-"<<num1<<"="<<num2 - num1<<endl<<endl;
10
// calculate product
cout<<num1<<"*"<<num2<<"="<<num1 * num2<<endl;
cout<<num2<<"*"<<num1<<"="<<num2 * num1<<endl<<endl;

// calculate quotient as integer and then as float type


cout<<"integer: "<<num1<<"/"<<num2<<"="<<num1 /
num2<<endl;
cout<<"float: "<<num1<<"/"<<num2<<"="<<(float)num1 /
num2<<endl;

cout<<"integer: "<<num2<<"/"<<num1<<"="<<num2 / num1<<endl;


cout<<"float: "<<num2<<"/"<<num1<<"="<<(float)num2 /
num1<<endl<<endl;

// calculate modulus
cout<<num1<<"%"<<num2<<"="<<num1 % num2<<endl;
cout<<num2<<"%"<<num1<<"="<<num2 % num1<<endl<<endl;

system("PAUSE"); 11
12
Exercise: Rectangle
• Write a C++ program to calculate the area and
perimeter of a rectangle.

Perimeter = 2 * ( length + width)


width
Area = length * width

length

13
Exercise: Rectangle

14
/*
PROG: Rectangle.cpp
DESCRIPTION: Find the perimeter and area of a rectangle.
*/
#include<iostream>
using namespace std;

int main()
{
// declare variables
int length, width, perimeter, area;

// get user input


cout<<"Enter rectangle length: ";
cin>> length;

cout<<"Enter rectangle width: ";


cin>> width;

// calculate and show perimeter and area


perimeter = 2 * ( length + width );
cout<<"Perimeter = "<<perimeter<<endl;

area = length * width;


cout<<"Area = "<< length << " * " << width << "= "<<area<<endl;

return 0;
15
}
Exercise: Rectangle

16
Exercise: Circle
Write a C++ program to calculate the area and
circumference of a circle. Make use of the M_PI
arithmetic constant defined in the library cmath.

Circumference = 2 * PI * Radius radius

Area = PI * Radius * Radius

17
Exercise: Circle

18
/*
PROG: Circle.cpp
DESCRIPTION: Use constant M_PI as declared from the cmath library.
*/

#define _USE_MATH_DEFINES
#include<cmath> // include the cmath library
#include<iostream>
using namespace std;

int main()
{
// declare variables
float radius, circumference, area;
const float PI = 3.14;

// get radius from user


cout<<"Enter the length of the radius: ";
cin>> radius;

// calculate and show circumference


circumference = 2 * PI * radius;
cout<<"\nCircumference = "<<circumference<<endl;

//calculate and show area


area = PI * radius * radius;
cout<<"Area = "<<area<<endl<<endl;

system("PAUSE");
return 0; 19
Exercise: 1 or 0
• Write a C++ program that gets two numbers
and displays 1 if first is bigger than second,
otherwise displays 0.

20
/*
PROG: Bool.cpp
DESCRIPTION: Compare if first is bigger than second
*/

#include<iostream>
using namespace std;

int main()
{
float num1, num2;

// get user input


cout<<"Enter the firs number: ";
cin>> num1;
cout<<"Enter second number: ";
cin>> num2;

// compare if first is greater


cout<<endl<< (num1 > num2)<<endl<<endl;

system("PAUSE");
return 0;
21
}
22

You might also like