C++ Codes
C++ Codes
C++ Programs
1|Page
TABLE OF CONTENTS
C++ PROGRAMMING
1. First code:...........................................................................................................................................................3
2. Boilerplate Code:................................................................................................................................................3
3. Comments:.........................................................................................................................................................3
4. Variables:............................................................................................................................................................4
5. Data types and variable sizes:............................................................................................................................4
6. Type casting and conversion:.............................................................................................................................4
7. Post/pre Unary increment operators:................................................................................................................5
Post unary increment operator:..................................................................................................................................5
Pre unary increment operator:....................................................................................................................................5
8. Conditional statements:.....................................................................................................................................5
Q: to check for UPPER/lower case:..........................................................................................................................5
9. Loops:.................................................................................................................................................................6
While loop:..................................................................................................................................................................6
Do while loop:.............................................................................................................................................................6
For loop:......................................................................................................................................................................7
Reverse for loop:.....................................................................................................................................................7
Q: calculating odd sum up to n:...............................................................................................................................7
Q: checking for a number whether it is prime or not:.............................................................................................7
Q: calculating n factorial:.........................................................................................................................................8
Q: Sum of all numbers up to n which are divisible by 3...........................................................................................8
Nested loops:..............................................................................................................................................................9
Patterns:..................................................................................................................................................................9
N numbers of stars in n number of lines:............................................................................................................9
1 to N for N numbers of lines:.............................................................................................................................9
Continue n Numbers for N lines:.........................................................................................................................9
N continuous characters in N lines:...................................................................................................................10
Continuous CHARACTERS in triangular shape:..................................................................................................10
Discontinuous same numbers in same line of triangular shape:.......................................................................10
Discontinuous reverse triangular number’s shape:...........................................................................................11
Floyd’s triangle:.................................................................................................................................................11
Inverted repeated numbers triangle:................................................................................................................12
Inverted repeated character triangle:...............................................................................................................12
Number pyramid pattern:.................................................................................................................................13
The butterfly pattern for “*”:............................................................................................................................13
Function to find prime numbers up to n:..........................................................................................................14
2|Page
Discontinuous reverse characters Triangle:.......................................................................................................15
10. Functions:.........................................................................................................................................................15
Functions in memory:................................................................................................................................................15
WAF to calculate sum up to n:..................................................................................................................................16
WAF to calculate n factorial:.....................................................................................................................................16
WAF to calculate digit sum:.......................................................................................................................................17
WAF to calculate nCr:................................................................................................................................................17
WAF to calculate Nth Fibonacci term:.......................................................................................................................17
C++ PROGRAMING:
First code:
#include<iostream>
using namespace std;
int main(){
cout<<"my name is sheheryar”<<endl;
return 0;
}
We can write endl or “\n” in the last. Usually “\n" refers to write the code in next.
“;” is a terminator.
“{}” includes the block of code.
Boilerplate Code:
#include<iostream>
using namespace std;
int main(){
return 0;
}
Comments:
#include<iostream>
using namespace std;
3|Page
int main(){
// I am comment
return 0;
}
Variables:
#include<iostream>
using namespace std;
int main(){
// variables:
int a = 14;
char grade = 'A';
return 0;
}
Variables are the containers which stores data precedes by its data type in c++.
int main(){
// data types its size:
int a = 14; // 4 bytes
char grade = 'A'; // 1 bytes
float PI = 3.14f; // 4 bytes
bool isTrue = true; // 1 bytes
double price = 100.99; // 8 bytes
// i.e:
bool isSafe = true;
cout<<sizeof(isSafe)<<endl; // returns size is = 1.
return 0;
}
For “float PI = 3.14f”, Generally without “f” the compiler assumes that it is a double datatype. We
must have to write the f at the end to make it float datatype. Float and Double have just the
difference of size.
Casting is explicit conversion means the type conversion in which we convert big data type to
small while conversion is opposite.
Conditional statements:
Q: to check for UPPER/lower case:
#include <iostream>
return 0;
}
Loops:
While loop:
#include <iostream>
using namespace std;
int main() {
// LOOPS
int n;
cout<<"Enter the number";
cin>>n;
int i= 1;
while(i <= n){
cout<<i<<" ";
i++;
}
return 0;
}
Do while loop:
#include <iostream>
using namespace std;
int main() {
// LOOPS
int n;
cout<<"Enter the number";
cin>>n;
int i= 1;
do
{
cout<<i<<" ";
i++;
} while (i<= n);
return 0;
}
The difference between while and do while is that do while always do something either the
condition is true or false and for while vice versa.
6|Page
For loop:
#include <iostream>
using namespace std;
int main() {
// LOOPS
for(int i=0; i<= 4; i++){
cout<<i<<" ";
}
return 0;
}
Q: calculating n factorial:
#include <iostream>
using namespace std;
int main() {
// LOOPS
int n;
cout<<"Enter the no: "<<endl;
cin>>n;
int fact = 1;
for (int i=1; i<=n; i++){
fact *= i;
}
cout<<fact<<endl;
return 0;
}
8|Page
Nested loops:
Patterns:
N numbers of stars in n number of lines:
#include <iostream>
using namespace std;
int main() {
//NESTED LOOPS
int n = 5;
for(int i=1; i<=n; i++) { //control no's of lines
int m = 5;
for(int j=1; j<=m; j++){ //control no's of items
cout<<"*";
}
cout<<endl;
}
return 0;
}
9|Page
}
}
In previous two codes, there is just difference in declaration of “num” variable (as highlighted above). In first
code, the variable resets its value due to its limit to inner loop.
When we declare the same variable initialize first at outer loop, it doesn’t get reset because it is outside of
the loop’s iteration.
10 | P a g e
int main() {
// int n = 4;
// for(int i=0; i<n; i++){
// for(int j=0; j<i+1; j++){
// cout<<i+1;
// }
// cout<<endl;
// } OR
int n = 4;
int num = 1;
for(int i=0; i<n; i++){
for(int j=0; j<i+1; j++){
cout<<num;
}
num++;
cout<<endl;
}
return 0;
}
Here variable “num” is incremented in outer loop because we need “num” to be incremented in columns. If
it was defined in inner loop, so it will increase its value in column. And we don’t want to reset it, therefore, it
is initialized at top of outer loop.
Floyd’s triangle:
#include <iostream>
using namespace std;
int main() {
//Floyd's triangle:
int n = 4;
int num = 1;
for(int i=0; i<n; i++){
for(int j=0; j<i+1; j++){
cout<<num<<" ";
num++;
11 | P a g e
}
cout<<endl;
return 0;
}
return 0;
}
return 0;
12 | P a g e
}
If the spaces are at the beginning of some pattern, then we have to also write the logic for spaces and then
for items in the pattern. As mentioned in above programs.
cout<<endl;
return 0;
}
13 | P a g e
cout<<endl;
}
// lower part
for(int i=0; i<n; i++){
//stars
for(int j=0; j<n-i; j++){
cout<<"*";
}
//spaces
for(int j=0; j<i*2; j++){
cout<<" ";
}
//stars
for(int j=0; j<n-i; j++){
cout<<"*";
}
cout<<endl;
}
return 0;
}
14 | P a g e
cin>>n;
primeUptoN(n);
return 0;
}
return 0;
}
Functions:
Functions in memory:
#include <iostream>
using namespace std;
// Defining a function:
int fun(){
int x = 6;
return x;
}
int main() {
int x = 7;
cout<<x<<endl;
return 0;
}
In above code, we noticed that the variable of any other function can’t be access in main function.
Every function is stored in call stack in the form of stack frame.
By default, the main function is called by compiler itself and for calling other function we must call it in main
function.
When a function called It occupy it stack frame in call stack and it’s regarding variables.
Return is a terminator it returns control to main function and clear’s up from memory.
#include <iostream>
using namespace std;
// Defining a function:
15 | P a g e
int minOfTwo(int a, int b){ //parameters(copy of arguments)
if(a < b){
return a;
} else{
return b;
}
}
int main() {
int min = minOfTwo(2,3); //(2,3)arguments and function calling
cout<<"Minimum is: "<< min <<endl;
return 0;
}
}
int main() {
cout<<digSum(2356)<<endl;
return 0;
}
int main() {
cout<<"nCr is: "<<nCr(8,2)<<endl;
return 0;
17 | P a g e
for(int i=2; i<=n; i++) {
int next_fib = prev_fib + curr_fib;
prev_fib = curr_fib;
curr_fib = next_fib;
}
return curr_fib;
}
int main(){
cout<<NFibonacci(5)<<endl; // 0 1 1 2 3 5
return 0;
}
int main(){
cout<<"The binary no: "<<decToBin(6)<<endl;
return 0;
}
18 | P a g e