C

Tuesday, 30 October 2018

Write a program to draw a circle using mid-point circle drawing algorithm.


#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void drawcircle(int x0, int y0, int r)
{
                int x=r;
                int y=0
                int err=0;

                while(x>=y)
                {
                                putpixel(x0+x,y0+y,7);
                                putpixel(x0+y,y0+x,7);
                                putpixel(x0-y,y0+x,7);
                                putpixel(x0-x,y0+y,7);
                                putpixel(x0-x,y0-y,7);
                                putpixel(x0-y,y0-x,7);
                                putpixel(x0+y,y0-x,7);
                                putpixel(x0+x,y0-y,7);
                                if(err<=0)
                                {
                                                y+=1;
                                                err+=2*y+1;
                                }
                                if(err>0)
                                {
                                                x-=1;
                                                err-=2*x+1;
                                }
                }
}
int main()
{
                int gd=DETECT, gm, error,x,y,r;
                initgraph(&gd,&gm,"c:\\turboc3\\bgi");
                cout<<"Enter the radius of Circle :";
                cin>>r;
                cout<<"Enter values for center x and y :";
                cin>>x>>y;
                drawcircle(x,y,r);
                getch();
                return 0;
}

Write a program to draw a circle using Bresenham’s circle drawing algorithm.


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void drawCircle(int x, int y, int xc, int yc);
void main()
{
                int gd = DETECT, gm;
                int r, xc, yc, pk, x, y;
                initgraph(&gd, &gm, "c:\\turboc3\\bgi");
                cout<<"\n
                Enter the values for center x and y\n";
                cin>>xc>>yc;
                cout<<"Enter the radius of circle\n";
                cin>>r;
                pk = 3 - 2*r;
                x=0; y = r;
                drawCircle(x,y,xc,yc);
                while(x < y)
                {
                                if(pk <= 0)
                                {
                                                pk = pk + (4*x) + 6;
                                                drawCircle(++x,y,xc,yc);
                                }
                                else
                                {
                                                pk = pk + (4*(x-y)) + 10;
                                                drawCircle(++x,--y,xc,yc);
                                }
                }

                getch();
                closegraph();
}

void drawCircle(int x, int y, int xc, int yc)
{
                putpixel(x+xc,y+yc,WHITE);
                putpixel(-x+xc,y+yc,WHITE);
                putpixel(x+xc, -y+yc,WHITE);
                putpixel(-x+xc, -y+yc, WHITE);
                putpixel(y+xc, x+yc, WHITE);
                putpixel(y+xc, -x+yc, WHITE);
                putpixel(-y+xc, x+yc, WHITE);
                putpixel(-y+xc, -x+yc, WHITE);
}

Write a C++ program to draw a line using Bresenham’s line drawing algorithm.


#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void drawline(int x0, int y0, int x1, int y1)
{
                int dx, dy,p,x,y;

                dx=x1-x0;
                dy=y1-y0;
                x=x0;
                y=y0;

                p=2*dy-dx;
                while(x<x1)
                {
                                if(p>=0)
                                {
                                                putpixel(x,y,7);
                                                y=y+1;
                                                p=p+2*dy-2*dx;
                                }
                                else
                                {
                                                putpixel(x,y,7);
                                                p=p+2*dy;
                                }
                                x=x+1;
                }
}
int main()
{
                int gd=DETECT, gm, error, x0,y0,x1,y1;
                initgraph(&gd,&gm,"c:\\turboc3\\bgi");
                cout<<"Enter the values of x0 and y0 :";
                cin>>x0>>y0;
                cout<<"Enter the vlaues of x1 and y1 :";
                cin>>x1>>y1;
                drawline(x0,y0,x1,y1);
                getch();
                return 0;
}

Write a C++ program of DDA Line Drawing Algorithm..

#include<graphics.h>
#include<stdio.h>
#include<math.h>
#include<dos.h>
#include<conio.h>
void main( )
{
       float x,y,x1,y1,x2,y2,dx,dy,step;
       int i,gd=DETECT,gm;

     initgraph(&gd,&gm,"c:\\turboc3\\bgi");

      printf("\nEnter the value of x1 and y1 : ");
      scanf("%f%f",&x1,&y1);
      printf("Enter the value of x2 and y2: ");
      scanf("%f%f",&x2,&y2);

    dx=abs(x2-x1);
    dy=abs(y2-y1);

    if(dx>=dy)
            step=dx;
    else
           step=dy;

    dx=dx/step;
    dy=dy/step;

    x=x1;
    y=y1;

    i=1;
    while(i<=step)
    {
           putpixel(x,y,WHITE);
           x=x+dx;
           y=y+dy;
           i=i+1;
           delay(100);
    }

getch();
        closegraph();
}

Monday, 2 October 2017

Program Of Increment and Decrement Operator.

#include<stdio.h>
main()
{
int x,y,exp1,exp2;
clrscr();
x=10;
y=15;
exp1=x* ++y;
printf("Value of the first expression is: %d\n",exp1);
x=10;
y=15;
exp2=x* y++;
printf("Value of second expression is : %d\n",exp2);
}

Program To Find The Values Of Logical Operators Expressions.

#include<stdio.h>
main()
{
int x=60, y=200, b=2, c=160;
int exp1, exp2, exp3, exp4, exp5, exp6;
clrscr();
exp1= x<y;
exp2= x+y<y;
exp3= x*b==120;
exp4= (x==60)&&(y>b);
exp5= (y!=60)||(c>x);
exp6= !(x>60);
printf("Value of first expression is : %d\n",exp1);
printf("Value of second expression is : %d\n",exp2);
printf("Valye of third expression is : %d\n",exp3);
printf("Value of forth expression is : %d\n",exp4);
printf("Value of fith expression is : %d\n",exp5);
printf("Value of sixth expression is : %d\n",exp6);
}

Program To Find The Solution Of Mixed Mode Expression

#include<stdio.h>
main()
{
float exp1, exp2;
int A, I, J, K;
clrscr();
A=4.5;
I=1;
J=2;
K=3;
exp1= J*K+A;
exp2= K/J*(A+2.3*I);
printf("Value of first expression is : %f\n",exp1);
printf("Value of second expression is : %f\n",exp2);
}

Program To Find Value Of Float Expression.

#include<stdio.h>
main()
{
float exp1, exp2;
clrscr();
exp1=12.0/3.0+5.5*2.0-3.0;
exp2=(12.5+2.5)*2.0-(11.5-2.5)/3.0+13.5;
printf("Value of first expression is : %f\n",exp1);
printf("Value of second expression is :%f\n",exp2);

}

Program To Find The Value Of Integer Expressions

#include<stdio.h>
main()
{
int exp1, exp2, exp3;
clrscr();
exp1=4*5+5/2-9%2;
exp2=3*5/2+6*9%3+2*3-3;
exp3=3*(5+2*3)+25/(2*3+2);
printf("Value of first expression is : %d\n",exp1);
printf("Value of second expression is : %d\n",exp2);
printf("Value of third expression is : %d\n",exp3);

}

Friday, 29 September 2017

Program Showing Type Conversion Across Assignment

#include<stdio.h>
 main()
 {
int a;
clrscr();
a=1;
printf("\n when a=1 then output is %d\n",a);
a='C';
printf("\n when a='C' then output is %d\n",a);
a=5.2;
printf("\n when a=5.2 then output is %d\n",a);
 }


Program Showing Use of Constants.

#include<stdio.h>
#define PASS_MARKS 40
main()
{
         printf("Passing Marks is : %d", PASS_MARKS);
}



Thursday, 7 September 2017

Program To Search An Element From Array

#include<stdio.h>
main()
{
int i,n,e,a[30];
clrscr();
printf("\nEnter total number of elements : ");
scanf("%d",&n);
printf("Enter %d Integer Numbers\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the element you want to search : ");
scanf("%d",&e);
i=0;
while(i<n&&e!=a[i])
{
i++;
}
printf("The Element of Array is located at : %d position",i+1);
return(0);
}


Program To Sort The Array Elements

#include<stdio.h>
main()
{
int n,i,j,temp,a[30];
clrscr();
printf("\nEnter The Total Number Of Elements You Want In Array :");
scanf("%d",&n);
printf("Enter %d Integer Numbers\n",n);
for(i=0;i<n;++i)
scanf("%d",&a[i]);
for(i=0;i<n;++i)
{
for(j=i+1;j<n;++j)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("The Sorted Array Elements Is\n");
for(i=0;i<n;++i)
printf("%d\n",a[i]);

}


Program To Reverse Of An Array Elements

#include<stdio.h>
main()
{
int a[25],n,i,j,temp;
clrscr();
printf("\nEnter The Number Of Elements You Want In Array : ");
scanf("%d",&n);
printf("Enter The %d Integer Numbers\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
j=i-1;
i=0;
while(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
i++;
j--;
}
printf("The Elements Of Reversed Array Is :");
for(i=0;i<n;i++)
printf("%d\n" ,a[i]);
return(0);
}


Program To Find Smallest And Biggest Number In Array Of Integer Numbers

#include<stdio.h>
main()
{
int a[20],n,i,big,small;
clrscr();
printf("\nEnter Total Numbers You Want In Array : ");
scanf("%d",&n);
printf("\nEnter The %d Integer Numbers\n",n);
for(i=0;i<=n;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=0;i<=n;i++)
if(a[i]>big)
big=a[i];
printf("\nThe Largest Number In Array Is : %d",big);
small=a[0];
for(i=0;i<=n;i++)
if(a[i]<small)
small=a[i];
printf("\nThe Smallest Number Is: %d",small);
return(0);
}


Tuesday, 5 September 2017

Program To Check Whether A Given String Is Palindrome Or Not.

#include<stdio.h>
#include<string.h>
main()
{
char strng[30];
int i,lngth,flag=0;
printf("Enter a string : ");
scanf("%s",&strng);
lngth=strlen(strng);
for(i=0;i<lngth;i++)
{
if(strng[i]!=strng[lngth-i-1])
{
flag=1;
break;
}
}
if(flag)
printf("%s is not a palindrome");
else
printf("%s is a palindrome");
returm 0;
}


Program To Check Whether A Given Number Is Palindrome Or Not.

#include<stdio.h>
main()
{
int num;
int temp;
int digit;
int rnum=0;
clrscr();
printf("Enter The Integer Number : ");
scanf("%d",&num);
temp=num;
do{
digit=temp%10;
temp=temp/10;
rnum=rnum*10+digit;
}
while(temp>0);
if(rnum==num)
printf("The Given Number Is Palindrome");
else
printf("The Given Number Is Not Palindrome");
}


Tuesday, 29 August 2017

C Program To Find First 10 Multiple of a Number Using For Loop

#include<stdio.h>
main()
{
int num, i=1;
clrscr();
printf("Enter the number whose multiple you want to see : ");
scanf("%d",&num);
for(i=1;i<=10;i++)
{
printf("%4d",i*num);
}
getch();
}


C Program To Find Sum of a First 10 Natural Numbers Using For Loop

#include<stdio.h>
main()
{
int i,sum=0;
clrscr();
for(i=1;i<=10;i++)
{
sum=sum+i;
}
printf("The total of first 10 natural number is : %d",sum);
getch();
}


C Program To Find First 10 Multiples of a Number Using Do-While Loop

#include<stdio.h>
main()
{
int num,i=1;
printf("Enter the number whose multiple you want");
scanf("%d",&num);
do
{
printf("%3d",i*num);
i++;
}while(i<=10);
getch();
}


Write a program to draw a circle using mid-point circle drawing algorithm.

#include<iostream.h> #include<graphics.h> #include<conio.h> void drawcircle(int x0, int y0, int r) {         ...