0% found this document useful (0 votes)
18 views

Cout : C++ Basic Programs

The document provides examples of basic C++ programs that demonstrate printing output, taking user input, performing arithmetic operations, using variables, and conditionals like if/else statements. It also shows examples of common loops like while and for loops to repeat operations a given number of times.

Uploaded by

Narender
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Cout : C++ Basic Programs

The document provides examples of basic C++ programs that demonstrate printing output, taking user input, performing arithmetic operations, using variables, and conditionals like if/else statements. It also shows examples of common loops like while and for loops to repeat operations a given number of times.

Uploaded by

Narender
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

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;
}

Changing variables value One variable = other


#include <iostream> My program
using namespace std; #include <iostream>
using namespace std;
int main() int main()
{ {
int a = 100; int a=10;
a = 50; int b=a;
cout << a; int sum;
return 0; sum=a+b;
} cout<<"sum="<<sum;
return 0;
}
* /
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int x = 5 * 6; int x = 10 / 3;
cout << x; cout << x;
return 0; return 0;
} }
% ++ increment operator
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main()
int main() {
{ int x = 11;
int x = 25 % 7; x++;
cout << x; cout << x;
return 0; return 0;
} }
Assignment operators my Assignment operator my
program / program +
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int x = 11; int x = 11;
x/=11; x+=11;
cout <<x; cout << x;
return 0; return 0;
} }
C++ Conditionals and loops
If (condition){statement} Relational operators
#include <iostream> >= greater than or equal to
using namespace std;

int main() <= less than or equal to


{
if (7 > 4) {
cout << "Yes"; == equal to
}

return 0; != not equal to


}
The if statement evaluates the condition (7>4),
finds it to be true, and then executes
the cout statement.
Comparison of variables in if Check the greatest variable
condition #include <iostream>
#include <iostream> using namespace std;
using namespace std; int main()
{
int main() int a = 98;
{ int b = 76;
int a = 55; if (a> b) {
int b = 33; cout << "greatest is " << a <<
if (a > b) { endl;
cout << "a is greater than }
b"; if (b> a) {
} cout<< "greatest is " << b <<
endl;
return 0; }
} return 0;
}
The else Statement Else statement
#include <iostream>
if statement can be followed by an using namespace std;
optional else statement, which int main()
executes when the condition {
is false. int mark = 90;
The code above will test the if (mark < 50) {
condition: cout << "You failed." <<
- If it evaluates to true, then the endl;
code inside the if statement will }
be executed. else {
- If it evaluates to false, then the cout << "You passed." <<
code inside the else statement will endl;
be executed. }
return 0;
}
If Else multiple statements If multiple conditions
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main()
{ int main()
int mark = 90; {
if (mark < 50) { int mark = 100;
cout << "You failed." << if (mark >= 50) {
endl; cout << "You passed." <<
cout << "Sorry" << endl; endl;
} if (mark == 100) {
else { cout <<"Perfect!" << endl;
cout << "Congratulations!" }
<< endl; }
cout << "You passed." << else {
endl; cout << "You failed." <<
cout << "You are awesome!" endl;
<< endl; }
} return 0;
return 0; }
}
Nesting Unlimited else and if While Loop
conditions #include <iostream>
#include <iostream> using namespace std;
using namespace std; int main()
int main() {
int num = 1;
{ while (num < 6) {
int age = 18; cout << "Number: " << num
if (age > 14) { << endl;
if(age >= 18) { num = num + 1;
cout << "Adult"; }
} return 0;
else { }
cout << "Teenager"; Change increment value
} #include <iostream>
} using namespace std;
else { int main()
if (age > 0) { {
cout << "Child"; int num = 1;
} while (num < 6) {
else { cout << "Number: " << num
cout << "Something's << endl;
wrong"; num = num + 3;
} }
}
return 0; return 0;
} }
Using increment in while loop Using the while loop know the
#include <iostream> sum of numbers added by the
using namespace std; user
int main() #include <iostream>
{ using namespace std;
int num = 1; int main()
while (num < 6) { {
cout << "Number: " << num int num = 1;
<< endl; int number;
num++; int total = 0;
}
return 0; while (num <= 5) {
} cin >> number;
total += number;
num++;
}
cout << total << endl;
return 0;
}
For loop Using assignment operators in
#include <iostream> for loops
using namespace std; #include <iostream>
int main() using namespace std;
{
for (int a = 0; a < 10; a++) { int main()
cout << a << endl; {
} for (int a = 0; a < 50; a+=10) {
return 0; cout << a << endl;
} }
return 0;
}
The do...while Loop Do while loop
#include <iostream>
Unlike for and while loops, which using namespace std;
test the loop condition at the top int main()
of the loop, the do...while loop {
checks its condition at the bottom int a = 0;
of the loop. do {
A do...while loop is similar to cout << a << endl;
a while loop. The one difference is a++;
that the do...while loop is } while(a < 5);
guaranteed to execute at least one return 0;
time. }
Do while forever Multiple statements
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int a = 42; int age = 42;
do { if (age == 16) {
cout << a << endl; cout <<"Too young";
} while (a > 0);
return 0; }
} if (age == 42) {
cout << "Adult";

}
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. }

Celsius to Fahrenheit conversion


#include <iostream>
using namespace std;

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;
}

You might also like