Example 1 (person info)
#include <iostream>
using namespace std;
int main() {
struct employe{
string name;
string surname;
int age;
string male;
bool boolean;
};
employe person1;
person1.name = "john";
person1.surname = "allen";
person1.age = 18;
person1.male = "man";
person1.boolean = true;
cout << "name: " << person1.name
<< endl
<< "surname: " <<person1.surname
<< endl
<< "age:" <<person1.age
<< endl
<< "male" << person1.male
<< endl
<< "boolean: " << person1.boolean;
return 0;
}
name: john
surname: allen
age:18
maleman
boolean: 1
Example 2 (cardinal_direction)
#include <iostream>
using namespace std;
int main() {
enum cardinal_direction
{
north,
south,
east,
west
};
cardinal_direction direction;
cout << north
<< endl
<< south
<< endl
<< east
<< endl
<< west;
}
0
1
2
3
Example 3: (name and surname)
#include <iostream>
using namespace std;
int main() {
string name,surname;
cout << "name and surname: " << endl;
cin >> name >> surname;
cout << "name: " << name << endl << "surname: " << surname;
}
name: john
surname: allen
Example 4 (number squared)
#include <iostream>
using namespace std;
int main() {
// number squared
int number;
cout << "number: ";
cin >> number;
number = number*number;
cout << "squared: " << number;
}
number: 5
squared: 25
Example 5 (sum of two numbers)
#include <iostream>
using namespace std;
int main() {
int number1, number2, total = 0;
cout << "1.number: "; cin >> number1;
cout << "2.number: "; cin >> number2;
total = number1 + number2;
cout << "total: " << total;
}
1.number: 10
2.number: 15
total: 25
Example 6 (exam average)
#include <iostream>
using namespace std;
int main() {
double exam1,
exam2,
exam3,
exam_average = 0;
cout << "1.exam: "; cin >> exam1;
cout << "2.exam: "; cin >> exam2;
cout << "3.exam: "; cin >> exam3;
exam_average = (exam1 + exam2 + exam3)/3;
cout << "exam average: " << exam_average;
}
1.exam: 70
2.exam: 85
3.exam: 90
exam average: 81.6667
Example 7
The program that finds the sum of the prime divisors of a number.
#include <iostream>
using namespace std;
int main() {
int number,digits = 0;
cout << "Number: ";
cin >> number;
while ( number > 0){
digits ++;
number /= 10;
}
cout << "Number of digits: " << digits << endl;
}
Number: 21024
Number of digits: 5
Example 8
Age control.
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Please enter your age: "; cin >> age;
if(age>=18){
cout << "Since you are over 18 years of age, the sale process is continuing.";
}
else{
cout << "As you are under 18 years of age, the sale process cannot be continued." ;
}
}
Please enter your age: 22
Since you are over 18 years of age, the sale process is continuing.
Example 9
Only the program that takes the square of positive numbers.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "squared: "; cin >> number;
if(number > 0){
number *= number;
cout << number;
}
}
squared: 10
100
Example 10
The program that finds the circumference and area of the rectangle.
#include <iostream>
using namespace std;
int main() {
int width,height,area,perimeter,option;
cout << "Find the perimeter of the rectangle (1) \n";
cout << "Find the area of the rectangle (2) \n";
cout << "option: "; cin >> option;
cout << "enter the height value: "; cin >> height;
cout << "enter the width value: "; cin >> width;
if(option == 1){
perimeter = 2*(width + height);
cout << "perimeter: " << perimeter;
}
else if(option == 2){
area = width*height;
cout << "area: " << area;
}
}
Find the perimeter of the rectangle (1)
Find the area of the rectangle (2)
option: 1
enter the height value: 5
enter the width value: 5
perimeter: 20
Find the perimeter of the rectangle (1)
Find the area of the rectangle (2)
option: 2
enter the height value: 5
enter the width value: 5
area: 25
Example 11
When two numbers are entered, the program finds the big one.
#include <iostream>
using namespace std;
int main() {
int num1,num2;
cout << "number 1: "; cin >> num1;
cout << "number 2: "; cin >> num2;
if(num1 > num2){
cout << "number 1 bigger than number 2 ";
}
else if(num2 > num1){
cout << "number 2 bigger than number 1 ";
}
else{
cout << "The two numbers are equal to each other.";
}
}
number 1: 10
number 2: 12
number 2 bigger than number 1
number 1: 15
number 2: 10
number 1 bigger than number 2
number 1: 7
number 2: 7
The two numbers are equal to each other.
Example 12
Note calculation program.
#include <iostream>
using namespace std;
int main() {
int not1,not2,homework,average;
cout << "note 1: "; cin >> not1;
cout << "note 2: "; cin >> not2;
cout << "homework: "; cin >> homework;
average = not1/3 + not2/3 + homework/3;
if(average > 90){
cout << "AA";
}
else if(average > 80){
cout << "BA";
}
else if(average > 70){
cout << "BB";
}
else if(average > 690){
cout << "BC";
}
else if(average > 50){
cout << "CC";
}
else if(average < 50){
cout << "FF";
}
}
note 1: 90
note 2: 85
homework: 100
AA
Example 13
Counter usage.
In this application, we will use the counter to find out how many of the numbers entered. For example, when a certain number, such as 5, is entered, the counter will stop counting and the counter value will be printed on the screen.
#include <iostream>
using namespace std;
int main() {
int counter = 0 , number;
read:
cout << "number: "; cin >> number;
counter += 1;
if(counter == 5){
cout << "counter value: " << counter;
}
else{
goto read;
}
return 0;
}
number: 5
number: 10
number: 15
number: 20
number: 25
number value: 5
Example 14
Printing of numbers from 1 to the “end”.
#include <iostream>
using namespace std;
int main() {
int end,n= 0;
cout << "end: "; cin >> end ;
counter:
n += 1;
if(end >= n){
cout << n << ", ";
goto counter;
}
}
end: 10
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
Example 15
A number will be entered. Then the number of pieces to be entered. It will increase the number value by one until the number is reset.
#include <iostream>
using namespace std;
int main() {
int piece,number,total=0;
cout << "number: "; cin >> number;
cout << "piece: "; cin >> piece;
a:
piece -= 1;
if( piece >= 0){
total += number;
number += 1;
goto a;
}
cout << "total: " << total;
}
number: 10
piece: 3
total: 33
Example 16
The program that finds the sum of the numbers up to the entered value.
#include <iostream>
using namespace std;
int main() {
int end,counter=0,total=0;
cout << "end: "; cin >> end;
a:
counter += 1;
if( end >= counter){
total += counter;
goto a;
}
cout << "total: " << total;
}
end: 5
total: 15
Example 17
factorial calculation.
#include <iostream>
using namespace std;
int main() {
int number, total = 1;
cout << "number: "; cin >> number;
a:
number --;
if(0 < number){
total += total*number;
goto a;
}
cout << "total: " << total;
}
number: 5
total: 120
Example 18
The program that prints “hello c” 5 times on the screen.
#include <iostream>
using namespace std;
int main() {
for(int i=0; i < 5; i++){
cout << "hello c++ \n";
}
}
hello c++
hello c++
hello c++
hello c++
hello c++
Example 19
The program that collects double numbers from zero to n.
#include <iostream>
using namespace std;
int main() {
int n,i;
cout << "number: ";cin >> n;
for(i=0;i<n;i++){
if(i%2==0){
cout << i << endl;
}
}
}
number: 10
0
2
4
6
8
Example 20
The sum of the numbers from one to five.
#include <iostream>
using namespace std;
int main() {
int i = 0,total=0;
while(i<=5){
total += i;
i++;
}
cout << "total: " << total;
}
total: 15
Example 21
The program that finds the sum of numbers from one to five. Only the 3rd digit, the “break” command to exit the loop. The program collects only one and two.
#include <iostream>
using namespace std;
int main() {
int i,total=0;
for(i = 0; i <= 5; i++){
if(i==3){
break;
}
total += i;
}
cout << "total: " << total;
}
3
Example 22
Average of N pieces.
#include <iostream>
using namespace std;
int main() {
int n,i=0,avg=0;
cout << "number: "; cin >> n;
while(i<=n){
avg += i;
i++;
}
cout << "average: " << avg/n;
}
4
2
Example 23
factorial
#include <iostream>
using namespace std;
int main() {
int number,i=1,total=1;;
cin >> number;
while(i<=number){
total *= i;
i++;
}
cout << total;
}
number: 5
120
Example 24
stars
#include <iostream>
using namespace std;
void star(int row){
string space = " ";
for(int i=row; i >= 0; i--){
for(int x=i; x >= 0;x--){
cout << "**";
};
cout << endl << space;
space += " ";
}
}
int main(){
int row;
cin >> row;
star(row);
}
**********
********
******
****
**
Example 25
Leave a comment