1.
Producing output hello world
int main() {
cout<<"Hello, World!";
return 0;
}
Get line- entire line of text
#include<iostream>
#include<cmath>
using namespace std;
int main()
int num1, num2;
cout << "enter first number: ";
cin >> num1;
cout << "enter second number: ";
cin >> num2;
cout << num1+num2;
ARRAYS
#include<iostream>
#include<cmath>
using namespace std;
int main()
int luckynums [] = {4,8,10,14,16};
cout << luckynums[1];
return 0;
FUNCTION
#include<iostream>
using namespace std;
void sayhi(string name, int age) {
cout << "hello" << name << " i am " << age << endl;
int main()
sayhi(" mike", 45);
sayhi(" tom", 21);
return(0);
}
C++
#include <iostream>
using namespace std; = these 2 r included always
int main() {
cout << "Hello World!"; =it gives output
return 0; = end a statement
} =code should always be inside curly brackets
INSERT NEW LINE
#include <iostream>
using namespace std;
int main() {
cout << "Hello World! \n";
cout << "I am learning C++";
return 0;
}
OR
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
cout << "I am learning C++";
return 0;
}
C++ Comments
Comments can be used to explain C++ code, and to make it more readable.
It is generally used for our understanding
Any text between // and the end of the line is ignored by the compiler (will
not be executed).
// This is a comment
cout << "Hello World!";
Multi-line comments start with /* and ends with */.
/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout << "Hello World!";
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'. Char values are
surrounded by single quotes
string - stores text, such as "Hello World". String values are
surrounded by double quotes
bool - stores values with two states: true or false
STRING
int main() {
string phrase = "fish";
cout << phrase;
TO MODIFY ONE LETTER
int main() {
string phrase = "fish";
phrase [0] = ‘b’;
cout << phrase;
TO FIND THE POSITION OF THE LETTER
int main() {
string phrase = "fish academy ";
0,1,2,3 4,5,6….
cout << phrase.find(“academy, 0);
output is 5 as A starts at 5 in fish academy
IF WE DO MATH WITH TWO INTEGERS WE GET A INTEGER BACK
int main() {
int wnum = ‘4’;
double dnum = ‘4.5’
cout << “10/3”;
output is 3
3 root to power 5
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << pow(3, 5);
return 0;
Others
Cout << round (2.3);
Cout << sqrt(2,2);