C Sample Codes

conversion and flag characters
//conversionflag.cpp //Program that uses conversion and flag characters void main { 	int c = 6000; float a = 2.9, b = 0.00015; printf("%f %f %d\n", a, b, c); printf("%8f %8f %8d\n", a,b,c); printf("%8.3f %8.3f %8.3e\n",a,b,c); printf("%e %e %e\n",a,b,c); printf("%12.4e %12.4e %12.4d\n",a,b,c); printf("%-8f %-8f %-8d\n",a,b,c); printf("%+8f %+8f %+8d\n",a,b,c); printf("#8f %#8f %#8d\n",a,b,c); printf("%g %g %g\n",a,b,c); printf("%#g %#g %#g\n",a,b,c); }
 * 1) include

if .. else statement
/*program to illustrate the if .. else .. statement*/ void main { 	int i; 	printf("\n Enter a number: "); scanf("%d",&i); if(i%3==0) printf("%d is divisible by 3.\n",i); else printf("%d is not divisible by 3.\n",i); }
 * 1) include

do .. while statement
/*Program using the do .. while statement*/ void main { 	int choice; do { 		printf("\n\t\t\t TOURIST RESORT CENTRES"); printf("\n\n 1- NAIROBI NATIONAL PARK"); printf("\n\n 2-TSAVO"); printf("\n\n 3- AMBOSELI"); printf("\n\n 4- EXIT"); printf("\n\n Pls enter your selection [1 - 4]"); scanf("%d",&choice); switch(choice) { 		case 1: printf("\n Be ready to see the"); printf("\n KING of the Jungle"); break; case 2: printf("\n Beware of the "); printf("\n man-eaters"); break; case 3: printf("\n Be ready to see "); printf("\n the Jumbo"); break; case 4: printf("\n Good bye "); break; default: printf("you have landed in a bush "); printf("\n without animals"); break; } 	}while(choice!=4); }
 * 1) include

for ststement
//program to arrange a set of n floating point numbers in ascending order. void main { 	float x[50]; int i,j,n; float temp; printf("Type in the number of values to be arranged :-> "); scanf("%d",&n); for(i = 1; i<=n; i++) { 			printf("Type in no%ds value : ",i); scanf("%f",&x[i]); } 		for(i=1; i<=n; i++) { 			 for(j=i+1; j<=n;j++) if(x[j]