CPP Day 1 - Programming
CPP Day 1 - Programming
#include <iostream>
using namespace std;
int main()
{
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a;
cin >> a;
cout << a;
return 0;
}
Data Types
1. Int
2. Char
3. Float
4. Double
Operators in C++
1. Addition ( + )
2. Subtraction ( - )
3. Multiplication (*)
4. Division ( / )
5. Modulo Division ( % )
2023-24
2
#include <iostream>
using namespace std;
int main()
{
int num = 15;
// Condition
if (num > 0)
cout << "Positive";
else if (num < 0)
cout << "Negative";
else
cout << "Zero";
return 0;
}
2023-24
3
#include <iostream>
using namespace std;
int main ()
{
int num = 14;
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
int num1 = 15, num2 = 20;
return 0;
}
2023-24
4
#include <iostream>
using namespace std;
int main ()
{
int first = 10, second = 20, third = 30;
else
cout << third << " is the greatest";
return 0;
}
2023-24
5
Loops in C++
1. For Loop
for (initialization; condition; increment)
{
// body of the loop
// statements we want to execute
}
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 5; i++)
{
cout << "Hello World\n";
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n = 5;
int sum=0;
// from 1 to n
for(int i=1; i<=n; i++)
{
sum = sum + i;
}
return 0;
}
2023-24
6
Factorial of a Number
#include <iostream>
using namespace std;
int main()
{
int n = 5;
int fact = 1;
return 0;
}
Power of a Number
#include <iostream>
using namespace std;
int main()
{
int num = 2;
int power = 5;
int ans = 1;
return 0;
}
2023-24
7
Prime Number
#include <bits/stdc++.h>
using namespace std;
int main()
{
int num = 13;
// 1 and the number itself
int divisors = 2;
if( divisors == 2 )
cout<< "It is Prime";
else
cout<< "It is not Prime";
return 0;
}
O(n/2) Complexity
2023-24
8
O(sqrt(n)) Complexity
Functions in C++
2023-24
9
#include <bits/stdc++.h>
using namespace std;
void calArea(int a)
{
int area = a * a;
cout <<"Area is " << area << endl;
}
int main()
{
calArea( 5 );
calArea( 7 );
return 0;
}
#include <iostream>
using namespace std;
int calArea(int a)
{
int area = a * a;
return area;
}
int main()
{
return 0;
}
2023-24