You are on page 1of 13

ELECTRONİC ENGİNEERİN

NARGİZ GASİMOVA
@nqasimova3@std.beu.edu.az
Şifrə :Nərgiz2004.
1. Functions in Cpp

#include <iostream>

int square(int num) {

return num * num;

int main() {

int A, B;

std::cin >> A >> B;

int sum1 = A + B;

std::cout << square(sum1) << std::endl;

std::cout << sum1 << std::endl;

std::cin >> A >> B;

int sum2 = A + B;

std::cout << square(sum2) << std::endl;

std::cout << sum2 << std::endl;

std::cin >> A >> B;

int sum3 = A + B;

std::cout << square(sum3) << std::endl;

std::cout << sum3 << std::endl;

return 0;
}

2. Parameters, Arguments

#include <iostream>

using namespace std;

string greet(string n,string m ) {

return m+n;

int main() {

string n = " Alice";

string m="Hello,";

cout<<greet(n,m)<<"!";

return 0;

3. Function within a function


#include <iostream>

#include <string>

#include <algorithm>

using namespace std;

void printGreeting(const string& name) {

string upperGreeting = "Hello, " + name + "!";


transform(upperGreeting.begin(), upperGreeting.end(),
upperGreeting.begin(), ::toupper);

cout<< "Final Result: " << upperGreeting << endl;

int main() {

string name = "Alice";

printGreeting(name);

return 0;

4. Test cases with multiple types of input


#include <bits/stdc++.h>

using namespace std;

int main()

int t;

cin>>t;

while(t--)

int A,B;

string S;

cin>>A>>B;

cin>>S;

cout<<A<<" "<<B<<" "<<S<<endl;


}

return 0;

5. How to use custom inputs


#include <bits/stdc++.h>

using namespace std;

int main()

int t;

cin>>t;

while(t--)

int N;

cin>>N;

cout<<N+1<<endl;

return 0;

6. Number mirror - Negative integer


#include <bits/stdc++.h>

using namespace std;

int main()

{
int t;

cin>>t;

while(t--)

int N;

cin>>N;

cout<<-N<<endl;

return 0;

7. String mirror - Double strings

#include <bits/stdc++.h>

using namespace std;

int main()

int t;

cin>>t;

while(t--)

string S,X;

cin>>S; Store the value of string S concatenated with itself to variable X

X=S+S;

cout<<X<<endl;
}

return 0;

8. Chef and Instant Noodles

#include <iostream>

using namespace std;

int main() {

int x,y;

cin>>x>>y;

cout<<x*y;

return 0;

9. If & Else Statements


#include <bits/stdc++.h>

using namespace std;

int main() {

int Age = 25;

int Vage = 18;

if (Age >= Vage) {

cout << "Old enough to vote!";

} else {
cout << "Not old enough to vote.";

return 0; }

10. Practice problem – Fitness


#include <bits/stdc++.h>

using namespace std;

int main()

int t;

cin>>t;

while(t--)

int X,Y;

cin>>X;

Y=(X*2)*5;

cout<<Y<<endl;

return 0;

11. Else Statement


#include <bits/stdc++.h>
using namespace std;

int main() {

int r;

int k;

r = 24;

k = 32;

if (r > k) {

cout << "Ram is heavier than Karan." << endl;

} else if (r < k) {

cout << "Karan is heavier than Ram" << endl;

else {

cout << "Ram & Karan have the same weight!" << endl;

r = 78;

k = 78;

if (r > k) {

cout << "Ram is heavier than Karan.";

} else if (r < k) {

cout << "Karan is heavier than Ram";

}
else {

cout << "Ram & Karan have the same weight!";

return 0;

12. User input Loop


#include <bits/stdc++.h>

using namespace std;

int main() {

int num;

cin >> num;

int a = 0;

while (a < num ) {

cout << a << endl;

a = a+1;

return 0;

13. Return values


#include <iostream>
using namespace std;

int calculateSquare(int a,int b) {


return a*a+2*a*b+b*b;

}
int calculateSum(int a,int b){
return a+b;
}
int main() {
int t = 3;
for (int i = 0; i < t; i++) {
int a, b;
cin >> a >> b;
cout<<calculateSquare(a,b)<<endl<<calculateSum( a, b)<<endl;
}
return 0;
}

14. Variable scope


#include <iostream>
using namespace std;

// Global variable
int globalVar = 10;

void displayScopes() {
// Local variable
int localVar = 20;

// Accessing and printing both the global variable and local variable
cout << localVar << endl; // Should print 20
cout << globalVar << endl; // Should print 10
}

int main() {
// Calling the function
displayScopes();
return 0;
}

15. Some useful math functions


#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
double X = 53.56;
double Y = 3;
int A = 45;
int B = 20;
cout << abs(A - B) << "\n";
cout << A % B << "\n";
cout << ceil(X) << "\n";
cout << floor(X) << "\n";
cout << pow(X,Y) << "\n";
return 0;
}

16. Problem (Make Avg) - Solve


sub-components

#include <bits/stdc++.h>

using namespace std;

int main ()

int A = 15;
int C = 19;

int B = (A + C)/2;

cout<<B<<endl;

A = 16;

C = 18;

B = (A + C)/2;

cout<<B<<endl;

18. Index & printing specific elements of an Array


#include <bits/stdc++.h>
using namespace std;

int main() {

int num[ 5 ] = {1, 2, 3, 4, 5 };


cout << num[ 2 ];

return 0;
}

19. Displaying elements

#include <bits/stdc++.h>
using namespace std;

int main() {
string week[4] = {"Monday", "Tuesday", "Wednesday",
"Thursday"};
cout << week[ 2 ] << endl;
cout << week[ 3 ];

return 0;
}

20. Displaying the count of elements

#include <bits/stdc++.h>
using namespace std;
int main() {
int Num[6] = {10, 20, 30, 40, 50, 60};
cout << sizeof(Num) / 4;
return 0;
}

You might also like