program using conditional operators to determine whether a year entered through the keyboard is a leap year or not in c




#include
main()
{

int year;

printf("Enter a year:");
scanf("%d", &year);

(year%4==0)?(printf("Leap Year")):(printf("Not a Leap Year"));

}

Program to print all the ASCII values characters using a while loop. The ASCII values from 0 to 255.


#include
main()
{
int num;
printf("Printing ASCII values Table...\n\n");
num = 1;
while(num<=255)
{
printf("\nValue:%d = ASCII Character:%c", num, num);

/*This change has been made as per the comment. Thank you anonymous Blog Viewer ... */
num++;
}
printf("\n\nEND\n");
}


Program to Detection of a Prime Number


#include
main()
{
int num, i;

printf ("Enter a number:");
scanf ("%d", &num);

i=2;
while (i<=num - 1)

{
if (num % i ==0)

{
printf ("Not a prime number");
break;
}

i++;
}

if (i == num)

printf ("Prime Number");
}

If four-digit number is input through the keyboard, write a program to obtain the sum of the first and the last digit of this number.


#include

main ()
{
int number, last_digit, first_digit, total;
printf ("Enter the number which is to be Work out: ");

scanf ("%d", &number);


last_digit = number % 10;

total = last_digit;

first_digit = (number / 1000) % 10;

total = total + first_digit;

printf ("The total of the first and the last digit of the entered number is: %d", total);

}


Program to reverses a Given Five Digit number in C


#include
main()
{
int number, rev_num, next_digit,last_digit;
printf ("Enter the Five Digit number that is to be reversed: ");
scanf("%d", &number);

last_digit = number - ((number / 10) * 10); /*units place*/

rev_num = last_digit; /* 5 */

next_digit = (number / 10) - ((number / 100) * 10); /*tenth's place*/

rev_num = (rev_num * 10) + next_digit; /*54*/

next_digit = (number / 100) - ((number / 1000) * 10); /*hundred's place*/

rev_num = (rev_num * 10) + next_digit; /*543*/

next_digit = (number / 1000) - ((number / 10000) * 10); /*thousand's place*/

rev_num = (rev_num * 10) + next_digit; /*5432*/

next_digit = (number / 10000) - ((number / 100000) * 10); /*ten thousand's place*/

rev_num = (rev_num * 10) + next_digit; /*54321*/

printf ("The Reversed Number is: %d",rev_num);
}




Output : Enter the Five Digit number that is to be reversed:

12345

The Reversed Number is: 54321

Program to interchanges the given two numbers in 2 variables


#include

main ()
{
int A,C,D;
printf ("Enter the value of C and D: ");
scanf ("%d %d", &C, &D);

A=C;
C=D;
D=A;
printf ("\nThe exchanged values of C and D are: %d and %d ", C, D);
}

Program to converts value entered in Km to inches, centimetres, feet and metres In C


#include

/* Program to converts value entered in Km to inches,
centimetres, feet and metres In C. */


main()
{
float km,m,cm,inch,foot;
printf("Enter the distance between the two cities in Km: ");
scanf("%f", &km);
inch = 39370*km;
foot = 3281*km;
cm= 100000*km;
m = 1000*km;
printf ("\n Distance converted to inches: %f", inch);
printf ("\n Distance converted to feet: %f", foot);
printf ("\n Distance converted to centimeters: %f", cm);
printf ("\n Distance converted to meters: %f", m);
}

Compiling Multi-File Programs in C

This process is rather more involved than compiling a single file program. Imagine a program in three files prog.c, containing main(), func1.c and func2.c. The simplest method of compilation (to produce a runnable file called a.out) would be


cc prog.c func1.c func2.c
If we wanted to call the runnable file prog we would have to type

cc prog.c func1.c func2.c -o prog
In these examples, each of the .c files is compiled, and then they are automatically linked together using a program called the loader ld.

How to Divide a Program between Several Files

Where a function is spread over several files, each file will contain one or more functions. One file will include main while the others will contain functions which are called by others. These other files can be treated as a library of functions.

Programmers usually start designing a program by dividing the problem into easily managed sections. Each of these sections might be implemented as one or more functions. All functions from each section will usually live in a single file.

Where objects are implemented as data structures, it is usual to to keep all functions which access that object in the same file. The advantages of this are;

* The object can easily be re-used in other programs.
* All related functions are stored together.
* Later changes to the object require only one file to be modified.

Where the file contains the definition of an object, or functions which return values, there is a further restriction on calling these functions from another file. Unless functions in another file are told about the object or function definitions, they will be unable to compile them correctly.

The best solution to this problem is to write a header file for each of the C files. This will have the same name as the C file, but ending in .h. The header file contains definitions of all the functions used in the C file.

Whenever a function in another file calls a function from our C file, it can define the function by making a #include of the appropriate .h file.

The Bit Array Improvement in C

When it comes to memory usage, the quickest and most obvious improvement comes with the realization that each candidate number either is or is not prime, and that's all you need to know. It's binary. That means each candidate number could be represented by a single bit, instead of the 8 bit unsigned char used in the preceding article. Theoretically, this would allow an 8 fold increase in the range to be looked at.

To accomplish this we need to write put and get routines to map info into single bits of an array whose byte count is now topCandidate/8. The bit arithmetic inside those put and get routines will doubtlessly slow the algorithm. The question is, how much. The answer is simple enough -- it depends on how well you write them.




#include
#include
#include
#include

typedef unsigned long long bignum;

typedef struct
{
unsigned int *p; /* pointer to array */
int bitsPerByte; /* 8 on normal systems */
int bytesPerInt; /* sizeof(unsigned int) */
int bitsPerInt; /* for bit arithmetic */
bignum bitsInArray; /* how many bits in array */
bignum intsInArray; /* how many uints to give necessary bits */
} BITARRAY;

void freeBitArray(BITARRAY *ba)
{
free(ba->p);
free(ba);
}

BITARRAY * createBitArray(bignum bits)
{
BITARRAY *ba = malloc(sizeof(BITARRAY));
assert(ba != NULL);
ba->bitsPerByte = 8;
ba->bytesPerInt = sizeof(unsigned int);
ba->bitsPerInt = ba->bitsPerByte * ba->bytesPerInt;
ba->bytesPerInt = sizeof(unsigned int);
ba->bitsInArray = bits;
ba->intsInArray = bits/ba->bitsPerInt + 1;
ba->p = malloc(ba->intsInArray * ba->bytesPerInt);
assert(ba->p != NULL);
return ba;
}

void setBit(BITARRAY *ba, bignum bitSS)
{
unsigned int *pInt = ba->p + (bitSS/ba->bitsPerInt);
unsigned int remainder = (bitSS % ba->bitsPerInt);
*pInt |= (1 << pint =" ba-">p + (bitSS/ba->bitsPerInt);
unsigned int remainder = (bitSS % ba->bitsPerInt);
unsigned int mask = 1 << mask =" ~mask;" pint =" ba-">p + (bitSS/ba->bitsPerInt);
unsigned int remainder = (bitSS % ba->bitsPerInt);
unsigned int ret = *pInt;
ret &= (1 << intss="0;">intsInArray; intSS++)
{
*(ba->p + intSS) = 0;
}
}

void setAll(BITARRAY *ba)
{
bignum intSS;
for(intSS=0; intSS <= ba->intsInArray; intSS++)
{
*(ba->p + intSS) = ~0;
}
}

void printPrime(bignum bn)
{
static char buf[1000];

sprintf(buf, "%ull", bn);
buf[strlen(buf) - 2] = '\0';
printf("%s\n", buf);
}

void findPrimes(bignum topCandidate)
{
BITARRAY *ba = createBitArray(topCandidate);
assert(ba != NULL);

/* SET ALL BUT 0 AND 1 TO PRIME STATUS */
setAll(ba);
clearBit(ba, 0);
clearBit(ba, 1);

/* MARK ALL THE NON-PRIMES */
bignum thisFactor = 2;
bignum lastSquare = 0;
bignum thisSquare = 0;
while(thisFactor * thisFactor <= topCandidate)
{ /* MARK THE MULTIPLES OF THIS FACTOR */
bignum mark = thisFactor + thisFactor;
while(mark <= topCandidate)
{
clearBit(ba, mark); mark += thisFactor;
}
/* PRINT THE PROVEN PRIMES SO FAR */
thisSquare = thisFactor * thisFactor;
for(;lastSquare < ba =" createBitArray(77);
" ss="0;">p));
}

int main(int argc, char *argv[])
{
bignum topCandidate = 1000;
if(argc > 1)
topCandidate = atoll(argv[1]);
/* test(); */
findPrimes(topCandidate);
return 0;
}

The Array Method (Sieve of Eratosthenes) in C


#include 
#include
#include
#include

typedef unsigned long long bignum;

void printPrime(bignum bn)
{
static char buf[1000];

sprintf(buf, "%ull", bn);
buf[strlen(buf) - 2] = '\0';
printf("%s\n", buf);
}

void findPrimes(bignum topCandidate)
{
char * array = malloc(sizeof(unsigned char) * (topCandidate + 1));
assert(array != NULL);

/* SET ALL BUT 0 AND 1 TO PRIME STATUS */
int ss;
for(ss = 0; ss <= topCandidate+1; ss++)
*(array + ss) = 1;
array[0] = 0;
array[1] = 0;

/* MARK ALL THE NON-PRIMES */
bignum thisFactor = 2;
bignum lastSquare = 0;
bignum thisSquare = 0;
while(thisFactor * thisFactor <= topCandidate)
{
/* MARK THE MULTIPLES OF THIS FACTOR */
bignum mark = thisFactor + thisFactor;
while(mark <= topCandidate)
{
*(array + mark) = 0;
mark += thisFactor;
}

/* PRINT THE PROVEN PRIMES SO FAR */
thisSquare = thisFactor * thisFactor;
for(;lastSquare < thisSquare; lastSquare++)
{
if(*(array + lastSquare)) printPrime(lastSquare);
}

/* SET thisFactor TO NEXT PRIME */
thisFactor++;
while(*(array+thisFactor) == 0) thisFactor++;
assert(thisFactor <= topCandidate);
}

/* PRINT THE REMAINING PRIMES */
for(;lastSquare <= topCandidate; lastSquare++)
{
if(*(array + lastSquare)) printPrime(lastSquare);
}
free(array);
}

int main(int argc, char *argv[])
{
bignum topCandidate = 1000;
if(argc > 1)
topCandidate = atoll(argv[1]);
findPrimes(topCandidate);
return 0;
}

Using Only Primes As Divisors in C


#include
#include
#include
#include

typedef unsigned long long bignum;

struct primerec
{
bignum bn;
struct primerec * next;
};

void printPrime(bignum bn)
{
static char buf[1000];

sprintf(buf, "%ull", bn);
buf[strlen(buf) - 2] = '\0';
printf("%s\n", buf);
}

void freeList(struct primerec *head)
{
struct primerec *thisPrime = head;
while(1)
{
struct primerec *nextPrime = thisPrime->next;
free(thisPrime);
if(nextPrime == NULL)
break;
else
thisPrime = nextPrime;
}
}

void findPrimes(bignum topCandidate)
{
struct primerec *firstPrime = malloc(sizeof(struct primerec));
assert(firstPrime != NULL);
struct primerec *latestPrime = firstPrime;
firstPrime->bn = 2;
firstPrime->next=NULL;
bignum candidate = 2;
while(candidate <= topCandidate)
{
struct primerec *thisPrime = firstPrime;
int prime = 1;
while(thisPrime->bn * thisPrime->bn <= candidate)
{ if(candidate % thisPrime->bn == 0)
{
prime = 0;
break;
}
thisPrime = thisPrime->next;
}
if(prime)
{
printPrime(candidate);
latestPrime->next = malloc(sizeof(struct primerec));
assert(latestPrime->next != NULL);
latestPrime = latestPrime->next;
latestPrime->bn = candidate;
latestPrime->next = NULL;
}
candidate++;
}
freeList(firstPrime);
}

int main(int argc, char *argv[])
{
bignum topCandidate = 1000;
if(argc > 1)
topCandidate = atoll(argv[1]);
findPrimes(topCandidate);
return 0;
}

The Obvious Method for Finding Primes


#include 
#include
#include
#include

typedef unsigned long long bignum;

void printPrime(bignum bn)
{
static char buf[1000];

sprintf(buf, "%ull", bn);
buf[strlen(buf) - 2] = '\0';
printf("%s\n", buf);
}

void findPrimes(bignum topCandidate)
{
bignum candidate = 2;
while(candidate <= topCandidate)
{ bignum trialDivisor = 2;
int prime = 1;
while(trialDivisor * trialDivisor <= candidate)
{ if(candidate % trialDivisor == 0) { prime = 0; break; } trialDivisor++; } if(prime) printPrime(candidate); candidate++; } } int main(int argc, char *argv[]) { bignum topCandidate = 1000; if(argc > 1)
topCandidate = atoll(argv[1]);
findPrimes(topCandidate);
return 0;
}