0% found this document useful (0 votes)
6 views10 pages

C++_fullcodes

The document outlines a winter vacation homework assignment for C++ programming, submitted by Sheher Yar and guided by Sir Abdul Saboor. It includes various topics such as basic code structure, data types, operators, conditional statements, loops, and nested loops, along with example codes and problems. The assignment is intended for F. SC (Part II) students for the session 2024-2025, starting on January 1, 2025.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

C++_fullcodes

The document outlines a winter vacation homework assignment for C++ programming, submitted by Sheher Yar and guided by Sir Abdul Saboor. It includes various topics such as basic code structure, data types, operators, conditional statements, loops, and nested loops, along with example codes and problems. The assignment is intended for F. SC (Part II) students for the session 2024-2025, starting on January 1, 2025.

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 PDF, TXT or read online on Scribd
You are on page 1/ 10

HOME WORK FOR WINTER VACATION

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

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

4|Page
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";
}
// 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() {

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

Q: calculating odd sum up to n:


#include <iostream>
using namespace std;
int main() {

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: 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){
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;
}

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

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

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;
}
}
 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 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;
9|Page
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.

10 | P a g e

You might also like