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

Notes For Fundamental Coding

This document discusses different data types used in programming such as integers, characters, floats, strings, and booleans. It also covers common operators like addition, subtraction, etc. and control structures like if/else statements, loops (for, while, do-while), switch-case statements and arrays. Examples provided include taking input from the user, performing calculations, printing output and looping constructs.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Notes For Fundamental Coding

This document discusses different data types used in programming such as integers, characters, floats, strings, and booleans. It also covers common operators like addition, subtraction, etc. and control structures like if/else statements, loops (for, while, do-while), switch-case statements and arrays. Examples provided include taking input from the user, performing calculations, printing output and looping constructs.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

DATA TYPES + Addition

1. INTEGER (int) - Subtraction


Integer data types often represent whole numbers * Multiplication
in programming. The int keyword is used to / Division
indicate integers. % Modulo(remainder)
2. CHARACTER (char) ++ Increase value by 1
In coding, alphabet letters denote characters. -- Decrease value by 1
Programmers might represent these data types as ++a add 1 to a
(char), and they can be single characters or a a++ add 2 to a
string of letters. --a add 1 to a
3. FLOAT (float) a-- stay to a
Floating-point data types represent fractional
numbers in programming. Float is a data type that *Note to increment and decrement:
typically allows up to seven points after a ++a or --a - the value of a is
decimal while double allows up to 15 points after incremented/decremented by 1; then it returns the
a decimal. value.
4. STRING (string) a++ or a-- - the original value of a is returned
A string data type is a combination of characters first; then a is incremented/decremented by 1.
that can be either constant or variable. This
often incorporates a sequence of character data Operations Function
types that result in specific commands depending == is equal to
on the programming language.  != is not equal to
5. BOOLEAN (bool) > greater than
Boolean data is what programmers use to show >= greater than or equal
logic in code. It's typically one of two values— < less than
true or false—intended to clarify conditional <= less than or equal
statements. These can be responses to "if/when"
scenarios, where code indicates if a user && logical AND
performs a certain action. || logical OR
! logical NOT

<< Output
>> Input

IF
OPERATORS
if (condition){
Operations Function //code;
} //body/code of the loop;
code after if; ____________________________________________
________________________
WHILE LOOP (commonly used for like: adding
IF – ELSE positive numbers only)
while (condition){
if (condition){
//code if true; //body/code of the loop;
} ____________________________________________
else{
//code if false; DO - WHILE LOOP
} do {
code after if – else; //body/code of the loop;
________________________
while (condition)
IF – ELSE – ELSE ____________________________________________
if (condition1){
//code 1; 1D ARRAY (commonly used in updating inventories
} and uses FOR loop)
else if (condition 2{ data_type array_name[array_size] =
//code 2; {comma_separated_element_list};
}
else{ or
//code 3;
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
code after if – else – else;
________________________

SWITCH – CASE STATEMENT (commonly used in


calculator operations)

switch (expression) {
case var1:
//code if expression is same to var1;
break;

case var2:
//code if expression is same to var2;
break;

default
//code if it doesn’t match any var;
FOR LOOP Print “Title”
for (initialization; condition; update/increment #include<iostream>
or decrement){ using namespace std;
int main()
{ {
cout<<"Title"; int val;
cout<<endl; cout<<"Enter the Number: ";
return 0; cin>>val;
} cout<<"\nThe Value is "<<val;
cout<<endl;
Print “Title” 10 times FOR loop return 0;
#include<iostream> }
using namespace std;
int main() Input a Character
{ #include<iostream>
int i; using namespace std;
for(i=0; i<10; i++) int main()
cout<<"Title\n"; {
cout<<endl; int name;
return 0; cout<<"Enter the Number: ";
} cin>>name;
cout<<"\nThe Value is "<<name;
cout<<endl;
Print “Title” 10 times WHILE loop return 0;
#include<iostream>
using namespace std;
int main()
{
int i=0;
while(i<10)
{
cout<<"Title\n";
i++;
}
cout<<endl;
return 0;
}

Input an Integer
#include<iostream> Add/Subtract/Multiply/Divide Integers
using namespace std; #include<iostream>
int main() using namespace std;
cout<<"Enter the Number: ";
int main() cin>>num;
{ while(num>0)
int num1, num2, add; {
cout<<"Enter Two Numbers: "; rem = num%10;
cin>>num1>>num2; sum = sum+rem;
add = num1(+, -, *, /) num2; num = num/10;
cout<<"\nResult = "<<add; }
cout<<endl; cout<<"\nSum of Digits = "<<sum;
return 0; cout<<endl;
} return 0;
}
Operation with Integer based on user’s decision
#include<iostream> Sum of the digits using FOR Loop
using namespace std; #include<iostream>
int main() using namespace std;
{ int main()
int num1, num2; {
char op; int num, rem, sum;
cout<<"Enter Two Numbers: "; cout<<"Enter the Number: ";
cin>>num1>>num2; cin>>num;
cout<<"Enter the Operator (+, -, *, /): "; for(sum=0; num>0; num=num/10)
cin>>op; {
if(op=='+') rem = num%10;
cout<<endl<<"Addition Result = "<<num1+num2; sum = sum+rem;
else if(op=='-') }
cout<<endl<<"Subtraction Result = "<<num1- cout<<"\nSum of Digits = "<<sum;
num2; cout<<endl;
else if(op=='*') return 0;
cout<<endl<<"Multiplication Result = }
"<<num1*num2;
else if(op=='/')
cout<<endl<<"Division Result = "<<num1/num2;
else
cout<<endl<<"Wrong Operator!";
return 0;
}

Sum of the digits using WHILE Loop Average and Percentage


#include<iostream> #include<iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int num, rem, sum=0; int i, mark[5];
float sum=0, avg, perc; space = rowNum-1;
cout<<"Enter Marks obtained in 5 Subjects: "; for(i=1; i<=rowNum; i++)
for(i=0; i<5; i++) {
{ for(j=1; j<=space; j++)
cin>>mark[i]; cout<<" ";
sum = sum+mark[i]; space--;
} for(j=1; j<=(2*i-1); j++)
avg = sum/5; cout<<"*";
perc = (sum/500)*100; cout<<endl;
cout<<"\nAverage Marks = "<<avg; }
cout<<"\nPercentage Marks = "<<perc<<"%"; space = 1;
cout<<endl; for(i=1; i<=(rowNum-1); i++)
return 0; {
} for(j=1; j<=space; j++)
cout<<" ";
Odd or Even space++;
#include<iostream> for(j=1; j<=(2*(rowNum-i)-1); j++)
using namespace std; cout<<"*";
int main() cout<<endl;
{ }
int num; cout<<endl;
cout<<"Enter a Number: "; return 0;
cin>>num; }
if(num%2==0)
cout<<"\nIt is an Even Number.";
else
cout<<"\nIt is an Odd Number.";
cout<<endl;
return 0;
}

Diamond Pattern Half Pyramid


#include<iostream>
#include<iostream> using namespace std;
using namespace std; int main()
int main() {
{ int i, j;
int i, j, rowNum, space; for(i=0; i<5; i++)
cout<<"Enter the Number of Rows: "; {
cin>>rowNum; for(j=0; j<=i; j++)
cout<<"* "; cout<<"* ";
cout<<endl; cout<<endl;
} }
cout<<endl; cout<<endl;
return 0; return 0;
} }
OR OR
#include<iostream> #include<iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int i, j, n; int i, j, n;
cout<<"Enter number of rows: "<< "\n "; cout<<"Enter number of rows: "<< "\n ";
cin>>n; cin>> n;
for(i=0; i<n; i++) for(i=0; i<n; i++)
{ {
for(j=0; j<=i; j++) for(j=i; j<n; j++)
cout<<"* "; cout<<"* ";
cout<<endl; cout<<endl;
} }
cout<<endl; cout<<endl;
return 0; return 0;
} }

Inverted Half Pyramid 1D Array


#include<iostream> #include<iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int i, j; int item = 6;
for(i=0; i<6; i++) int inv[item] = {12, 13, 14, 15, 16, 17};
{ int num, qty;
for(j=i; j<6; j++)
for(i=0; i<3; i++)
cout << "Items Prices: "<<endl; {
for (int i=0; i<item;i++){ for(j=0; j<3; j++)
cout<< "Item "<< i+1<< "= " << inv[i]<<endl; cin>>mat1[i][j];
} }
cout << "Enter item number to update: "; cout<<"Enter Elements of Second Matrix: ";
cin>>num; for(i=0; i<3; i++)
cout << "How many: "; {
cin>>qty; for(j=0; j<3; j++)
inv[item-1]+=qty; cin>>mat2[i][j];
cout << "Updated Item prices: "<<endl; }
for (int i=0; i<item;i++){ cout<<"\nAdding the Two Given Matrix...\n";
cout<< "Item "<< i+1<< "= " << inv[i]<<endl; for(i=0; i<3; i++)
{
} for(j=0; j<3; j++)
return 0; mat3[i][j] = mat1[i][j]( +, -) mat2[i]
[j];
}
cout<<"Addition Result of Two Given Matrix is:\
n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cout<<mat3[i][j]<<" ";
cout<<endl;
}
return 0;
}

Add / Subtract Matrix Multiply Matrix


#include<iostream>
using namespace std; #include<iostream>
int main() using namespace std;
{ int main()
int mat1[3][3], mat2[3][3], i, j, mat3[3][3]; {
cout<<"Enter Elements of First Matrix: "; int matOne[3][3], matTwo[3][3], matThree[3][3];
int i, j, k, sum=0;
cout<<"Enter 9 Elements for First Matrix: "; }
for(i=0; i<3; i++) cout<<"\nOriginal Matrix is:\n";
{ for(i=0; i<3; i++)
for(j=0; j<3; j++) {
cin>>matOne[i][j]; for(j=0; j<3; j++)
} cout<<mat[i][j]<<" ";
cout<<"\nEnter 9 Elements for Second Matrix: "; cout<<endl;
for(i=0; i<3; i++) }
{ // copying the transpose of given matrix to
for(j=0; j<3; j++) matT[][]
cin>>matTwo[i][j]; for(i=0; i<3; i++)
} {
// Multiplying two matrices... for(j=0; j<3; j++)
for(i=0; i<3; i++) matT[j][i] = mat[i][j];
{ }
for(j=0; j<3; j++) cout<<"\nTranspose of Given Matrix is:\n";
{ for(i=0; i<3; i++)
sum=0; {
for(k=0; k<3; k++) for(j=0; j<3; j++)
sum = sum + (matOne[i][k] * cout<<matT[i][j]<<" ";
matTwo[k][j]); cout<<endl;
matThree[i][j] = sum; }
} cout<<endl;
} return 0;
cout<<"\nMultiplication Result:\n"; }
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cout<<matThree[i][j]<<"\t";
cout<<endl;
}
cout<<endl;
return 0;
}
Inversed 2D array
Transpose Matrix
#include<iostream> #include <iostream>
using namespace std; using namespace std;
int main()
{ int main() {
int mat[3][3], i, j, matT[3][3];
const int ROWS = 3;
cout<<"Enter 9 Elements for 3*3 Matrix: ";
for(i=0; i<3; i++)
const int COLS = 4;
{ int arr[ROWS][COLS] = {{1, 2, 3, 4},
for(j=0; j<3; j++) {5, 6, 7, 8},
cin>>mat[i][j]; {9, 10, 11, 12}};
// Print the original array
cout << "Original array: " << endl;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}

// Create and print the inversed array


int inversed[COLS][ROWS];
for (int i = 0; i < COLS; i++) {
for (int j = 0; j < ROWS; j++) {
inversed[i][j] = arr[j][i];
cout << inversed[i][j] << " ";
}
cout << endl;
}

return 0;
}

You might also like