You are on page 1of 30

#include<stdio.h> #include<conio.

h> void main() { int x,y; char test[50] = {0}; clrscr(); printf("\nenter value\n"); gets(test); x = sscanf(test,"%d", &y); printf(" \nx is %d", x); printf("\n y is %d", y); getch(); }

#include<stdio.h> #include<conio.h> main() { int x; char y; char test[10]; printf("\nenter value\n"); gets(test); x = sscanf(test,"%c", &y); printf(" \nx is %d", x); getch(); }

#include<stdio.h> #include<conio.h> #define team 11 int main() { int scores[team]; int swap, temp, i; printf("enter the scores one by one"); for(i=0; i<team; i++) { printf("\nscore for batsman %d: ", i+1);

scanf("%d", &scores[i]); } printf("now doing the sorting...."); do { swap=0; for(i=0; i<10; i++) { if(scores[i]>scores[i+1]) { temp = scores[i+1]; scores[i+1] = scores[i]; scores[i] = temp; swap = 1; } } } while(swap != 0); printf("\nthe scores are:"); for (i=0; i<team; i++) { printf(" \n %d", scores[i]); } getch(); } #include<stdio.h> #include<conio.h> #define team 11 #define game 2 void main() { int player_count, game_count, topscore; int scores[game][team]; int totals[team]; clrscr(); /*storing values*/ for(game_count=1; game_count <= game; game_count++)

{ for(player_count=1; player_count<=team; player_count++) { printf("for game %d, player %d scored:", game_count, player_count); scanf("%d", &scores[game_count][player_count]); } } for(player_count=1; player_count <= team; player_count++) { totals[player_count]=0; } /*calculations*/ for(player_count=1; player_count<=team; player_count++) { for(game_count=1; game_count<=game; game_count++) { totals[player_count]=totals[player_count]+scores[game_count][player_co unt]; } } for(player_count=1; player_count<=team; player_count++) { printf("\ntotal score of batsman %d is: %d", player_count, totals[player_count]); } for(game_count=1; game_count<=game; game_count++) { for(player_count=1; player_count<=team; player_count++) { if(topscore>scores[game_count][player_count]) { printf("\nhighest score is: %d", topscore); printf("\ntop scoring batsman is: %d", player_count); printf("\nhe achieved this feat in game: %d", game_count); } } } getch(); } #include<stdio.h> #include<conio.h>

#define team 11 #define game 2 int { int int int main() player_count, game_count, topscore; scores[game][team]; totals[team];

/*storing values*/ for(game_count=1; game_count <= game; game_count++) { for(player_count=1; player_count<=team; player_count++) { printf("for game %d, player %d scored:", game_count, player_count); scanf("%d", &scores[game_count][player_count]); } } for(player_count=1; player_count <= team; player_count++) { totals[player_count]=0; } /*calculations*/ for(player_count=1; player_count<=team; player_count++) { for(game_count=1; game_count<=game; game_count++) { totals[player_count]=totals[player_count]+scores[game_count][player_co unt]; } } getch(); } /* combination nCr program */ #include<stdio.h> #include<conio.h> int main() { int x, y,i,fact_x=1,fact_y=1,ans,z,fact_z=1;

printf("Enter n and r for the format nCr"); scanf("%d%d", &x, &y); if(x<y) { printf("your input is invalid"); } else { z=(x-y); for(i=1; i<=x; i++) { fact_x=(fact_x*i); } for(i=1; i<=y; i++) { fact_y=fact_y*i; } if(z==0) { fact_z=fact_z+0; } else { for(i=1; i<=z; i++) { fact_z=fact_z*i; } } ans=fact_x/((fact_y)*(fact_z)); printf("ans = %d", ans); printf("\n%d, %d, %d", fact_x,fact_y,fact_z); } getch(); } /* combination nCr program */ #include<stdio.h> #include<conio.h> void main() { int x, y,i,fact_x=1,fact_y=1,ans,z,fact_z=1;

printf("Enter n and r for the format nCr"); scanf("%d%d", &x, &y); if(x<y) { printf("your input is invalid"); } else { z=(x-y); for(i=1; i<=x; i++) { fact_x=(fact_x*i); } for(i=1; i<=y; i++) { fact_y=fact_y*i; } if(z==0) { fact_z=fact_z+0; } else { for(i=1; i<=z; i++) { fact_z=fact_z*i; } } ans=fact_x/((fact_y)*(fact_z)); printf("ans = %d", ans); printf("\n%d, %d, %d", fact_x,fact_y,fact_z); } getch(); } #include<stdio.h> #include<conio.h> int squarenumber(const int array[]) { int i; for(i=0; i<3; i++) { array[i]=array[i]*array[i]; } }

main() { int number[3], i; printf("enter 3 numbers"); for(i=0; i<3; i++) { scanf("%d", &number[i]); } squarenumber(number); for(i=0; i<3; i++) { printf(" new elements = %d", number[i]); } getch(); } #include<stdio.h> #include<conio.h> #include<stdlib.h> void encrypt(char array[], int random) { int x=0; while(array[x] != '\0') { array[x]=+random; x++; } x=0; printf("\nthe encrypted message is\t %s", array); /*watch the decrypt fn for diffrncs*/ } void decrypt(char array[], int random) { int x=0; while(array[x] != '\0') { array[x]=-random; x++; } x=0; printf("\nthe decrypted message is\t");

while(array[x] != '\0') { printf("%c", array[x]); x++; } } main() { int selection, arbit; char message[10]; arbit=(rand() % 4) + 1; while(selection != 4) { printf("\n\nEnter your choice"); printf("\n1\tEncrypt message"); printf("\n2\tDecrypt message"); printf("\n3\tGenerate new key"); printf("\n4\tQuit\n\n"); scanf("%d", &selection); switch(selection) { case 1: printf("Enter your clear text\n"); scanf("%s", message); encrypt(message, arbit); break; case 2: printf("Enter your cipher message\n"); scanf("%s", message); decrypt(message, arbit); break; case 3: arbit=(rand() % 4) +1; printf("New key generated"); break; case 4: abort(); } } getch()}

#include<stdio.h> #include<conio.h> main() { int array[5]; int i; FILE *output_file; for(i=0; i<5; i++) { scanf("%d", &array[i]); } output_file = fopen("first_file", "w"); fwrite(array, sizeof(array), 5, output_file); fclose(output_file); getch(); } #include<stdio.h> #include<conio.h> main() { int i; int array[5]; FILE *output_file; output_file=fopen("first_file", "r"); fread(array, sizeof(array), 5, output_file); for(i=0; i<5; i++) { printf("\n%d\n", array[i]); } getch(); } f#include<stdio.h> #include<conio.h> void main() { int a,b,c,d; clrscr(); printf("enter 4 nos"); scanf("%d%d%d%d", &a, &b,&c, &d);

if(a>b && a>c && a>d) printf("greatest = %d", a); else if( b>c && b>d) printf("greatest = %d", b); else if(c>d) printf("greatest = %d", c); else printf("greatest = %d", d); if(a<b && a<c && a<d) printf("\nsmallest = %d", a); else if(b<c && b<d) printf("\nsmallest = %d", b); else if(c<d) printf("\nsmallest = %d", c); else printf("\nsmallest = %d", d); getch(); } #include<stdio.h> #include<conio.h> #include<ctype.h> main() { char ch; printf("enter datatype"); scanf("%c", &ch); if (isdigit(ch)) printf("digit"); else if (isalpha(ch)) printf("alphabet"); else printf("special character"); getch(); } #include<stdio.h> #include<conio.h> #include<stdlib.h> main() { int i=0, random=4; char message[20];

printf("enter message"); scanf("%s", message); while(message[i] != '\0') { message[i]=+random; i++; } printf("the new message is %s", message); getch(); } #include <stdio.h> #include<conio.h> main() { char *myString = "Mike"; int x; printf("\nThe pointer variable's value is: %p\n", *myString); printf("\nThe pointer variable points to: %s\n", myString); printf("\nThe memory locations for each character are: \n\n"); //access & print each memory address in hexadecimal format for ( x = 0; x < 5; x++ ) printf("%p\n", myString[x]); getch(); } //end main #include<stdio.h> #include<conio.h> int squarenumber(int array[]) { int i; for(i=0; i<3; i++) { array[i]=array[i]*array[i]; } } int main() { int number[3]; int i; printf("enter the numbers"); for(i=0; i<3; i++) { scanf("%d", &number[i]); }

squarenumber(number); printf("\nthe squares are:"); for(i=0; i<3; i++) { printf("\n %d", number[i]); } getch(); } #include<stdio.h> #include<conio.h> int squarenumber(demo[]) { int i,x,y; int demo[x]; for(i=0; i<x; i++) { array[x]=array[x]*array[x]; } } main() { int number[3]; int i; printf("enter the numbers"); for(i=0; i<3, i++) { scanf("%d", &number[i]); } squarenumber(number); printf("\nthe squares are:"); for(i=0; i<3; i++) { printf("\n %d", number[i]); } getch(); } #include<stdio.h> void main() int a,b,c; printf(" enter nos");

scanf(" %d,%d", &a, &b); c = a+b; printf(" sum is = %d", &c); #include<stdio.h> #inlude<conio.h> void main() { clrscr() printf("Hello World"); getch(); } /* combination nCr program */ #include<stdio.h> #include<conio.h> void main() { int x, y,i,fact_x=1,fact_y=1,ans,z,fact_z=1; clrscr(); printf("Enter n and r for the format nCr"); scanf("%d%d", &x, &y); if(x<y) { printf("your input is invalid"); } else { z=(x-y); for(i=1; i<=x; i++) { fact_x=(fact_x*i); } for(i=1; i<=y; i++) { fact_y=fact_y*i; } for(i=1; i<=z; i++) { fact_z=fact_z*i; } ans=fact_x/((fact_y)*(fact_z)); printf("ans = %d", ans);

printf("\n %d, %d, %d", fact_x, fact_y, fact_z); } getch(); } #include<stdio.h> #include<conio.h> #include<ctype.h> main() char ch= '\0'; printf("enter datatype"); scanf("%c", &ch); if (isdigit(ch)==0) printf("digit"); else if (isalpha(ch)==0) printf("alphabet"); else printf("special character"); getch(); } #include<stdio.h> #include<conio.h> int namelength(char array[]) { int i=0; while(array[i] != 0) /*also '/0' can be used to denote null*/ i++; return i; } main() { char name[20]; printf("\nenter a name\n"); scanf("%s", name); printf("\nnumber of characters= %d", namelength(name)); printf("\nmemory address of first element of array = %p", name); printf("\nmemory address of first element same as above = %p", &name[0]); /*mark the use of variable*/

getch(); } #include<stdio.h> #include<conio.h> int passbyref(int *ptrx) { *ptrx=+5; return *ptrx; } main() { int x; int *ptrx; ptrx=&x; printf("\nenter x"); scanf("%d", &x); printf("\nthe value is x= %d", x); x = passbyref(&x); printf("\nthe value of x printf("\nthe value of x printf("\nmemory address printf("\nmemory address getch(); } #include<stdio.h> demopassbyvalue(int x) { x=x+5; return x; } void main() { int x; printf("enter x"); scanf("%d", &x); printf("the value of x is= %d", x); is now=%d", *ptrx); is now by not using pointer= %d", x); stored in pointer=%p", ptrx); of x, same as above= %p", &x);

demopassbyvalue(x); printf("now value of x is= %d", x);} #include<stdio.h> #include<conio.h> #include<string.h> typedef struct sample { int id; char name[10]; } s; void prostr(s *); // prototype declaration. int main() { s s1 = {0,0}; s *ptrs; ptrs=&s1; prostr(ptrs); printf("\nid = %d", ptrs->id); printf("\nname= %s", ptrs->name); getch(); } void prostr(s *s1) { s1->id=123; strcpy(s1->name, "Monideep"); }

#include<stdio.h> #include<conio.h> #include<string.h> typedef struct sample { int id; char name[10]; } sam;

void prostr(sam); int main() { sam s1={0,"1"};//string literals used as it is a char array prostr(s1); printf("\n%d", s1.id); printf("\n%s", s1.name); getch(); } void prostr(sam s) { s.id=420; strcpy(s.name, "Monideep"); } #include<stdio.h> #include<conio.h> #include<string.h> typedef struct sample { int id; char *name; } sam; void prostr(sam s) { s.id=123; strcpy(s.name, "Monideep"); } main() { sam s1 = {0,0}; prostr(s1); printf("%d", s1.id); printf("%s", s1.name); getch(); } #include<stdio.h> #include<conio.h.> main() { int i; char *str1[5]={0}; str1[0]="Apple"; str1[1]="Ball"; str1[2]="Cat";

str1[3]="Dog"; str1[4]="Egg"; printf("the pointer elements are:\n"); for(i=0; i<5; i++) { printf("\n%s\n", str1[i]); } printf("\n%s\n", str1); getch(); } //this program doesnot work.. but correct as per mike vine. #include<stdio.h> #include<conio.h> int main() { char *string = "Mike"; int x; printf("\nthe pointer points to %p", *string); printf("\nstring is %s", string); printf("\nthe memory locations are"); for(x=0; x<5; x++) { printf("\n%p\n", string[x]); } for(x=0; x<5; x++) { printf("\n%c\n", string[x]);//also %s can be used in place of %c for same result. then [x] must be omitted } getch(); } #include<stdio.h> #include<conio.h> #include<ctype.h> int main() { char random_result='\0'; char random_guess='\0'; /*mark this line*/

random_result=(rand() % 10) + 1; printf("enter a number from 1 to 10"); scanf("%c", &random_guess); if (isalpha(random_guess)) { printf("\nERROR:::: you have entered an alphabet"); } else if (isdigit(random_guess)) {

if( random_result==random_guess) { printf("\nyou have guessed it right."); printf("\nthe number is %c", random_result); } else if ( random_result != random_guess ) { printf("\n you have guessed it wrong"); printf("\n the number is %c", random_result); } }else { printf("\nERROR:::: you have entered a special character"); } getch(); } #include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char random_result='\0'; char random_guess='\0';

printf("\n\nEnter a number from 1 to 10\n");

scanf("%c", &random_guess); if (isalpha(random_guess)) { printf("\nERROR:::: you have entered an alphabet"); } else if (isdigit(random_guess)) { random_result=(rand() % 10) + 1; if( random_result == random_guess) { printf("\nyou have guessed it right."); } else { printf("\n you have guessed it wrong"); printf("\n the number is %c", random_guess); } } else { printf("\nERROR:::: you have entered a special character"); } getch(); } #include<stdio.h> #include<conio.h> void main() { int guess=0, result=0; clrscr(); printf("\nenter number b/w 1 and 10\n"); scanf("%d", &guess); srand(time()); result = (rand() % 10) + 1;

if(guess == result) { printf("\nyou guessed it right"); } #include<stdio.h> #include<conio.h> main() { int guess=0, result=0; printf("\nenter number b/w 1 and 10\n"); scanf("%d", &guess); result = (rand() % 10) + 1; if(guess == result) { printf("\nyou guessed it right"); } else { printf("\nyou guessed it wrong."); printf("\nthe number is %d", result); } getch(); } /*sample program by rob miles*/ #include<stdio.h> #include<conio.h> #define #define #define #define #define #define #define #define #define max_height 3.0 max_width 5.0 min_height 0.75 min_width 0.5 cost_to_make 2.00 wood_cost 2.00 glass_cost 3.00 markup_factor 2.25 max_windows 10

int main() { float width, height, window_cost, window_sell, house_cost, house_sell; int no_of_windows, window_count;

printf("double glazing house calculator\n"); do { printf("give the number of windows: "); scanf("%d", &no_of_windows); } while ((no_of_windows <= 0)||(no_of_windows > max_windows)); house_cost = 0.0; house_sell = 0.0; for(window_count = 1; window_count <= no_of_windows; window_count++) { printf("window %d details.\n", window_count); do { printf("enter the width:"); scanf("%f", &width); } while ((width < min_width)||(width > max_width)); do { printf("enter the height:"); scanf("%f", &height); } while ((height < min_height)||(height > max_height)); window_cost = wood_cost*2*(width+height); window_sell = window_cost*markup_factor; printf("window %d costs %.2f, sells for %.2f \n\n", window_count, window_cost, window_sell); house_cost += window_cost; house_sell += window_sell; } printf("\n total cost to do all %d windows: %.2f \n", no_of_windows, house_cost); printf("\n total sale price for all %d windows: %.2f \n", no_of_windows, house_sell);

getch(); }

#include <stdio.h> #include<conio.h> #define TEAMLENGTH 11 #define SEASONLENGTH 2 int main () { /* define our main storage array */ int scores [SEASONLENGTH] [TEAMLENGTH] ; /* define our totals array */ int totals [TEAMLENGTH] ; /* define our counters */ int game_count, player_count ; /* topscore holds the highest score so far */ int topscore ; /* first read our data */ for ( game_count = 0 ; game_count < SEASONLENGTH ; game_count++) { for ( player_count = 0 ; player_count < TEAMLENGTH ; player_count++) { printf ( "Enter score player %d game %d : ", game_count+1, player_count+1 ) ; scanf ( "%d", &scores [game_count][player_count] ) ; } } /* now zero our totals array */ for ( player_count = 0 ; player_count < TEAMLENGTH ; player_count++) { totals [player_count] = 0 ; } /* Now set our initial to p score */ topscore = 0 ; /* Now work out our results */ for ( game_count = 0 ; game_count < SEASONLENGTH ; game_count++) { for ( player_count = 0 ; player_count < TEAMLENGTH ; player_count++) { /* increase our totals */ totals [player_count] += scores [game_count] [player_count] ; /* decide if we have a highscore */

/* bigger than our current one */ if ( scores [game_count] [player_count] > topscore) { topscore = scores [game_count][player_count] ; } } } /* Now print out the results */ printf ( "\n\nScore totals:\n\n" ) ; for ( player_count = 0 ; player_count < TEAMLENGTH ; player_count++ ) { printf ( "Total for player %d : %d \n", player_count+1, totals [player_count] ) ; } printf ( "\n\nHighest score was : %d\n\n", topscore ) ; getch(); } /*sample program by rob miles using functions*/ #include<stdio.h> #include<conio.h> #define #define #define #define #define #define #define #define #define max_height 3.0 max_width 5.0 min_height 0.75 min_width 0.5 cost_to_make 2.00 wood_cost 2.00 glass_cost 3.00 markup_factor 2.25 max_windows 10

/*defining function*/ float range_fn (float min, float max) { float temp; do { printf("\n enter the value"); scanf("%f", &temp); } while ((temp<min)||(temp>max)); return temp; } int main() {

float width, height, window_cost, window_sell, house_cost, house_sell; int no_of_windows, window_count;

printf("DOUBLE GLAZING PROGRAM\n"); do { printf("give the number of windows: "); scanf("%d", &no_of_windows); } while ((no_of_windows <= 0)||(no_of_windows > max_windows)); house_cost = 0.0; house_sell = 0.0; for(window_count = 1; window_count <= no_of_windows; window_count++) { printf("window %d details.\n", window_count); printf("WIDTH:"); width = range_fn (min_width, max_width); printf("HEIGHT:"); height = range_fn (min_height, max_height);

window_cost = wood_cost*2*(width+height); window_sell = window_cost*markup_factor; printf("window %d costs %.2f, sells for %.2f \n\n", window_count, window_cost, window_sell); house_cost += window_cost; house_sell += window_sell; } printf("\n total cost to do all %d windows: %.2f \n", no_of_windows, house_cost); printf("\n total sale price for all %d windows: %.2f \n", no_of_windows, house_sell); getch(); } #include<stdio.h> #include<conio.h>

struct sample { int x,y,z; }; void main() { struct sample inc1; //couldn't initialise x,y,z as said by mike vine. inc1.x=10; inc1.y=5; inc1.z=(inc1.x + inc1.y); printf("o/p is = %d", inc1.z); getch(); }

#include<stdio.h> #include<conio.h> #include<string.h> typedef struct sample { char *name; int prices; } sam; void main() { sam inc1[1]; int x; strcpy(inc1[0].name, "Monideep"); inc1[0].prices = 20; // only one element.

for (x=0; x<1; x++) { printf("\n Elements = %s \t %d", inc1[x].name, inc1[x].prices); } getch(); } #include<stdio.h>

#include<conio.h> #include<string.h> typedef struct sample //typedef used here { char *fname; //strings used here. char *lname; int id; } sam; //modification for typedef void main() { sam inc1; //typedef modification shorthand. clrscr(); strcpy(inc1.fname, "Monideep"); strcpy(inc1.lname, "Bora"); inc1.id = 420; printf("\n First name: %s", inc1.fname); printf("\n Last name: %s", inc1.lname); printf("\n ID: %d", inc1.id); getch(); }

You might also like