Program-1
Write a program to show that during function overloading, if no matching
argument is found, then java will apply automatic type conversions (from
lower to higher data type).
class AutoTypePromotion {
void show(int x) {
[Link]("Called show(int x): " + x);
void show(double x) {
[Link]("Called show(double x): " + x);
public static void main(String[] args) {
AutoTypePromotion obj = new AutoTypePromotion();
[Link](5);
[Link](5.5);
[Link]('A');
[Link](5.5f);
}
Output:
Program-2
Write a program to show the difference between public and private access
specifiers.
class Test {
public int publicVar = 10;
private int privateVar = 20;
public void show() {
[Link]("publicVar: " + publicVar);
[Link]("privateVar: " + privateVar);
class SpecifierDemo {
public static void main(String[] args) {
Test t = new Test();
[Link]("Accessing publicVar: " + [Link]);
[Link]();
}
Output:
Program-3
Write a Java Program to implement the Vector class and its methods.
import [Link];
class VectorDemo {
public static void main(String[] args) {
Vector<Integer> v = new Vector<Integer>();
[Link](10);
[Link](20);
[Link](30);
[Link]("Initial Vector: " + v);
[Link](1, 15);
[Link]("After adding 15 at index 1: " + v);
[Link](2);
[Link]("After removing element at index 2: " + v);
[Link](1, 25);
[Link]("After setting index 1 to 25: " + v);
[Link]("Element at index 1: " + [Link](1));
[Link]("Size of vector: " + [Link]());
[Link]();
[Link]("Vector after clear: " + v);
}
}
Program-4
Write a Java Program to implement Wrapper classes and their methods.
class WrapperDemo {
public static void main(String[] args) {
Integer intObj = [Link](100);
Double doubleObj = [Link](3.14);
Character charObj = [Link]('A');
Boolean boolObj = [Link](true);
int i = [Link]();
double d = [Link]();
char c = [Link]();
boolean b = [Link]();
[Link]("Integer object: " + intObj);
[Link]("Double object: " + doubleObj);
[Link]("Character object: " + charObj);
[Link]("Boolean object: " + boolObj);
[Link]("int value: " + i);
[Link]("double value: " + d);
[Link]("char value: " + c);
[Link]("boolean value: " + b);
String str = "200";
Integer parseInt = [Link](str);
[Link]("Parsed integer from string: " + parseInt);
intObj = [Link]("150");
[Link]("Changed value of intObj: " + intObj);
String s = [Link]();
[Link]("String representation of intObj: " + s);
Output:
Program-5
Write a Java Program to implement inheritance and demonstrate the use of
method overriding.
class Animal {
void sound() {
[Link]("Animal makes a sound");
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
class InheritanceDemo {
public static void main(String[] args) {
Animal a = new Animal();
[Link]();
Dog d = new Dog();
[Link]();
Animal ref = new Dog();
[Link]();
}
}
Output:
Program-6
Write a program to demonstrate the use of implementing extending
interfaces.
interface Printable {
void print();
}
interface Showable extends Printable {
void show();
class DemoClass implements Showable {
public void print() {
[Link]("Printing from Printable interface");
public void show() {
[Link]("Showing from Showable interface");
class InterfaceDemo {
public static void main(String[] args) {
DemoClass obj = new DemoClass();
[Link]();
[Link]();
}
}
Output:
Program-7
Write a program to implement the concept of threading by extending Thread
Class.
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link](i + " from " + getName());
try {
[Link](500);
} catch (InterruptedException e) {
[Link]("Thread interrupted");
}
}
class ThreadDemo {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
[Link]("Thread-1");
[Link]("Thread-2");
[Link]();
[Link]();
}
}
Output:
Program-10
Write programs for using Graphics class.
import [Link];
import [Link];
public class SimpleGraphics extends Frame {
public SimpleGraphics() {
setSize(400, 300); // Width 400, Height 300
setTitle("Graphics Example");
setVisible(true);
public void paint(Graphics g) {
// Draw a rectangle
[Link](50, 50, 200, 100);
// Draw a string inside the rectangle
[Link]("Graphics in Java AWT", 75, 100);
public static void main(String[] args) {
new SimpleGraphics();
}
}
Output:
Program-1
Write a program to find length of string, concatenation of two strings
without using inbuilt function.
#include <iostream.h>
#include <conio.h>
void main()
clrscr();
char str1[50], str2[50], concat[100];
int i, j, len1 = 0, len2 = 0;
cout << "Enter first string: ";
cin >> str1;
cout << "Enter second string: ";
cin >> str2;
for(i = 0; str1[i] != '\0'; i++)
len1++;
for(j = 0; str2[j] != '\0'; j++)
len2++;
cout << "Length of first string: " << len1 << endl;
cout << "Length of second string: " << len2 << endl;
for(i = 0; str1[i] != '\0'; i++)
concat[i] = str1[i];
for(j = 0; str2[j] != '\0'; j++, i++)
concat[i] = str2[j];
concat[i] = '\0';
cout << "Concatenated string: " << concat;
getch();
Output:
Program-2
Write a program to Print a Symbol table.
#include <iostream.h>
#include <conio.h>
struct Symbol
char name[20];
char type[10];
int address;
};
void main()
clrscr();
Symbol s[20];
int n, i;
cout << "Enter number of symbols: ";
cin >> n;
for(i = 0; i < n; i++)
cout << "\nEnter name of symbol " << i+1 << ": ";
cin >> s[i].name;
cout << "Enter type of symbol: ";
cin >> s[i].type;
cout << "Enter address of symbol: ";
cin >> s[i].address;
cout << "\n-----------------------------------";
cout << "\nName\tType\tAddress";
cout << "\n-----------------------------------\n";
for(i = 0; i < n; i++)
cout << s[i].name << "\t" << s[i].type << "\t" << s[i].address << "\n";
getch();
Output:
Program-3
Write a program to implement pattern matching.
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
void main()
clrscr();
char text[100], pattern[50];
int i, j, found = 0;
cout << "Enter text: ";
gets(text);
cout << "Enter pattern: ";
gets(pattern);
int n = strlen(text);
int m = strlen(pattern);
for(i = 0; i <= n - m; i++)
for(j = 0; j < m; j++)
if(text[i + j] != pattern[j])
break;
if(j == m)
{
cout << "Pattern found at position: " << i + 1;
found = 1;
break;
if(!found)
cout << "Pattern not found";
getch();
Output:
Program-4
Write a program to to validate an email ID/Mobile Number.
#include <iostream.h>
#include <conio.h>
#include <string.h>
int isValidMobile(char m[])
int i, len = strlen(m);
if(len != 10)
return 0;
for(i = 0; i < len; i++)
if(m[i] < '0' || m[i] > '9')
return 0;
if(m[0] != '6' && m[0] != '7' && m[0] != '8' && m[0] != '9')
return 0;
return 1;
int isValidEmail(char e[])
int i, at = 0, dot = 0, len = strlen(e);
for(i = 0; i < len; i++)
if(e[i] == '@')
at = i;
if(e[i] == '.')
dot = i;
if(at > 0 && dot > at + 1 && dot < len - 1)
return 1;
else
return 0;
void main()
clrscr();
char input[100];
cout << "Enter Email ID or Mobile Number: ";
cin >> input;
if(isValidMobile(input))
cout << "Valid Mobile Number";
else if(isValidEmail(input))
cout << "Valid Email ID";
else
cout << "Invalid Input";
getch();
Output:
Program-5
Write a program to to check number of single spaces, double spaces and
multiple spaces in a given string. Print the resultant string after replacing
double/multiple spaces with single space.
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
clrscr();
char str[200], result[200];
int i, j = 0, single = 0, doubleSpace = 0, multiSpace = 0;
cout << "Enter a string: ";
[Link](str, 200);
for(i = 0; str[i] != '\0'; i++)
if(str[i] == ' ')
int count = 1, k = i + 1;
while(str[k] == ' ')
count++;
k++;
if(count == 1)
single++;
else if(count == 2)
doubleSpace++;
else if(count > 2)
multiSpace++;
result[j++] = ' ';
i = k - 1;
else
result[j++] = str[i];
result[j] = '\0';
cout << "\nNumber of single spaces: " << single;
cout << "\nNumber of double spaces: " << doubleSpace;
cout << "\nNumber of multiple spaces: " << multiSpace;
cout << "\nResultant string: " << result;
getch();
}
Program-1
Write a program that draws a side-view of a car using only built-in shape
primitives (lines, rectangles, circles/ellipses, arcs), with no external images.
#include <graphics.h>
#include <conio.h>
void main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
rectangle(150, 300, 450, 350);
rectangle(200, 250, 400, 300);
rectangle(220, 260, 280, 300);
rectangle(320, 260, 380, 300);
circle(200, 350, 30);
circle(400, 350, 30);
circle(200, 350, 10);
circle(400, 350, 10);
getch();
closegraph();
}
Output:
Program-2
Create a program that renders at least five concentric circles sharing the
same centre, each in a different colour.
#include <graphics.h>
#include <conio.h>
void main() {
int gd = DETECT, gm;
int x, y;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
x = getmaxx() / 2;
y = getmaxy() / 2;
setcolor(RED);
circle(x, y, 30);
setcolor(GREEN);
circle(x, y, 60);
setcolor(YELLOW);
circle(x, y, 90);
setcolor(BLUE);
circle(x, y, 120);
setcolor(MAGENTA);
circle(x, y, 150);
getch();
closegraph();
}
Program-3
Develop a program that draws the Olympic Rings by placing five
interlocking rings in the official colours (blue, yellow, black, green, red)
and correct relative positions using circle primitives.
#include <graphics.h>
#include <conio.h>
void main() {
int gd = DETECT, gm;
int x = 200, y = 200, radius = 50, offsetX = 110, offsetY = 60;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
setcolor(BLUE);
circle(x, y, radius);
setcolor(YELLOW);
circle(x + offsetX / 2, y + offsetY, radius);
setcolor(BLACK);
circle(x + offsetX, y, radius);
setcolor(GREEN);
circle(x + 3 * offsetX / 2, y + offsetY, radius);
setcolor(RED);
circle(x + 2 * offsetX, y, radius);
getch();
closegraph();
Output:
Program-4
Create an animation of a ball that moves within a rectangular boundary and
bounces off the walls, including simple gravity and energy loss.
#include <graphics.h>
#include <conio.h>
#include <dos.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TurboC++\\Disk\\TurboC3\\BGI");
int x = getmaxx() / 2;
int y = 30;
int flag = 1;
while (!kbhit()) {
cleardevice();
setcolor(WHITE);
circle(x, y, 30);
if (flag)
y = y + 5;
else
y = y - 5;
if (y >= getmaxy() - 30)
flag = 0;
if (y <= 30)
flag = 1;
delay(10);
}
closegraph();
return 0;
Ourput:
Program-5
Write a program that draws a simple kite using only built-in shape functions:
a diamond-shaped body filled with colour, two cross spars, and a short tail.
#include<graphics.h>
#include<conio.h>
int main()
int points[] = {320, 150, 370, 200, 320, 250, 270, 200, 320, 150};
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
setcolor(BLUE);
setfillstyle(SOLID_FILL, CYAN);
fillpoly(5, points);
line(320, 150, 320, 250);
line(270, 200, 370, 200);
line(320, 250, 310, 280);
line(310, 280, 300, 270);
getch();
closegraph();
return 0;
}
Output:
Program-6
Draw a triangle and fill its interior with a single colour using a simple scanline
approach.
#include<graphics.h>
#include<conio.h>
void scanlineFill(int x1, int y1, int x2, int y2, int x3, int y3, int color)
int i, j, minY, maxY;
minY = y1 < y2 ? (y1 < y3 ? y1 : y3) : (y2 < y3 ? y2 : y3);
maxY = y1 > y2 ? (y1 > y3 ? y1 : y3) : (y2 > y3 ? y2 : y3);
for(i = minY; i <= maxY; i++)
int nodes = 0, nodeX[10], j;
if ((i >= y1 && i < y2) || (i >= y2 && i < y1))
nodeX[nodes++] = x1 + (i - y1) * (x2 - x1) / (y2 - y1);
if ((i >= y2 && i < y3) || (i >= y3 && i < y2))
nodeX[nodes++] = x2 + (i - y2) * (x3 - x2) / (y3 - y2);
if ((i >= y3 && i < y1) || (i >= y1 && i < y3))
nodeX[nodes++] = x3 + (i - y3) * (x1 - x3) / (y1 - y3);
for(j = 0; j < nodes-1; j+=2)
line(nodeX[j], i, nodeX[j+1], i);
setcolor(color);
int main()
int x1=200, y1=150, x2=300, y2=300, x3=100, y3=300;
int gd=DETECT, gm;
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
setcolor(WHITE);
triangle:
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
setcolor(RED);
scanlineFill(x1,y1,x2,y2,x3,y3,RED);
getch();
closegraph();
return 0;
}
Output:
Program-7
Create a solid circle by first drawing its outline and then using flood fill from
the centre.
#include<graphics.h>
#include<conio.h>
int main()
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
int x=250, y=200, radius=80;
circle(x, y, radius);
floodfill(x, y, WHITE);
getch();
closegraph();
return 0;
}
Output:
Program-9
Write a program to draw a line using Bresenham’s line algorithm.
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
int main()
int x1=100, y1=100, x2=400, y2=300;
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int sx = (x1 < x2) ? 1 : -1;
int sy = (y1 < y2) ? 1 : -1;
int err = dx - dy;
int e2;
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
while(1)
putpixel(x1, y1, WHITE);
if (x1 == x2 && y1 == y2)
break;
e2 = 2 * err;
if(e2 > -dy)
err = err - dy;
x1 = x1 + sx;
if(e2 < dx)
err = err + dx;
y1 = y1 + sy;
getch();
closegraph();
return 0;
}
Output:
Program-10
Write a program to draw a circle using the Midpoint circle algorithm.
#include<graphics.h>
#include<conio.h>
void put8pixels(int xc, int yc, int x, int y, int color)
putpixel(xc+x, yc+y, color);
putpixel(xc-x, yc+y, color);
putpixel(xc+x, yc-y, color);
putpixel(xc-x, yc-y, color);
putpixel(xc+y, yc+x, color);
putpixel(xc-y, yc+x, color);
putpixel(xc+y, yc-x, color);
putpixel(xc-y, yc-x, color);
void midpointCircle(int xc, int yc, int r, int color)
int x=0, y=r;
int d=1-r;
put8pixels(xc, yc, x, y, color);
while(x < y)
if(d < 0)
{
d = d + 2*x + 3;
else
d = d + 2*(x - y) + 5;
y--;
x++;
put8pixels(xc, yc, x, y, color);
int main()
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
midpointCircle(250, 200, 100, WHITE);
getch();
closegraph();
return 0;
}
Output:
Program-8
Fill a donut shape: draw two concentric circles; flood fill the ring (area
between them) without colouring the hole.
#include<graphics.h>
#include<conio.h>
int main()
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
int x = 250, y = 200;
int outerRadius = 100;
int innerRadius = 50;
circle(x, y, outerRadius);
circle(x, y, innerRadius);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(x, y + innerRadius + 1, WHITE);
getch();
closegraph();
return 0;
}
Output:
Program-11
Write a program to draw an ellipse using the Midpoint ellipse algorithm.
#include <graphics.h>
#include <conio.h>
void put4pixels(int xc, int yc, int x, int y, int color)
putpixel(xc + x, yc + y, color);
putpixel(xc - x, yc + y, color);
putpixel(xc + x, yc - y, color);
putpixel(xc - x, yc - y, color);
void midpointEllipse(int xc, int yc, int rx, int ry, int color)
int x = 0;
int y = ry;
float rxSq = rx * rx;
float rySq = ry * ry;
float p;
float px = 0;
float py = 2 * rxSq * y;
put4pixels(xc, yc, x, y, color);
p = rySq - (rxSq * ry) + (0.25 * rxSq);
while (px < py)
x++;
px += 2 * rySq;
if (p < 0)
p += rySq + px;
else
y--;
py -= 2 * rxSq;
p += rySq + px - py;
put4pixels(xc, yc, x, y, color);
p = rySq * (x + 0.5) * (x + 0.5) + rxSq * (y - 1) * (y - 1) - rxSq * rySq;
while (y > 0)
y--;
py -= 2 * rxSq;
if (p > 0)
p += rxSq - py;
else
x++;
px += 2 * rySq;
p += rxSq - py + px;
put4pixels(xc, yc, x, y, color);
int main()
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
midpointEllipse(250, 200, 150, 100, WHITE);
getch();
closegraph();
return 0;
}
Output:
Program-12
Write a program to translate and rotate a triangle about its own centroid,
then redraw both the original and transformed triangle.
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int color) {
setcolor(color);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
int main() {
int gd = DETECT, gm;
int x1, y1, x2, y2, x3, y3;
float tx, ty, angle, rad;
float cx, cy;
float rx1, ry1, rx2, ry2, rx3, ry3;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
printf("Enter first coordinate (x1 y1): ");
scanf("%d%d", &x1, &y1);
printf("Enter second coordinate (x2 y2): ");
scanf("%d%d", &x2, &y2);
printf("Enter third coordinate (x3 y3): ");
scanf("%d%d", &x3, &y3);
drawTriangle(x1, y1, x2, y2, x3, y3, CYAN);
cx = (x1 + x2 + x3) / 3.0;
cy = (y1 + y2 + y3) / 3.0;
printf("Enter translation vector (tx ty): ");
scanf("%f%f", &tx, &ty);
printf("Enter rotation angle (degrees): ");
scanf("%f", &angle);
rad = angle * (3.14159 / 180.0);
rx1 = (x1 - cx) * cos(rad) - (y1 - cy) * sin(rad) + cx + tx;
ry1 = (x1 - cx) * sin(rad) + (y1 - cy) * cos(rad) + cy + ty;
rx2 = (x2 - cx) * cos(rad) - (y2 - cy) * sin(rad) + cx + tx;
ry2 = (x2 - cx) * sin(rad) + (y2 - cy) * cos(rad) + cy + ty;
rx3 = (x3 - cx) * cos(rad) - (y3 - cy) * sin(rad) + cx + tx;
ry3 = (x3 - cx) * sin(rad) + (y3 - cy) * cos(rad) + cy + ty;
drawTriangle((int)rx1, (int)ry1, (int)rx2, (int)ry2, (int)rx3, (int)ry3, YELLOW);
getch();
closegraph();
return 0;
Output:
Program- 13
Write a program to uniformly and non-uniformly scale a rectangle about
its center and display the before/after outlines.
#include <graphics.h>
#include <stdio.h>
void drawRectangle(int left, int top, int right, int bottom, int color) {
setcolor(color);
rectangle(left, top, right, bottom);
int main() {
int gd = DETECT, gm;
int left, top, right, bottom;
float sx, sy;
float cx, cy;
int newLeft, newTop, newRight, newBottom;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
printf("Enter left and top coordinates of rectangle: ");
scanf("%d%d", &left, &top);
printf("Enter right and bottom coordinates of rectangle: ");
scanf("%d%d", &right, &bottom);
drawRectangle(left, top, right, bottom, WHITE);
cx = (left + right) / 2.0;
cy = (top + bottom) / 2.0;
printf("Enter scaling factor for X (sx): ");
scanf("%f", &sx);
printf("Enter scaling factor for Y (sy): ");
scanf("%f", &sy);
newLeft = (int)(cx + (left - cx) * sx);
newTop = (int)(cy + (top - cy) * sy);
newRight = (int)(cx + (right - cx) * sx);
newBottom = (int)(cy + (bottom - cy) * sy);
drawRectangle(newLeft, newTop, newRight, newBottom, YELLOW);
getch();
closegraph();
return 0;
Output:
Program-14
Write a program to reflect a polygon first across the x-axis and then across
the y-axis, showing the original and both reflections.
#include <graphics.h>
#include <conio.h>
void drawPolygon(int pts[][2], int n)
int i;
for(i=0; i<n-1; i++)
line(pts[i][0], pts[i][1], pts[i+1][0], pts[i+1][1]);
// Closing the polygon
line(pts[n-1][0], pts[n-1][1], pts[0][0], pts[0][1]);
void reflectX(int pts[][2], int n, int result[][2], int axisY)
int i;
for(i=0; i<n; i++)
result[i][0] = pts[i][0];
result[i][1] = 2*axisY - pts[i][1];
}
void reflectY(int pts[][2], int n, int result[][2], int axisX)
int i;
for(i=0; i<n; i++)
result[i][0] = 2*axisX - pts[i][0];
result[i][1] = pts[i][1];
int main()
int gd=DETECT, gm;
int n=4;
int pts[4][2] = {{200,200}, {250,150}, {300,200}, {250,250}};
int reflectedX[4][2], reflectedY[4][2];
int x_axis = 300; // y = 300 is x-axis for reflection (can be screen center y)
int y_axis = 250; // x = 250 is y-axis for reflection (can be screen center x)
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
setcolor(WHITE);
drawPolygon(pts, n);
setcolor(RED);
reflectX(pts, n, reflectedX, x_axis);
drawPolygon(reflectedX, n);
setcolor(GREEN);
reflectY(pts, n, reflectedY, y_axis);
drawPolygon(reflectedY, n);
getch();
closegraph();
return 0;
Output:
Program-15
Write a program to demonstrate window-to-viewport mapping by drawing
a polyline in a “world window” and its mapped version in a “screen
viewport”.
#include <graphics.h>
#include <conio.h>
void draw_polyline(int pts[][2], int n)
int i;
for (i = 0; i < n - 1; i++)
line(pts[i][0], pts[i][1], pts[i+1][0], pts[i+1][1]);
int main()
int gd = DETECT, gm;
int pts[5][2] = { {100,200}, {200,300}, {300,250}, {400,350}, {500,300} };
int n = 5;
int i;
int vx = 600, vy = 150, vw = 300, vh = 250;
int mapped_pts[5][2];
float xwmin = 50, xwmax = 550, ywmin = 150, ywmax = 400;
float xvmin = vx, xvmax = vx+vw, yvmin = vy, yvmax = vy+vh;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
rectangle(50, 150, 550, 400);
draw_polyline(pts, n);
rectangle(vx, vy, vx+vw, vy+vh);
for (i = 0; i < n; i++)
mapped_pts[i][0] = (int)((pts[i][0] - xwmin) * (xvmax - xvmin) / (xwmax - xwmin) +
xvmin);
mapped_pts[i][1] = (int)((pts[i][1] - ywmin) * (yvmax - yvmin) / (ywmax - ywmin) +
yvmin);
draw_polyline(mapped_pts, n);
getch();
closegraph();
return 0;
}
Output: