C++_fullcodes
C++_fullcodes
Programs
1|Page
TABLE OF CONTENTS
C++ PROGRAMMING
1. First code:............................................................................................................................................................. 3
2. Boilerplate Code: ................................................................................................................................................. 3
3. Comments: ........................................................................................................................................................... 3
4. Variables: ............................................................................................................................................................. 3
5. Data types and variable sizes: .............................................................................................................................. 4
6. Type casting and conversion: ............................................................................................................................... 4
7. Post/pre Unary increment operators: ................................................................................................................. 4
Post unary increment operator: ................................................................................................................................... 4
Pre unary increment operator: ..................................................................................................................................... 5
8. Conditional statements:....................................................................................................................................... 5
Q: to check for UPPER/lower case: ........................................................................................................................... 5
9. Loops: ................................................................................................................................................................... 5
While loop: .................................................................................................................................................................... 5
Do while loop: ............................................................................................................................................................... 6
For loop: ........................................................................................................................................................................ 6
Q: calculating odd sum up to n: ................................................................................................................................ 6
Q: checking for a number whether it is prime or not: .............................................................................................. 7
Q: calculating n factorial: .......................................................................................................................................... 7
Q: Sum of all numbers up to n which are divisible by 3. ........................................................................................... 8
Nested loops: ................................................................................................................................................................ 8
Patterns: .................................................................................................................................................................... 8
N numbers of stars in n number of lines: ............................................................................................................. 8
1 to N for N numbers of lines: ............................................................................................................................... 8
Continue n Numbers for N lines: .......................................................................................................................... 9
N characters in N lines: ......................................................................................................................................... 9
2|Page
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;
int main(){
// I am comment
return 0;
}
Variables:
#include<iostream>
using namespace std;
int main(){
// variables:
int a = 14;
char grade = 'A';
return 0;
}
3|Page
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.
4|Page
return 0;
}
x++ means first work then update
Conditional statements:
Q: to check for UPPER/lower case:
#include <iostream>
return 0;
}
Loops:
While loop:
#include <iostream>
using namespace std;
int main() {
5|Page
// 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.
For loop:
#include <iostream>
using namespace std;
int main() {
// LOOPS
for(int i=0; i<= 4; i++){
cout<<i<<" ";
}
return 0;
}
6|Page
// LOOPS
int n = 10;
int oddSum = 0;
for(int i=0; i<= n; i++){
if( i%2 != 0){
oddSum += i;
}
}
cout<<"Odd sum is: "<<oddSum;
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;
}
7|Page
Q: Sum of all numbers up to n which are divisible by 3.
#include <iostream>
using namespace std;
int main() {
// LOOPS
int n;
cout<<"Enter the no: ";
cin>>n;
int sum = 0;
for(int i=1; i<=n; i++){
if(i % 3 == 0){
sum += i;
}
}
cout<<"The sum is: "<<sum<<endl;
return 0;
}
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;
}
N characters in N lines:
##include <iostream>
10 | P a g e