C Programs
1
/* PR1 */ main() { clrscr();
/* Clears the screen contents */
printf("We are three brothers: \n Shoni \n Cooki \n Mani"); printf("\n There are two kinds of break Softbreak \n and \r Hardbreak"); printf("\n Provide following \n name: \t class: \t roll no:"); printf("\a"); sleep(1);
/* Stop the execution of program for given no of seconds */
printf("\n Did you hear a beep?"); sleep(5); } /* PR2 */ main() { clrscr();
/* Clears the screen contents */
printf("\n\t My name is tanveer and i am %d years old: ",45); printf("\n\t Temperature in the core of the sun is %ld degree celcius:",41000000); printf("\n\t The value of the PI is %.4f ",3.1415); printf("\n\t The character %c is pronounced as \'SEE\': ",'c'); printf("\n\t The character %c is pronounced as \'%s\' : ",'T',"TEE"); printf("\n\t The data type LONG can store uupto %e unsigned value: ",4294967295); sleep(5);
/* Stop the execution of program for given no of seconds */
}
Prepared by Tanveer Ahmed
2/11/09
1
C Programs
2
/* PR3 */ main() { int x = 12, y = 6, z = 2; clrscr();
/* Clears the screen contents */
printf("\n\t X = %d Y = %d Z = %d",x,y,z); printf("\n\n\t The Division of X and Y is %d",x/y); printf("\n\t The Product of X and Y is %d",x*y); printf("\n\t The Remainder of X divided by Z is %d",x%z); printf("\n\t The Addition of X and Y is %d",x+y); printf("\n\t The Subtraction of X and Y is %d",x-y); printf("\n\t An expression using more than one operators:"); printf("\n\t X * Y + Z = %d",x*y+z); printf("\n\t Use of Parenthesis to change the priority of operators"); printf("\n\t X * (Y + Z) = %d",x*(y+z)); sleep(5); /* Stop the execution of program for given no of seconds */ }
Prepared by Tanveer Ahmed
2/11/09
2
C Programs
3
/* PR4 */ main() { int x, y; clrscr();
/* Clears the screen contents */
printf("\n\t Enter two integers and see their relationship\n\t"); scanf("%d %d",&x,&y); if(x == y) printf("\n\t %d is equal to %d",x,y); if(x != y) printf("\n\t %d is not equal to %d",x,y); if(x < y) printf("\n\t %d is less than %d",x,y); if(x > y) printf("\n\t %d is greater than %d",x,y); if(x <= y) printf("\n\t %d is less than or equal to %d",x,y); if(x >= y) printf("\n\t %d is greater than or equal to %d",x,y); sleep(5); /* Stop the execution of program for given no of seconds */ }
Prepared by Tanveer Ahmed
2/11/09
3
C Programs
4
/* PR5 */ main() { char x, y; clrscr();
/* Clears the screen contents */
printf("\n\t Truth table for Logical AND, OR and NOT "); printf("\n\t Enter two expression values for AND (T,F)\n\t"); scanf("%c,%c",&x,&y); x = toupper(x); y = toupper(y); printf("\n\t AND \t OR \t NOT "); if (x == 'T' && y == 'T') printf("\n\t TRUE"); if (x == 'T' && y == 'F') printf("\n\t FALSE"); if (x == 'F' && y == 'T') printf("\n\t FALSE"); if(x == 'F' && y == 'F') printf("\n\t FALSE"); getch(); /* Stop the execution of program for given no of seconds */ if (x == 'T' && y == 'T') printf("\t TRUE"); if (x == 'T' && y == 'F') printf("\t TRUE"); if (x == 'F' && y == 'T') printf("\t TRUE"); if(x == 'F' && y == 'F') printf("\t FALSE"); getch(); if (x == 'T') printf("\t FALSE"); if (x == 'F') printf("\t TRUE"); getch(); } /* PR6 */ main() { Prepared by Tanveer Ahmed
2/11/09
4
C Programs
5
int a = 2; clrscr();
/* Clears the screen contents */
printf("\n\t The value of memory variable a is %d \n\t",a); printf(" and the address of memory variable a is %u ",&a); getch(); /* It is used to read a character */ } /* PR7 */ main() { int a; clrscr();
/* Clears the screen contents */
a = 2; printf("\n\t The value of a is
%d",a);
++a; printf("\n\t The Value of ++a is %d",a); a++; printf("\n\t The Value of a++ is %d",a); a--; printf("\n\t The Value of a++ is %d",a); --a; printf("\n\t The Value of a++ is %d",a); getch(); /* It is used to read a character */ }
Prepared by Tanveer Ahmed
2/11/09
5
C Programs
6
/* PR8 */ main() { int a; clrscr();
/* Clears the screen contents */
a = 2; printf("\n\t The value of a is
%d",a);
a = a + 7; printf("\n\t The Value of a=a+7 is %d",a); a+=3; printf("\n\t The Value of a+= 3 is %d",a); a-=6; printf("\n\t The Value of a-= 6 is %d",a); a*=4; printf("\n\t The Value of a*= 4 is %d",a); a/=8; printf("\n\t The Value of a/= 8 is %d",a); getch(); /* It is used to read a character */ } /* PR9 */ main() { int a; clrscr();
/* Clears the screen contents */
a = 16; printf("\n\t The integer value of a is
%d",a);
printf("\n\t The hexa decimal value of a is %x",a); printf("\n\t The octal value of a is %o",a); getch(); /* It is used to read a character */ }
/* PR19 */ main() { Prepared by Tanveer Ahmed
2/11/09
6
C Programs
7
int a; clrscr();
/* Clears the screen contents */
printf("\n\t Enter any integer: "); scanf("%d",&a); printf("\n\t Value read in by scanf is %d:",a); getch(); } /* PR11 */ main() { int a = 2; clrscr();
/* Clears the screen contents */
if(a > 0) { printf("\n\t If statement can also execute more than one statements"); printf("\n\t First Statement"); printf("\n\t Second statement"); } getch(); }
Prepared by Tanveer Ahmed
2/11/09
7
C Programs
8
/* PR12 */ main() { int a = 2; clrscr();
/* Clears the screen contents */
printf("\n\t Use of if-else with multiple statements:\n\n"); if(a > 0) { printf("\n\t Value is greater than zero :"); printf("\n\t And therefore is positive"); } else { printf("\n\t Value is less than zero :"); printf("\n\t And therefore is negative"); } getch(); }
Prepared by Tanveer Ahmed
2/11/09
8
C Programs
9
/* PR13 */ main() { char a; clrscr();
/* Clears the screen contents */
printf("\n\t Enter a lower case alphabet fro a-c:\n\n"); scanf("%c",&a); if(a == 'a') printf("\n\t An apple a day keeps the doctor away:"); else { if(a =='b') printf("\n\t Before you let yourself go, be sure that you can get your self back"); else { if(a == 'c') printf("\n\t Children are gifts of God :"); else printf("\n\t Illegal character, enter character from a-c"); } } getch(); }
Prepared by Tanveer Ahmed
2/11/09
9
C Programs
10
/* PR14*/ #include main() { char a; clrscr();
/* Clears the screen contents */
printf("\n\t Enter any negative or positive number :"); scanf("%c",&a); if(isdigit(a)) { if(a < 0) printf("\n\t You entered a negative number:"); else { printf("\n\t You Entered a positive number:"); printf("\n\t Or it may be zero:"); } } else printf("\n\t Please enter a numeric value:"); getch(); }
Prepared by Tanveer Ahmed
2/11/09
10
C Programs
11
/* PR15 */ main() { int a; clrscr();
/* Clears the screen contents */
printf("\n\t Enter an integer froum 1 to 4 :"); scanf("%d",&a); switch(a) { case 1: printf("\n one"); break; case 2: printf("\n two"); break; case 3: printf("\n three"); break; case 4: printf("\n four"); break; default: printf("\n\t Please enter from 1 to 4:"); } getch(); }
Prepared by Tanveer Ahmed
2/11/09
11
C Programs
12
/* PR16 for */ main() { int i=0; clrscr();
/*
Clears the screen contents
*/
printf("Simple for loop\n"); for(i=0 ; i<5; i++) printf(" tanveer "); getch(); } /* PR17 for */ main() { int i=0; clrscr();
/*
Clears the screen contents
*/
printf("Simple for loop\n"); for( ; i<5; i++) /*
initialize expression
*/
printf(" tanveer "); getch(); } /* PR18 for */ main() { int i=0; clrscr();
/* Clears the screen contents
*/
printf("Simple for loop\n"); for(i=0; i<5; )
/*
update expression
*/
{ printf(" Ahmed "); i++;
/* update expression
*/
} getch(); } /* PR19 for */ main() { Prepared by Tanveer Ahmed
2/11/09
12
C Programs
13
int i=0; clrscr();
/* Clears the screen contents
*/
printf("Simple for loop\n"); for( ; i<5; )
/*
initialize & update expression
*/
{ printf(" Ahmed "); i++;
/* update expression
*/
} getch(); } /* PR20 while */ main() { int i=0; clrscr();
/* Clears the screen contents */
while( i<5 ) { printf(" tanveer "); i++;
/* update expression
*/
} getch(); }
Prepared by Tanveer Ahmed
2/11/09
13
C Programs
14
/* PR21 while */ main() { int i; clrscr();
/* Clears the screen contents */
i = 0; /* initialize expression */ while( i<5 ) { printf(" tanveer "); i++;
/* update expression
*/
} getch(); } /* PR22 while */ main() { int i; clrscr();
/* Clears the screen contents */
i = 5; /* initialize expression */ while( i>0 ) { printf(" %d",i); i--;
/* update expression
*/
} getch(); }
Prepared by Tanveer Ahmed
2/11/09
14
C Programs
15
/* PR23 while */ main() { int i,j; clrscr();
/* Clears the screen contents */
i = 0; /* initialize expression */ while( i<3 ) { printf("\n %d",i); for(j=0;j<3;j++) printf(" tanveer"); i++;
/* update expression
*/
} getch(); } /* PR24 while */ main() { int i,j; clrscr();
/* Clears the screen contents */
i = 0; /* initialize expression */ while( i<21 ) { printf("\n %d\t",i); for(j=0;j
/* update expression
*/
} getch(); }
Prepared by Tanveer Ahmed
2/11/09
15
C Programs
16
/* PR25 do while */ main() { int i=0; clrscr();
/* Clears the screen contents */
do { printf(" tanveer "); i--;
/* update expression
*/
} while( i>0 ); getch(); }
/* PR26 */ main() { int car[]={79,86,89,90,98}; /* Array declaration */ clrscr();
/* Clears the screen contents */
printf("\n\t %d",car[0]); printf("\n\t %d",car[1]); printf("\n\t %d",car[2]); printf("\n\t %d",car[3]); printf("\n\t %d",car[4]); getch(); }
Prepared by Tanveer Ahmed
2/11/09
16
C Programs
17
/* PR27 array */ main() { int car[4];
/* Array declaration */
clrscr();
/* Clears the screen contents */
car[0] = 79;
/* Element feeding to an array */
car[1] = 86; car[2] = 89; car[3] = 90; car[4] = 98; printf("\n\t %d",car[0]); /* element reading from an array */ printf("\n\t %d",car[1]); printf("\n\t %d",car[2]); printf("\n\t %d",car[3]); printf("\n\t %d",car[4]); getch(); } /* PR28 array */ main() { int car[]={79,86,89,90,98}; /* Array declaration */ clrscr();
/* Clears the screen contents */
printf("\n\t %d",car[0]);
/* Element reading from an array */
printf("\n\t %d",car[1]); printf("\n\t %d",car[2]); printf("\n\t %d",car[3]); printf("\n\t %d",car[4]); getch(); }
Prepared by Tanveer Ahmed
2/11/09
17
C Programs
18
/* PR29 array */ main() { int car[4],i; /* Array declaration */ clrscr();
/* Clears the screen contents
*/
printf("\n\t Enter array elements please: \n"); for(i=0; i<5; i++)
/* Element reading from an array */
scanf("%d",&car[i]); for(i=0; i<5; i++) printf("\n\t %d",car[i]);
/* Element reading from an array */
getch(); } /* PR30 array */ main() { int car[4],i; /* Array declaration */ clrscr();
/* Clears the screen contents
*/
printf("\n\t Enter array elements please: \n"); for(i=0; i<5; i++)
/* Element reading from an array */
scanf("%d",&car[i]); for(i=4; i>=0; i--) printf("\n\t %d",car[i]);
/* Element reading from an array */
getch(); }
Prepared by Tanveer Ahmed
2/11/09
18
C Programs
19
/* PR31 array */ main() { int rokalum[3][3] = { {2, 4, 6
},
{8, 10, 12 },
/* Array declaration */
{14, 16, 18}, }; clrscr(); printf("\n COL1"); printf("\t %d",rokalum[0][0]);
/* Element reading from an array */
printf(" %d",rokalum[0][1]); printf(" %d",rokalum[0][2]); printf("\n COL2"); printf("\t %d",rokalum[1][0]); printf(" %d",rokalum[1][1]); printf(" %d",rokalum[1][2]); printf("\n COL3"); printf("\t %d",rokalum[2][0]); printf(" %d",rokalum[2][1]); printf(" %d",rokalum[2][2]); getch(); }
Prepared by Tanveer Ahmed
2/11/09
19
C Programs
20
/* PR32 array */ main() { int rokalum[3][3],i,j; clrscr(); printf("\n Enter two dimentional array elements: "); for(i=0; i<3; i++) { for(j=0; j<3; j++) scanf("%d",&rokalum[i][j]); } for(i=0; i<3; i++) { for(j=0; j<3; j++) printf("\t %d",rokalum[i][j]);
/* Element reading from an array */
printf("\n"); } getch(); }
Prepared by Tanveer Ahmed
2/11/09
20
C Programs
21
/* PR33 array */ main() { int rokalum[2][3][2] = { { {1, 3 }, {5, 7 }, {9, 11}, }, { {2 , 4 }, {6 , 8 }, {10, 12}, }, }; clrscr(); printf("\n Reading data from multi-dimentional array: "); printf("\n %d",rokalum[0][0][0]); printf(" %d",rokalum[0][0][1]);
/* Element reading from an array */
printf("\n %d",rokalum[0][1][0]); printf(" %d",rokalum[0][1][1]); printf("\n %d",rokalum[0][2][0]); printf(" %d",rokalum[0][2][1]); printf("\n\n %d",rokalum[1][0][0]); printf(" %d",rokalum[1][0][1]); printf("\n %d",rokalum[1][1][0]); printf(" %d",rokalum[1][1][1]); printf("\n %d",rokalum[1][2][0]); printf(" %d",rokalum[1][2][1]); getch(); }
Prepared by Tanveer Ahmed
2/11/09
21
C Programs
22
/* PR34 string */ main() { int rokalum[2][3][2],i,j,k; clrscr(); printf("\n Feeding elements to multidimentional array: "); for(i=0; i<2; i++) for(j=0; j<3; j++) for(k=0; k<2; k++) scanf("%d",&rokalum[i][j][k]); printf("\n "); printf("\n Reading data from multi-dimentional array: \n"); for(i=0; i<2; i++) { for(j=0; j<3; j++) { for(k=0; k<2; k++) { printf(" %d",rokalum[i][j][k]); } printf("\n"); } printf("\n"); } getch(); }
Prepared by Tanveer Ahmed
2/11/09
22
C Programs
23
/* PR35 string */ main() { char name[20]; clrscr(); printf("\n Enter any string MAX LENGTH = 20 characters"); gets(name); puts(name); getch(); } /* PR36 string */ main() { int c; char name1[20], name2[20]; clrscr(); printf("\n Enter first string MAX LENGTH = 20 characters: "); gets(name1); printf("\n Enter second string MAX LENGTH = 20 characters: "); gets(name2); c = strcmp(name1, name2); if(c == 0) printf("\n Both strings are same: "); else printf("\n Strings are Different: and the difference is %d",c); getch(); }
Prepared by Tanveer Ahmed
2/11/09
23
C Programs
24
/* PR37 string */ #include<string.h> main() { char name[10]; clrscr(); printf("\n Enter your name: "); gets(name); strcat(name, " hello"); printf("\n %s",name); getch(); } /* PR38 string */ main() { int i; char name[30]; clrscr(); printf("\n Enter your name: "); gets(name); i = strlen(name); printf("\n %s is %d characters long",name,i); getch(); }
Prepared by Tanveer Ahmed
2/11/09
24
C Programs
25
/* PR39 function */ main() { stars(); } stars() { printf("\*******************"); } stars(); /* function declaration */
Prepared by Tanveer Ahmed
2/11/09
25
C Programs
26
/* PR40 */ main() { stars(); } stars() { printf("\*******************"); } stars(); main() { char name[30]; clrscr(); printf("\n Enter your name: "); gets(name); stars(); printf("\n %s\n",name); stars(); getch(); } stars(void) { int i; for(i=0; i<=21; i++) printf("\*"); }
Prepared by Tanveer Ahmed
2/11/09
26
C Programs
27
/* PR41 */ add(); main() { int a,b,sum; clrscr(); printf("\n Enter two integers: "); scanf("%d %d",&a,&b); sum = add(a,b); printf("\n The addition of given two integers is %d :",sum); getch(); } add(a,b) { int sum; sum = a + b; return(sum); }
Prepared by Tanveer Ahmed
2/11/09
27
C Programs
28
/* PR42 */ float percentage(); main() { float per; int tmarks,omarks; clrscr(); printf("\n Enter your obtained and total marks respectively: "); scanf("%d %d",&omarks,&tmarks); per = percentage(omarks,tmarks); printf("\n The percentage is %f :",per); getch(); } float percentage(int omarks,int tmarks) { float per; per = omarks*100.0/tmarks; return(per); } /* PR43 */ main() { int i=3; int *j; j = &i; clrscr(); printf("\n Address of variable i is %u : ",&i); printf("\n Address of variable i is %u :",j); printf("\n Address of variable j is %u :",&j); printf("\n Value of variable j is %u :",j); printf("\n Value of variable i is %d :",i); printf("\n Value of variable i is %d :",*(&i)); printf("\n Value of variable i is %d :",*j); getch(); } /* PR44 */ main() Prepared by Tanveer Ahmed
2/11/09
28
C Programs
29
{ int a=3, b=6, c; clrscr(); c = add(&a, &b); printf("\n Value of a is %d : ",a); printf("\n Value of b is %d : ",b); printf("\n Addition of a and b is %d : ",c); getch(); } add(int *i, int *j) { int c; c = *i + *j; return(c); } /* Doll */ main() { char far *scr; int i; scr=0xb0008000; while(1) { for(i=0;i<=3999;i=i+2) { if(*(scr+i)>='A' && *(scr+i)<='Z') *(scr+i)=*(scr+i)+32; else { if(*(scr+i)<='a' && *(scr+i)<='z') *(scr+i)=*(scr+i)+32; } } } }
Prepared by Tanveer Ahmed
2/11/09
29
C Programs
30
/* Percent */ main() { int a, b; float c; scanf(" %d %d",&a, &b); c = a*100.0/b; printf("\n result is %f :",c); getch(); }
/* System */ #include<stdlib.h> #include<dos.h> #include main() { char pth[80]; int a; clrscr(); system("dir *.exe >> log.txt"); getcwd(pth,66); printf("%s",pth); getch(); }
Prepared by Tanveer Ahmed
2/11/09
30
C Programs
31
JAVA Section /* Classex */ class box { double width; double height; double depth; } class classex { public static void main(String args[]) { box mb = new box(); double vol; mb.width = 10; mb.height = 12; mb.depth = 30;
}
vol = mb.width * mb.height * mb.depth; System.out.println("Volume is vol: " + vol);
}
/* Expconvert */ class expconvert { public static void main(String args[]) { byte b; int i = 257; double d = 323.567; System.out.println("Conversion of INT to BYTE "); b = (byte) i; System.out.println("i and b" +"\t"+ i +" "+ b); System.out.println("Conversion of DOUBLE to INT "); i = (int) d; System.out.println("d and i" +"\t"+ d +" "+ i); System.out.println("Conversion of DOUBLE to BYTE "); b = (byte) d; System.out.println("d and b" +"\t"+ d +" "+ b); }
}
/* Impconvert */ Prepared by Tanveer Ahmed
2/11/09
31
C Programs
32
class impconvert { public static void main(String args[]) { byte b = 42; char c = 'c'; short s = 1024; int i = 5000; float f = 5.67f; double d = 0.12345; double result = (f * b) + (i / c) - (d * s);
}
System.out.println((f * b)+" + "+(i / c) +" - "+ (d * s)); System.out.println("Result is =" + result);
} /* mthd */ class box { double width; double height; double depth;
}
void vol() { System.out.println(width*height*depth); }
class mthd { public static void main(String args[]) { box mb = new box(); mb.width = 10; mb.height = 12; mb.depth = 30; }
mb.vol();
}
/* mymath */ Prepared by Tanveer Ahmed
2/11/09
32
C Programs
33
class mymath { public static void main(String args[]) { int a = 1 + 1; int b = a * 3; int c = b / 4; int d = c - a; int e = -d; System.out.println("a is :" + a); System.out.println("b is :" + b); System.out.println("c is :" + c); System.out.println("d is :" + d); System.out.println("e is :" + e); }
}
/* mymod */ class mymod { public static void main(String args[]) { int x = 42; double y = 42.3; System.out.println("x System.out.println("x System.out.println("y System.out.println("x }
is integer with value :" +x); mod 10 is :" +x % 10); is double with value :" +y); mod y is :" +x % y);
}
/* nestedif */ class nestedif { public static void main(String args[]) { String name = "tanveer"; if(name.length() == 7) { if(name == "tanveer") System.out.println("tanveer"); } else System.out.println("Unknown word"); } }
/* obj */ Prepared by Tanveer Ahmed
2/11/09
33
C Programs
34
class box { double width; double height; double depth; } class obj { public static void main(String args[]) { double vol; box mb1 = new box(); box mb2 = new box(); mb1.width = 10; mb1.height = 12; mb1.depth = 30; vol = mb1.width * mb1.height * mb1.depth; System.out.println("volume of first object is :" + vol); mb2.width = 20; mb2.height = 22; mb2.depth = 60; vol = mb2.width * mb2.height * mb2.depth; System.out.println("volume of second object is :" + vol); }
}
/* switchex */ class switchex { public static void main(String args[]) { int a = 10; switch(a) { case 1: System.out.println("Hello "); break; case 5: System.out.println("Hey "); break; case 9: System.out.println("Hi "); break; default: System.out.println("Why default "); break; } } }
Prepared by Tanveer Ahmed
2/11/09
34