Sunday 11 December 2016

Basic C++ and Common Build in Function.

/**Bismillahir Rahmanir Rahim.**/

সি প্লাস প্লাস প্রোগ্রাম রান করানোর জন্য ফাইলের এক্সটেনশন অবশ্যই .cpp হতে হবে।

*Print 1 to N using C.
----------------------
#include <stdio.h>

int main()
{
    int N;
    scanf("%d", &N);
    int i;
    for(i=1; i<=N; i++)
        printf("%d\n", i);
    return 0;
}



*Print 1 to N using C++.
------------------------
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int N;
    cin >> N;
    for(int i=1; i<=N; i++)
        cout << i << endl;
    return 0;
}


*Taking Input:
--------------
int N;
double X;
char str[100];
cin >> N;
cin >> X;
cin >> N >> X >> str;


*Giving Output:
---------------
cout << N << endl:
cout << X << endl;
cout << N << X << str << endl;
cout << "Maira Laichere!!" << endl;


*Is N divisible by 4 and 7??
----------------------------
if(N%4==0 and N%7==0)
 cout << "YES" << endl;
else
 cout << "NO" << endl;


*Is N divisible by 4 or 7??
----------------------------
if(N%4==0 or N%7==0)
 cout << "YES" << endl;
else
 cout << "NO" << endl;


*ফর লুপের প্রথম অংশে আমরা চাইলে ভেরিয়েবল ডিকলেয়ার করতে পারি।
------------------------------------------------
for(int i=1; i<=100; i++)
 cout << "What The Hell!!!" << endl;


*long long int ডাটা টাইপে সংখ্যার পড়ে suffix "ll" লাগাতে হবে।
-----------------------------------------------------
long long int num, ans;
ans = num/2;  (wrong)
ans = num/2ll; (right)

---------------
---------------
কমন বিল্ডইন ফাংশন
---------------
---------------

*ফ্লোরিংঃ
------
int ans = floor(a/b);


*সেইলিংঃ
-------
int ans = ceil(a/b);
or,
int ans = (a+b-1)/b; //ceiling law(safe)


*পরমমানঃ
-------
int ans = abs(a - b);


*Maximmum, Minimum, Midium:
---------------------------
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int a, b, c;
    int mx, mn, mid;
    cin >> a >> b >> c;
    mx = max(a, max(b, c));
    mn = min(a, min(b, c));
    mid = (a + b + c) - mx - mn;
    cout << mx << " " << mid << " " << mn << endl;
    return 0;
}


*এরের ইনডেক্স গুলোতে "0" অথবা "-1" রাখাঃ
--------------------------------
memset(ara, 0, sizeof(ara); // for put 0
memset(ara, -1, sizeof(ara); //for put -1


* এরে সর্ট (ছোট থেকে বড়)
---------------------
sort(ara, ara + N); //N is array size.


* এরে সর্ট (বড় থেকে ছোট)
---------------------
int WAY(int x, int y) //Write this before main function.
{
    return x>y;
}

sort(ara, ara+N, WAY); //Write this on main function.


*গসাগুঃ
-------
int ans = __gcd(a, b);


*লসাগুঃ
------
int lcm = (a/__gcd(a, b))*b;


*কোন সংখ্যার বাইনারিতে একের সংখ্যাঃ
--------------------------
int ans = __buildin_popcount(X); //when x int.
int ans = __buildin_popcountll(X); //when x long long int.


*লগারিদমঃ
--------
int ans = log2(X); // for base 2.
int ans = log10(X); // for base 10.
int ans = log10(X)/log10(N); //for base N.


*a to the power b:
-------------------
int ans = pow(a, b);
int ans = ceil(pow(a,b)); //safe.

1 comment:

Note: only a member of this blog may post a comment.