# Category: C

• ## Hacking with integer divisibility

/* work copied from http://www.sanfoundry.com/c-program-number-divisible-by-5/ */ #include <stdio.h> numberdivi() { int i, num1, num2, count = 0, sum = 0; printf(“Enter the value of num1 and num2 : “); scanf(“%d %d”,&num1,&num2); for (i = num1; i < num2 ; i++) { if ((i % 5) == 0) { printf(“%3d,”,i); count++; sum = sum + i; […]

• ## How to write an algorithm to find factorial of a given number ?

#include <stdio.h> factorial() { int p = 1; int i = 1; int n; printf(“Enter a Number greater than 0 \n”); scanf(“%d”,&n); while ( i <= n) { p = p * i; i = i + 1; } printf(“Factorial of the input is %d \n”,p); }

• ## Hacking with an algorithm to find prime factor of a number

\$./app Sign detection  Power of two  Counting No. of bits set  Set or clear bits without branching Find maximum value Finding least common multiple Finding the greatest commom divisor Finding if a number is an Armstrong number or not Finding if a given number is prime number Finding the number of twin […]

• ## How to write an algorithm to find if a number is palindrome or not

#include <stdio.h> palindrome() { int n, reverse = 0, temp; printf(“Enter a number to check if it is a palindrome or not: “); scanf(“%d”,&n); temp = n; while ( temp != 0 ) { reverse = reverse * 10; reverse = reverse + temp % 10; temp = temp/10; } if ( n == reverse […]

• ## Tinkering with an Armstrong number finding program function

\$./app Sign detection  Power of two  Counting No. of bits set  Set or clear bits without branching Find maximum value Finding least common multiple Finding the greatest commom divisor Finding if a number is an Armstrong number or not 8 Enter a positive integer: 153 153 is an Armstrong number \$./app Sign […]

• ## Hacking with the program to find the greatest common factor

\$./app Sign detection  Power of two  Counting No. of bits set  Set or clear bits without branching Find maximum value Finding least common multiple Finding the greatest commom divisor 7 Enter the two values for finding GCD: 12 6 GCD is: 6 \$./app Sign detection  Power of two  Counting No. […]

• ## How to write an algorithm to find the number of bits set in a typical number ?

#include <stdio.h> cbs () { unsigned int v; /* count the number of bits set in v */ unsigned int c; /* c accumulates the total bits set in v */ printf(“Enter v”); scanf(“%u”,&v); for (c = 0; v ; c++) { v = v & (v – 1); /* clear the least significant bit […]

• ## How to write an algorithm to detect if two integers have opposite signs ?

/* detect if two integers have opposite signs */ #include <stdio.h> int x , y; typedef int bool; bool f; int sdetect(x , y) { f = ((x ^ y) < 0); if ( f == 1 ) { printf(“They have opposite signs\n”); } else { printf(“They have same signs\n”); } }

• ## How to write a program to connect to a PostgreSQL server ? (program may not be perfect )

#include <stdio.h> #include <stdlib.h> #include <postgresql/libpq-fe.h> int main(int argc, char **argv) { const char *conninfo; const char *serverversion; PGconn *conn; const char *paramtext = “server_version”; conninfo = “hostaddr = 127.0.0.1 dbname = test user = earl password = bigshot”; conn = PQconnectdb(conninfo); if ( PQstatus(conn) != CONNECTION_OK ) { printf(“Unable to establish connection: %s”,PQerrorMessage(conn)); return […]

• ## How to write a program to find endianess of a machine ?

#include <stdio.h> #include <stdlib.h> static inline int little_endian() { int endian = 1; return (0 == (*(char *)&endian)); } int main (int argc, char **argv) { printf(“This machine is “); little_endian() ? printf(“little”) : printf(“big”); printf(” endian\n”); exit(0); }