Cout : C++ Basic Programs
Cout : C++ Basic Programs
cout <<
#include <iostream> #include <iostream>
Using namespace std; using namespace std;
int main() int main ()
{ {
cout<<"Hello World"; cout<<"This "<<"is
return 0; "<<"awesome.";
} return 0;
}
Endl \n
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
cout<<"Hi!"<<endl; cout << "Hello world! \n";
cout<<"How are you?"; cout << "I love programming!";
return 0; return 0;
} }
/*multiple line comments*/ My Variable Print
//single line comments #include <iostream>
#include <iostream> using namespace std;
using namespace std; int main()
int main() {
{ int myVariable= 10;
/*We are going to cout << myVariable;
print "Hello world!" */ return 0;
cout << "Hello world!"; }
// prints Hello world!
return 0;}
Sum of Variables Cin>>
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int a = 30; int a;
int b = 12; cout << "Please enter a
int sum = a + b; number \n";
cout << sum; cin >> a;
return 0; cout << a;
} return 0;
}
Cin>> my program Cin>> sum
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int a,b; int a, b;
cin>>a; int sum;
cout<<"a= "<<a<<endl; cout << "Enter a number \n";
cin>>b; cin >> a;
cout<<"b= "<<b<<endl; cout << "Enter another
int sum; number \n";
sum=a+b; cin >> b;
cout<<"sum= "<<sum; sum = a + b;
return 0; cout << "Sum is: " << sum <<
} endl;
return 0;
}
}
if (age == 70) {
cout << "Senior";
}
return 0;
}
Switch statement
#include <iostream>
using namespace std;
int main()
{
int age = 42;
switch (age) {
case 16:
cout << "Too young";
break;
case 42:
cout << "Adult";
break;
case 70:
cout << "Senior";
break;
}
return 0;
Data Types Arrays and pointers
Int, Float and Double Signed: A signed integer can
Integers: hold both negative and
An integer type holds non-fractional positive numbers.
numbers, which can be positive or Unsigned:
negative. Examples of integers would An unsigned integer can hold
include 42, -42, and similar numbers. only positive values.
Short: Half of the default
size.
Long: Twice the default size.
Strings String
A string is an ordered sequence of #include <string>
characters, enclosed in double using namespace std;
quotation marks.
It is part of the Standard Library. int main() {
You need to include string a = "I am learning
the <string> library to use C++";
the string data type. Alternatively, you
can use a library that includes return 0;
the string library. }
American Standard Code for Booleans
Information Interchange (ASCII) is Boolean variables only have
a character-encoding scheme that is two possible values: true (1)
used to represent text in computers. and false (0).
To declare a Boolean variable,
we use the keyword bool.
Initializing Arrays Arrays in Loops
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int b[] = {11, 45, 62, 70, 88}; int myArr[5];
cout << b[0] << endl; for(int x=0; x<5; x++) {
// Outputs 11 myArr[x] = 42;
cout<< b[3] << endl; cout << x << ": " <<
// Outputs 70 myArr[x] << endl;
}
return 0; return 0;
} }
Arrays in Calculations Pointers
#include <iostream> #include <iostream>
using namespace std;
int main() using namespace std;
{
int arr[] = {11, 35, 62, 555, 989}; int main()
int sum = 0; {
for (int x = 0; x < 5; x++) { int score = 5;
sum += arr[x];
} cout << &score << endl;
cout << sum << endl;
return 0; return 0;
} }
Pointers Using Pointers
#include <iostream>
A pointer is a variable, with the using namespace std;
address of another variable as its
value. int main()
In C++, pointers help make certain {
tasks easier to perform. Other tasks, int score = 5;
such as dynamic memory allocation, int *scorePtr;
cannot be performed without using scorePtr = &score;
pointers.
cout << scorePtr << endl;
All pointers share the same data type -
a long hexadecimal number that return 0;
represents a memory address. }
int main() {
float celsius;
float fahrenheit;
cout<<"Celsius to Fahrenheit
conversion"<<endl;
while(cin>>celsius){
fahrenheit=1.8*celsius+32;
cout<<"fahrenheit="<<fahrenheit
<<endl;
}
return 0;
}