297

C,C++ & Java

Embed Size (px)

Citation preview

C Language1. C Hello world program C hello world program: - code to print hello world. This program prints hello world, printf function is used to display text on screen, '\n' places cursor on the beginning of next line. This may be your first c code while learning programming. #include int main() { printf("Hello world\n"); return 0; } Compiling and running the above code. Compiling and running c programs in gcc compiler 1. Save the code in a file say hello.c 2. To compile open terminal type gcc hello.c 3. To execute type ./a.out You can also specify the output file name as follows: gcc hello.c -o hello.out hello.out is name of output file. Turbo C compiler 1. Copy and paste above code in a file and save it. 2. Compile by pressing Alt+F9. 3. Execute by pressing Ctrl+f9 4. For output of this program press (Alt+f5). We may store "hello world" in a character array and then print it. #include int main() { char string[] = "Hello World"; printf("%s\n", string); return 0; } Output:

2. C program print integer This c program first inputs an integer and then prints it. Input is done using scanf function and number is printed on screen using printf. #include main() { int a; printf("Enter an integer\n"); scanf("%d", &a); printf("Integer that you have entered is %d\n", a); return 0; } Output:

3.C program to add two numbers C program to add two numbers: This c language program perform the basic arithmetic operation of addition on two numbers and then prints the sum on the screen. For example if the user entered two numbers as 5, 6 then 11 ( 5 + 6 ) will be printed on the screen. #include #include main() { int a, b, c; printf("Enter two numbers to add "); scanf("%d%d",&a,&b); c = a + b; printf("Sum of entered numbers = %d\n",c); getch(); return 0; } Output:-

Addition without using third variable #include main() { int a = 1, b = 2; /* Storing result of addition in variable a */ a = a + b; /* Not recommended because original value of a is lost * and you may be using it some where in code considering it * as it was entered by the user. */ printf("Sum of a and b = %d\n", a); return 0; } Adding numbers in c using function We have used long data type as it can handle large numbers. #include long addition(long, long); main() { long first, second, sum; scanf("%ld%ld", &first, &second); sum = addition(first, second); printf("%ld\n", sum); return 0; } long addition(long a, long b) {

long result; result = a + b; return result; }

4. C program to perform addition, subtraction, multiplication and division This c program perform basic arithmetic operations i.e. add , subtract, multiply and divide two numbers. Numbers are assumed to be integers and will be entered by the user. #include #include main() { int first, second, add, sub, mul, div; printf("Enter two numbers "); scanf("%d%d",&first,&second); add = first + second; sub = first - second; mul = first * second; div = first / second; printf("Sum of two numbers = %d\n",add); printf("Difference of two numbers = %d\n",sub); printf("Multiplication of two numbers = %d\n",mul); printf("Division of two numbers = %d",div); getch(); return 0; }

Output:

5. C program to multiply numbers without using multiplication sign #include main() { int x, y, c, temp, temp1, result = 0; scanf("%d%d",&x,&y); temp = x; temp1 = y; if ( temp < 0 ) temp = -temp;

/* If x is negative*/

for ( c = 1 ; c = 'A' && a 0) { movemouseptr(tempx, tempy); x = tempx; y = tempy; } tempx = x; tempy = y; } closegraph(); return 0; }

C Graphics Codes1. Draw shapes using C This c graphics program draws basic shapes such as circle, line, rectangle, ellipse and display text on screen using c graphics. This can be a first graphics program for a beginner. C programming code #include #include main() { int gd = DETECT,gm,left=100,top=100,right=200,bottom=200,x= 300,y=150,radius=50; initgraph(&gd, &gm, "C:\\TC\\BGI"); rectangle(left, top, right, bottom); circle(x, y, radius); bar(left + 300, top, right + 300, bottom); line(left - 10, top + 150, left + 410, top + 150); ellipse(x, y + 200, 0, 360, 100, 50); outtextxy(left + 100, top + 325, "My First C Graphics Program");

getch(); closegraph(); return 0; } Output of program:

2. C program draw bar chart This program draws bar chart using c graphics. Chart is drawn using bars filled with different styles and in different colors. C programming code #include #include main() { int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI"); setcolor(YELLOW); rectangle(0,30,639,450); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); setcolor(WHITE); outtextxy(275,0,"Bar Chart"); setlinestyle(SOLID_LINE,0,2); line(100,420,100,60); line(100,420,600,420); line(90,70,100,60); line(110,70,100,60); line(590,410,600,420); line(590,430,600,420); outtextxy(95,35,"Y"); outtextxy(610,405,"X"); outtextxy(85,415,"O"); setfillstyle(LINE_FILL,BLUE); bar(150,100,200,419); setfillstyle(XHATCH_FILL,RED); bar(225,150,275,419); setfillstyle(WIDE_DOT_FILL,GREEN); bar(300,200,350,419); setfillstyle(INTERLEAVE_FILL,MAGENTA); bar(375,125,425,419); setfillstyle(HATCH_FILL,BROWN); bar(450,175,500,419); getch(); return 0; }

Output of program:

3. C program to draw pie chart This c program draws a pie chart showing percentage of various components drawn with different filling styles and colors. C programming code #include #include main() { int gd = DETECT, gm, midx, midy; initgraph(&gd, &gm, "C:\\TC\\BGI"); setcolor(MAGENTA); rectangle(0,40,639,450); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); setcolor(WHITE); outtextxy(275,10,"Pie Chart"); midx = getmaxx()/2;

midy = getmaxy()/2; setfillstyle(LINE_FILL,BLUE); pieslice(midx, midy, 0, 75, 100); outtextxy(midx+100, midy - 75, "20.83%"); setfillstyle(XHATCH_FILL,RED); pieslice(midx, midy, 75, 225, 100); outtextxy(midx-175, midy - 75, "41.67%"); setfillstyle(WIDE_DOT_FILL,GREEN); pieslice(midx, midy, 225, 360, 100); outtextxy(midx+75, midy + 75, "37.50%"); getch(); return 0; } Output of program:

4. C program to draw a 3d bar chart This c program draws a 3d bar chart. C code #include #include

main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); setcolor(YELLOW); rectangle(0,30,639,450); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); setcolor(WHITE); outtextxy(275,0,"Bar Chart"); setlinestyle(SOLID_LINE,0,2); line(100,420,100,60); line(100,420,600,420); line(90,70,100,60); line(110,70,100,60); line(590,410,600,420); line(590,430,600,420); outtextxy(95,35,"Y"); outtextxy(610,405,"X"); outtextxy(85,415,"O"); setfillstyle(LINE_FILL,BLUE); bar(150,100,200,419); setfillstyle(XHATCH_FILL,RED); bar(225,150,275,419); setfillstyle(WIDE_DOT_FILL,GREEN); bar(300,200,350,419); setfillstyle(INTERLEAVE_FILL,MAGENTA); bar(375,125,425,419); setfillstyle(HATCH_FILL,BROWN); bar(450,175,500,419); getch(); return 0; }

Output of program:

5. C smiling face animation This animation using c draws a smiling face which appears at random position on screen. See output below the code, it will help you in understanding the code easily. C programming code #include #include #include main() { int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75; void *p; initgraph(&gd,&gm,"C:\\TC\\BGI"); setcolor(YELLOW); circle(50,100,25); setfillstyle(SOLID_FILL,YELLOW); floodfill(50,100,YELLOW);

setcolor(BLACK); setfillstyle(SOLID_FILL,BLACK); fillellipse(44,85,2,6); fillellipse(56,85,2,6); ellipse(50,100,205,335,20,9); ellipse(50,100,205,335,20,10); ellipse(50,100,205,335,20,11); area = imagesize(left, top, left + 50, top + 50); p = malloc(area); setcolor(WHITE); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); outtextxy(155,451,"Smiling Face Animation"); setcolor(BLUE); rectangle(0,0,639,449); while(!kbhit()) { temp1 = 1 + random ( 588 ); temp2 = 1 + random ( 380 ); getimage(left, top, left + 50, top + 50, p); putimage(left, top, p, XOR_PUT); putimage(temp1 , temp2, p, XOR_PUT); delay(100); left = temp1; top = temp2; } getch(); closegraph(); return 0; }

Output of program:

6. C program to draw circles in circles This program draws circles in circles in two different colors. C programming code #include #include #include main() { int gd = DETECT, gm, x, y, color, angle = 0; struct arccoordstype a, b; initgraph(&gd, &gm, "C:\\TC\\BGI"); delay(2000); while(angle=0 ; i--) { sprintf(a,"%d",i); outtextxy(getmaxx()/2, getmaxy()/2, a); delay(1000); if ( i == 0 ) break; cleardevice(); } getch(); closegraph(); return 0; }

8.C Program for Bresenham Circle Drawing algorithm # include # include # include # include void main() { int gd=DETECT,gm; int r,x,y,p,xc=320,yc=240; initgraph(&gd,&gm,"C:\\TC\\BGI"); cleardevice();

printf("Enter the radius "); scanf("%d",&r);

x=0; y=r; putpixel(xc+x,yc-y,1); p=3-(2*r); for(x=0;x