/* Swapping 3 numbers without using extra variable */ #include<stdio.

h> void Swap(int *a,int *b,int *c) { *a = *a + *b + *c; *b = *a - (*b + *c); *c = *a - (*b + *c); *a = *a - (*b + *c); } int main() { int x=1,y=2,z=3; printf("BEFORE SWAPPING : %d %d %d\n",x,y,z); Swap(&x,&y,&z); printf("AFTER SWAPPING : %d %d %d",x,y,z); return 0; } /* End of Main */

/* Multiplication of two numbers using BITWISE OPERATORS ONLY */ #include<stdio.h> int main() { long int i,n,mul,mul2,count,temp,a,b,sum,carry,res,tot; printf("\nEnter any 2 numbers - num1,num2 : "); scanf("%ld,%ld",&mul,&n); mul2=temp=mul; for (i=2; i<=n; i++) { temp=mul; count=32; res=1; tot=sum=carry=0; while (count--) { a=temp & 0x1; b=mul2 & 0x1; if ((a^b==1) && (carry==1)) { sum=(a^b)^carry; carry=(a^b)&carry; }

else { sum=a^b|carry; carry=a&b; } temp=temp>>1; mul2=mul2>>1; tot+=res*sum; res=res*2; } mul2=tot; } printf("\n%3ld * %3ld = %3ld",mul,i-1,tot); return 0; } /* End of Main */

/* Check whether the given number x is equal to the value 2 power i or not using BITWISE operators ONLY */ #include<stdio.h> int main() { long x; printf("Enter a number : "); scanf("%ld",&x); if ((x & 0x1) == 0x0) // Given number x is equal to the value 2 power i printf("The given number %ld is EQUAL to the value 2 POWER something",x); else printf("The given number %ld is NOT EQUAL to the value 2 POWER something",x); return 0; } /* End of Main */

/* Maintaining TWO STACKS within a SINGLE ARRAY */ #include<stdio.h> #include<stdlib.h> #define MAX 10 int stack[MAX],top1,top2; void init() { top1=-1;

top2=10; } void Push1(int item) { stack[++top1]=item; } void Push2(int item) { stack[--top2]=item; } int Pop1() { return stack[top1--]; } int Pop2() { return stack[top2++]; } void Display() { int i; if(top1==-1) printf("\nStack1 : Empty"); else { printf("\nContent of Stack1 :\n"); for(i=0;i<=top1;i++) printf("%d\t",stack[i]); } if(top2==10) printf("\nStack2 : Empty"); else { printf("\nStack2 contains:\n"); for(i=MAX-1;i>=top2;i--) printf("%d\t",stack[i]); } } int main() { int item,ch; init(); while(1) { printf("\n\n\tMenu\n1.Push1\n2.Push2\n3.Pop1\n4.Pop2\n5.Display\n6.Exit");

printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1 : if ((top1 + 1) < top2) { printf("\nEnter item to Push into Stack1:"); scanf("%d",&item); Push1(item); } else printf("\nMemory is Full. Overflow Error"); break; case 2 : if ((top2 - 1) > top1) { printf("\nEnter item to Push into Stack2:"); scanf("%d",&item); Push2(item); } else printf("\nMemory is Full. Overflow Error"); break; case 3 : if(top1 <= -1) printf("\nError : Underflow on pop1"); else printf("\nPopped item from stack1 is : %d",Pop1()); break; case 4 : if(top2 >= 10) printf("\nError : Underflow on pop2"); else printf("\nPopped item from stack2 is : %d",Pop2()); break; case 5 : Display(); break;

case 6 : exit(0); default: printf("\nInvalid Choice"); } } return 0; } /* End of Main */

/* Counting vowels and consonants in a given line of text */ #include<stdio.h> #include<string.h> int main() { char str[80]; int i,vow=0,cons=0; printf("Enter a string : "); scanf("%[ ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]",str); for (i = 0; i < strlen(str); i++) if (str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U' || str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') vow++; else if (str[i] != ' ') // Ignore BLANK characters cons++; printf("\n\nThe given string is %s",str); printf("\n\nThe total number of VOWELS in a given string is %d",vow); printf("\nThe total number of CONSONANTS in a given string is %d",cons); return 0; } /* End of Main */

/* 4-letter word unscrambling */ #include<stdio.h> #include<string.h> int main() { int i,j,k,l,sum=6; char str[5]; printf("Enter a 4-letter word or string : ");

scanf("%s",str); if (strlen(str) == 4) { printf("The possible combinations of the given 4-letter word is shown."); for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) if (i != j) { for (k = 0; k < 4; k++) if ((k != i) && (k != j)) { l = sum - (i + j + k); printf("\n%c%c%c%c",str[i],str[j],str[k],str[l]); } } printf("\nTotal combinations = %d",4*3*2*1); } else printf("\nInvalid string. Length of the string must be 4-letters only "); return 0; } /* End of Main */ /* Note: Tested with TurboC++ compiler ONLY */ /* Print C FOR SWIMMERS on each line, forming a diagonal pattern */ #include<stdio.h> #include<conio.h> #include<dos.h> void main() { char str[]="C For Swimmers"; int x=1,y; for (y=1; y<=25; y++) { clrscr(); gotoxy(x,y); printf("%s",str); delay(250); if (y < 8) x+=2; else x+=3; } } /* End of main */

/* Note: Tested with TurboC++ compiler ONLY */ /* Compute the sum of values in the variable argument list */ #include<stdio.h> #include<stdarg.h> int Sum(int a,...) { int total=a; va_list ap; int arg; va_start(ap,a); while ((arg = va_arg(ap,int)) != 0) { total+=arg; } va_end(ap); return total; } void main() { clrscr(); printf("%d",Sum(5,6,7,8,9,10)); getch(); } /* End of main */ /* Note: Tested with TurboC++ compiler ONLY */ // Compute the sum of two values using function and return the result w/o using RETURN statement #include<stdio.h> #include<conio.h> int Sum(int a,int b) { _AX = a + b; // equivalent to : return (a + b); } void main(void) { clrscr(); printf("Result is = %d",Sum(4,6)); getch(); } /* End of main */

/* Note: Tested with TurboC++ compiler ONLY */ /* To find largest of two numbers using 'asm' statement */

#include<stdio.h> #include<conio.h> void main() { int val=85,num=77; clrscr(); asm mov ax,val asm cmp ax,num asm jg FirstNum asm mov ax,num FirstNum:printf("Largest of two numbers is %d",_AX); } /* End of main */ /* Note: Tested with TurboC++ compiler ONLY */ /* To reverse the words in a given line of text */ #include<stdio.h> #include<string.h> void main() { char ch,str[80],*res="",*temp,*temp1; clrscr(); printf("Enter a sentence :\n"); scanf("%[^\n]",str); temp=strtok(str," "); do { strcpy(temp1,temp); strcat(temp1," "); strcat(temp1,res); strcpy(res,temp1); temp=strtok(NULL," "); }while (temp != NULL); printf("\n Result : %s",res); getch(); } /* End of main */ /* Note: Tested with TurboC++ compiler ONLY */ /* To reverse a given line of test using recursive function */ #include<stdio.h> #include<conio.h> void reverse(void) { char c;

if ((c = getchar()) != '\n') reverse(); putchar(c); return; } void main() { clrscr(); printf("Please enter a line of text below\n"); reverse(); } /* End of main */

Self-reproducible programs - programs that produce their own source code. I included an interesting collection of self-reproducing code in C programming language. Few of them might generate warnings but should compile fine. Source : Obfuscated C Code contest - www.ioccc.org Note: This page can be best viewed with IE 6.0 or above. [Q001] Self-reproducible C code : main(){char*p="main(){char*p=%c%s%c;(void)printf(p,34,p,3 4,10);}%c";(void)printf(p,34,p,34,10);} OUTPUT: Same as the actual source code. main(){char*p="main(){char*p=%c%s%c;(void)printf(p,34,p,34,10);}%c";(void) printf(p,34,p,34,10);} [Q002] Self-reproducible C code : main(a){a="main(a){a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);} OUTPUT: Same as the actual source code. main(a){a="main(a){a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);} [Q003] Self-reproducible C code :

main(){char*a="main(){char*a=%c%s%c;int b='%c';printf(a,b,a,b,b);}";int b='"';printf(a,b,a,b,b);} OUTPUT: Same as the actual source code. main(){char*a="main(){char*a=%c%s%c;int b='%c';printf(a,b,a,b,b);}";int b='"';printf(a,b,a,b,b);}

/*****************************************************************/ // File : primenum.c // Purpose : Checks whether the given number is PRIME or NOT a PRIME. // Author : Nanda Kishor K N // Mail Id : knnkishor@yahoo.com, nandakishorkn@rediffmail.com // Website : www.c4swimmers.esmartguy.com ( WEB MASTER ) // Group : c4swimmers@yahoogroups.com ( MODERATOR ) /*****************************************************************/ #include<stdio.h> #include<math.h> /* Name : main() Purpose : This is the main function that accepts the number and checks whether the given number is prime or not. Uses the built-in sqrt() function defined in math.h file Parameter : None */ main() { // Variable Declarations & Initialization // Naming conventions are used like all integer variables int iNum,iFlag=1,iIdx; // Set the Flag printf("Enter number : "); scanf("%d",&iNum); // Check whether the given number n, which is divisible by 2,3,4,...,sqrt(n) // If the number is divisible then it is NOT a PRIME number // Otherwise it is a PRIME number for (iIdx = 2; iIdx <= (int)sqrt(iNum); iIdx++) {

if (iNum % iIdx == 0) { iFlag=0; // Reset the Flag & terminate the loop break; } } // Verify the flag status and print the given number as PRIME or NOT a PRIME if (iFlag != 0) printf("It's a PRIME number\n"); else printf("\nIt's NOT a PRIME number\n"); return 0; } // End of main() program

/*****************************************************************/ // File : biggest.c // Purpose : To find the largest and second largest element in a list // Author : Nanda Kishor K N // Mail Id : knnkishor@yahoo.com, nandakishorkn@rediffmail.com // Website : www.c4swimmers.esmartguy.com ( WEB MASTER ) // Group : c4swimmers@yahoogroups.com ( GROUP OWNER ) /*****************************************************************/ #include <stdio.h> /* Name : main() Purpose : This is the main function that accepts 'N' number of elements and computes the largest and second largest element in a list. This does not call any other function. Parameter : None */ int main() { // Variable Declarations & Initialization // Naming conventions are used like all integer variables should begin with 'i' int iList[50], iIdx, iN, iBig, iSec; printf("\nHow many numbers in the list ? "); scanf("%d",&iN); // Accept 'N' number of elements

printf("\nEnter the elements:\n"); for(iIdx = 0; iIdx < iN; iIdx++) { scanf("%d",&iList[iIdx]); } // Computing the largest and second largest element in a list iBig = iList[0]; iSec = 0; for(iIdx = 1; iIdx < iN; iIdx++) { if (iList[iIdx] > iBig) { iSec = iBig; // Second biggest iBig = iList[iIdx]; // Biggest } else if (iList[iIdx] > iSec) iSec = iList[iIdx]; // Second biggest } // Display the 'N' number of elements in a list printf("\n\nThe elements in the list are: "); for(iIdx = 0; iIdx < iN; iIdx++) { printf("\n%d",iList[iIdx]); } // Display the result or print the biggest and second biggest element printf("\n\nThe biggest element in the list is: %d",iBig); printf("\nThe second biggest element in the list is: %d",iSec); } return 0; // End of main() program

/*****************************************************************/ // File : guessnum.c // Purpose : Game of guessing the random number. // Author : Nanda Kishor K N // Mail Id : knnkishor@yahoo.com, nandakishorkn@rediffmail.com // Website : www.c4swimmers.esmartguy.com ( WEB MASTER ) // Group : c4swimmers@yahoogroups.com ( GROUP OWNER ) /*****************************************************************/ #include <stdio.h> #include <stdlib.h>

/* Name : main() Purpose : This is the main function that allows you to guess the random number with the help of hints in 10 chances only. This does not call any other function. Parameter : None */ int main() { // Variable Declarations & Initialization // Naming conventions are used like all integer variables should begin with 'i' int iRandom,jIdx,iNum; printf("Find out the number in the range 0 - 500, using hints given by the computer."); // Generate a random number iRandom = rand() % 500; printf("\nI will give you ten chance to find out the number.\nNow the number puzzle starts.\n\n"); for (jIdx = 1; jIdx <= 10; jIdx++) { // Guess the number??? printf("\nNumber %d = ",jIdx); scanf("%d",&iNum); if (iNum < iRandom) // Guessed Number < Generated Random Number printf("Number is less than the given number."); if (iNum > iRandom) // Guessed Number > Generated Random Number printf("Number is greater than the given number."); if (iNum == iRandom) // Guessed Number == Generated Random Number { printf("\n\t\t\t\t!!! Wonderful !!!"); printf("\nIt's great!!! You solved the puzzle & I heartly wish good luck to you."); break; } }

return 0; } // End of Program

Sign up to vote on this title
UsefulNot useful