Tuesday, September 25, 2018

Write a C program to test whether a given identifier is valid or not

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
    char a[10];
    int flag,
    i=1;
    clrscr();
    printf("\n Enter an identifier:");
    gets(a);
    if(isalpha(a[0]))
    flag=1;
    else
    printf("\n Not a valid identifier");
    while(a[i]!='\0')
    {
        if(!isdigit(a[i])&&!isalpha(a[i]))
        {
            flag=0; break;
           
        } i++;
       
    }
    if(flag==1)
    printf("\n Valid identifier");
    getch();
   
}

Thursday, September 13, 2018

Program for DDA Line Drawing Algorithm in C

//   Program for DDA Line Drawing Algorithm in C
 
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.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("Enter 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,5);
        x=x+dx;
        y=y+dy;
        i=i+1;
        delay(100);
    }
 
    closegraph();
}

Tuesday, September 11, 2018

Compiler Design Program


/******************************************************************************

Write a C program to identify whether a given line is a comment or not.

*******************************************************************************/

#include<stdio.h>
#include<conio.h>
void main()
{
    char com[30];
    int i=2,a=0;
    clrscr();
    printf("\n Enter comment:");
    gets(com);
    if(com[0]=='/')
    {
        if(com[1]=='/')
    printf("\n It is a comment");
    else if(com[1]=='*')
    {
        for(i=2;i<=30;i++)
        {
            if(com[i]=='*'&&com[i+1]=='/')
            {
                printf("\n It is a comment");
                a=1;
                break;
               
            } else continue;
           
        }
        if(a==0)
        printf("\n It is not a comment");
       
    } else printf("\n It is not a comment");
       
    } else printf("\n It is not a comment");
    getch();
   
}