Saturday, 9 April 2016

Some coding template to solve your code easily

Hi, Here I am going to share some of my ready made code that can you use when you are on any competition.
This codes can help you to solve the problem quickly as you need not to type this code as you can copy and paste.

Any numeric system converter


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// this code will help you to convert decimals number to any base numeric system/
// pass the value of 'i' that you need to convert the numeric system.
// like if you pass 'i'=2 this code you act as decimal to binary converter.
// if you pass 'i'=8 this code will act as decimal to octal converter.
// pass the value of 'nm' as the number to convert
// here the type of the passing number is as 'long long'. You can change the type as you need.
// the converted number is stored in 'dec' and 'dec; is returning the converted value.
long long conveter(int i, long long nm)
{
     long long  dec = 0, rem, num, base = 1;
     num = nm;
    while (num > 0)
    {
        rem = num % 10;
        dec = dec + rem * base;
        base = base * i;
        num = num / 10;
    }

    return dec;

}



Prime Number Checker


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// Returns '0' if its prime, and a '1' if its not prime.
int primecker(long long num)   
{
   int isprime = 0;
 for(Long long i = 2; i <= sqrt(num); i ++)
 {

  if((num% i) == 0)
  {
   isprime = 1;
   break;
  }
 }

 return isprime;
}

Decimal to Binary converter


1
2
3
4
5
6
7
8
9
// this code will convert decimal number to binary number
// just pass an 'int' number and the code will convert the number into binary and will save into 'bin' variable

int bin=0; // binary value will store here
void dictobin(int n)
{
    if(n/2!=0) dictobin(n/2);
    bin = bin*10 + n%2;
}

0 comments:

Post a Comment