0% found this document useful (0 votes)
18 views11 pages

C

Uploaded by

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

C

Uploaded by

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

// Example program

#include <iostream>

#include <string>

int main()

std::string name;

std::cout << "What is your name? ";

getline (std::cin, name);

std::cout << "Hello, " << name << "!\n";

}
Example 1: Variables and Output
Example 2: Conditional Statements

Example 3: Loops (for loop)


a simple C++ program that adds two numbers together:
Explanation:

1. #include <iostream>: This line includes the input-output stream header file necessary
for using cin, cout, and other I/O functions.
2. using namespace std;: This line allows us to use cout, cin, and other standard C++
functions without specifying the namespace std:: each time.
3. int main(): Every C++ program must have a main function from where execution
starts.
4. Variable Declaration:
o int num1, num2, sum;: Declares three integer variables num1, num2, and sum. num1
and num2 will hold the numbers to be added, and sum will hold the result.

5. Input:
o cout << "Enter first number: ";: Prints a message asking the user to enter
the first number.
o cin >> num1;: Reads the first number entered by the user and stores it in num1.
o cout << "Enter second number: ";: Prints a message asking the user to enter
the second number.
o cin >> num2;: Reads the second number entered by the user and stores it in
num2.
6. Calculation:
o sum = num1 + num2;: Adds num1 and num2 together and stores the result in sum.

7. Output:
o cout << "Sum of " << num1 << " and " << num2 << " is: " << sum <<
endl;: Prints the sum of num1 and num2 along with a descriptive message.

8. return 0;: Indicates that the program has executed successfully and returns 0 to the
operating system.

This program takes two integers as input from the user, computes their sum, and then
displays the result using standard input/output operations in C++.
Printing triangle.

Explanation:

1. #include <iostream>: Includes the input-output stream header file necessary for using
cin, cout, and other I/O functions.
2. using namespace std;: Allows us to use cout, cin, and other standard C++ functions
without specifying the namespace std:: each time.
3. int main(): Every C++ program must have a main function from where execution
starts.
4. Variable Declaration:
o int rows;: Declares an integer variable rows to store the number of rows for the
triangle.
5. Input:
o cout << "Enter number of rows for the triangle: ";: Prints a message
asking the user to enter the number of rows.
o cin >> rows;: Reads the integer input from the user and stores it in the variable
rows.
6. Printing the Triangle:
o Outer for loop (i loop): Iterates through each row of the triangle. Starts from 1
and goes up to rows.
o Inner for loop (j loop): Prints the stars (*) for each row. The number of stars in
each row corresponds to the current row number (i).
o cout << "* ";: Prints a star followed by a space.
7. New Line:
o cout << endl;: Moves to the next line after printing all stars in a row, so the
next row starts on a new line.
8. return 0;: Indicates that the program has executed successfully and returns 0 to the
operating system.

IF CONTROL STRUCTURES.

If control structures are to be discussed in the context of C++ programming, there are primarily
three main constructs: if statements, else if statements, and else statements. These constructs
allow you to control the flow of execution based on conditions.

1. if Statement:

The if statement evaluates a condition and executes a block of code if the condition is true.

Syntax:

2. else if Statement:
The else if statement allows you to check multiple conditions and execute a block of code as
soon as one of the conditions evaluates to true. It comes after the initial if statement.

Syntax
These control structures (if, else if, else) and the conditional operator (?:) are fundamental in C++
programming for making decisions based on conditions, allowing your programs to perform different
actions based on different inputs or conditions.

You might also like