0% found this document useful (0 votes)
2 views18 pages

C++ Codes

This document outlines a homework assignment for C++ programming, submitted by Sheher Yar and guided by Sir Abdul Saboor for the session 2024-2025. It includes a comprehensive table of contents covering various C++ topics such as variables, loops, functions, and conditional statements, along with linked code examples and problems. The assignment starts on January 1, 2025, and requires the inclusion of all relevant C++ topics with key points and code snippets.

Uploaded by

ksheheryar590
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

C++ Codes

This document outlines a homework assignment for C++ programming, submitted by Sheher Yar and guided by Sir Abdul Saboor for the session 2024-2025. It includes a comprehensive table of contents covering various C++ topics such as variables, loops, functions, and conditional statements, along with linked code examples and problems. The assignment starts on January 1, 2025, and requires the inclusion of all relevant C++ topics with key points and code snippets.

Uploaded by

ksheheryar590
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

HOME WORK FOR WINTER VACATION

C++ Programs

SUBMITTED BY: SHEHER YAR


GUIDED BY: SIR ABDUL SABOOR SB
SESSION: 2024-2025
CLASS: F. SC (Part II)
Starting date: 01|01|2025
Submission date:

(Include all c++ topics with linked codes, problems


and key points)

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

Data types and variable sizes:


#include<iostream>
using namespace std;

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.

Type casting and conversion:


#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 2;
4|Page
cout<<(a/(float)b)<<endl; // giving output 2 because: int /int = int now type
casting;
return 0; // now it gives 2.5 bc int / float = float
}

 Casting is explicit conversion means the type conversion in which we convert big data type to
small while conversion is opposite.

Post/pre Unary increment operators:


Post unary increment operator:
#include <iostream>
using namespace std;
int main() {
int x = 2; //unary post increment operator
int y = x++; //work then update so y = 2, but x = 3
cout<<"y is :"<<y<<endl;
cout<<"x is :"<<x<<endl;
return 0;
}
 x++ means first work then update

Pre unary increment operator:


#include <iostream>
using namespace std;
int main() {
int x = 2; //unary pre increment operator
int y = ++x; //update(incr) then work(assign) so y = 3, but x = 3
cout<<"y is :"<<y<<endl;
cout<<"x is :"<<x<<endl;
return 0;
}

 ++x means first increasing then assigning

Conditional statements:
Q: to check for UPPER/lower case:
#include <iostream>

using namespace std;


int main() {
char ch;
cout<<"Enter character:"<<endl;
cin>>ch;
conditional statements
if (ch >= 'a' && ch <= 'z') {
cout<<"Lowercase\n";
} else{
cout<<"Uppercase\n";
5|Page
}
// or
if (ch >= 65 && ch <= 90){ //conversion by ASCII VALUE
cout<<"upper case\n";
} else{
cout<<"lower case\n";
}

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

Reverse for loop:


#include <iostream>
using namespace std;
int main() {
//reverse or backward loop:
int n = 4;
for(int i=n; i>=1; i--){
cout<<i<<endl;
}
return 0;
}

Q: calculating odd sum up to n:


#include <iostream>
using namespace std;
int main() {
// 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: checking for a number whether it is prime or not:


#include <iostream>
using namespace std;
int main() {
// LOOPS
int n = 11;
bool isPrime = true;
for(int i=2; i<= n-1; i++){
if( n % i == 0){
7|Page
isPrime = false;
break;
}
}
if(isPrime == true){
cout<<"Prime no.";
} else{
cout<<"Not prime";
}
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;
}

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

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

1 to N for N numbers of lines:


#include <iostream>
using namespace std;
int main() {
//NESTED LOOPS
int n = 4;
for(int i=0; i<n; i++){
int num = 1; // defining var here shows that
for(int j=1; j<=n; j++){ // for every iteration of inner loop will starts up.
cout<<num<<" ";
num++;
}
cout<<endl;
}
}

Continue n Numbers for N lines:


#include <iostream>
using namespace std;
int main() {
//NESTED LOOPS
int n = 4;
int num = 1;// defining var here shows that for every iteration of inner loop
will continue.
for(int i=0; i<n; i++){

for(int j=1; j<=n; j++){


cout<<num<<" ";
num++; // loop breaks here on value 4.
}
cout<<endl;

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.

N continuous characters in N lines:


##include <iostream>

using namespace std;


int main() {
int n = 5;
char ch = 'A';
for( int i=0; i<n ; i++){
for(int i=0; i<n; i++){
cout<<ch<<" ";
ch++;
if(ch > 90){ //repeating after z and implicitly converted;
ch = 'A';
}
}
cout<<endl;
}
}

 By changing variable “ch” will print continue alphabets.

Continuous CHARACTERS in triangular shape:


#include <iostream>
using namespace std;
int main() {
int n = 7;
char ch = 'A';
for( int i=0; i<n ; i++){
for(int j=0; j<i+1; j++){
cout<<ch<<" ";
ch++;
if(ch > 90){ //repeating after z and implicitly converted;
ch = 'A';
}
}
cout<<endl;
}
}
 In the inner loop, the “j” is relating with “I” rather than n.

Discontinuous same numbers in same line of triangular shape:


#include <iostream>
using namespace std;

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.

Discontinuous reverse triangular number’s shape:


#include <iostream>
using namespace std;
int main() {
//reverse discontinuous no's triangular shape:
int n = 4;
for(int i=0; i<n; i++){
for(int j=i+1; j>0; j--){
cout<<j;
}
cout<<endl;
}
return 0;
}

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

Inverted repeated numbers triangle:


#include <iostream>
using namespace std;
int main() {
int n = 4;
for(int i=0; i<n; i++){
// spaces
for(int j=0; j<i; j++){
cout<<" ";
}
// nums
for(int j=0; j<n-i; j++){
cout<<i+1;
}
cout<<endl;

return 0;
}

Inverted repeated character triangle:


#include <iostream>
using namespace std;
int main() {
int n = 4;
char ch = 'A';
for(int i=0; i<n; i++){
// spaces
for(int j=0; j<i; j++){
cout<<" ";
}
// nums
for(int j=0; j<n-i; j++){
cout<<ch;
}
ch++;
cout<<endl;

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.

Number pyramid pattern:


#include <iostream>
using namespace std;
int main() {
int n = 4;
char ch = 'A';
for(int i=0; i<n; i++){
// spaces
for(int j=0; j<n-i-1; j++){
cout<<" ";
}
// nums1
for(int j=1; j<=i+1; j++){
cout<<j;
}
//nums2
for(int j=i; j>=1; j-- ){
cout<<j;
}

cout<<endl;

return 0;
}

The butterfly pattern for “*”:


#include <iostream>
using namespace std;
int main() {
int n = 6;
cout<<"The butterfly\nPattern is:"<<endl;
// upper part
for(int i=0; i<n; i++){
//stars
for(int j=0; j<i+1; j++){
cout<<"*";
}
//spaces
for(int j=0; j<2*(n-i)-2; j++){
cout<<" ";
}
//stars
for(int j=0; j<i+1; j++){
cout<<"*";
}

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

Function to find prime numbers up to n:


#include <iostream>
using namespace std;
void primeUptoN(int n){
if(n <= 0){
cout<<n<<" is invalid no.\nEnter number \ngreater than 1";
} else if(n == 1){
cout<<n<<" is composite no.\n";
} else if(n == 2){
cout<<n<<" is prime no.";
} else{
for(int i=2; i<=n; i++){ //outer loop
if(i == 2){
cout<<i<<" is prime\n";
} else{
for(int j=2; j<i; j++){ //inner loop
if(i % j != 0){
cout<<i<<" is prime\n";
}
break;
}
}
}
}
}
int main() {
int n;
cout<<"Enter the no: ";

14 | P a g e
cin>>n;
primeUptoN(n);
return 0;
}

Discontinuous reverse characters Triangle:


#include <iostream>
using namespace std;
int main() {
int n;
cout<<"Enter the numbers\nof lines: ";
cin>>n;
for(int i=0; i<n; i++){
char chr = 'A';
for(char ch = chr +i; ch>= chr; ch--){
cout<<ch<<" ";
}
cout<<endl;
}

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.

WAF to calculate min of two no:

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

WAF to calculate sum up to n:


#include <iostream>
using namespace std;
// Defining a function:
int sumN(int n){
int sum = 0;
for(int i=1; i<=n; i++){
sum += i;
}
return sum;
}
int main() {
int result = sumN(5);
cout<<"The sum upto n is: "<<result<<endl;
return 0;
}

WAF to calculate n factorial:


#include <iostream>
using namespace std;
// Defining a function:
int factN(int n){
int fact = 1;
for(int i=1; i<=n; i++){
fact *= i;
}
return fact;
}
int main() {
int result = factN(5);
cout<<"The fact upto n is: "<<result<<endl;
return 0;
}

WAF to calculate digit sum:


#include <iostream>
using namespace std;
16 | P a g e
// Calculating digit sum in a function:
int digSum(int dig){
int digitSum = 0;
while(dig > 0){
int lastDig = dig % 10;
dig /= 10;
digitSum += lastDig;
}
return digitSum;

}
int main() {
cout<<digSum(2356)<<endl;
return 0;
}

WAF to calculate nCr:


#include <iostream>
using namespace std;
// Calculating digit sum in a function:
int factN(int n){
int fact = 1;
for(int i=1; i<=n; i++){
fact *= i;
}
return fact;
}

int nCr(int n, int r){


int result = factN(n)/(factN(r)*factN(n - r));
return result;
}

int main() {
cout<<"nCr is: "<<nCr(8,2)<<endl;
return 0;

WAF to calculate Nth Fibonacci term:


#include <iostream>
using namespace std;
int NFibonacci(int n){
int prev_fib = 0;
int curr_fib = 1;

if( n <= 1){


return n;
}

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

Binary to Decimal and Decimal to Binary:


Code for Decimal to Binary:
#include <iostream>
using namespace std;

int decToBin(int dec){


int ans = 0, pow = 1;
while(dec > 0){ //stopping condition
int rem = dec % 2; // binary bit
ans += rem*pow; // store binary bit
pow *= 10; // 10 power increasing from zero.
dec /= 2; // updating condition
}
return ans;
}

int main(){
cout<<"The binary no: "<<decToBin(6)<<endl;
return 0;
}

18 | P a g e

You might also like