Friday, October 9, 2015

Find the Factorial of a Number (Iterative way)

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int fact(int n)
{
    int i, ans=1;
    for(i=1; i<=n; i++)
        ans*=i;
    return ans;
}
int main()
{
    int n;
    cin>> n;
    cout << fact(n) << endl;
}

Find Factorial of a Number (Recursively)

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int fact(int n)
{
    if(n==1) return 1;
    return n*fact(n-1);
}

int main()
{
    int n;
    cin>> n;
    cout << fact(n) << endl;
}

1214 - Large Division (LightOJ)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;

int main()
{
    //freopen("in.txt" , "r", stdin);
    string a;
    long long b, rem;
    int test, t, i;
    cin>> test;
    for(t=1; t<=test; t++)
    {
        cin >> a >> b;
        if(b<0)
            b = b*(-1);
        int len = a.length();
        rem=0;
        for(i=0; i<len; i++)
        {
            if(a[i]=='-')
                i=1;
            rem*=10;
            rem+=(a[i]-'0');
            rem%=b;
        }
        if(rem==0)
            printf("Case %d: divisible\n", t);
        else
            printf("Case %d: not divisible\n", t);
    }
}

Compare equality of two string in C

#include <stdio.h> #include<string.h> int main() {     char* country = "Bangladesh";     char* country2;     ...