Friday, October 30, 2009

Write a C program to get the maximum and minimum values of a Data type

#include(stdio.h)  // note place '<' & '>' in place of '(' & ')'
#include(conio.h)
void main( )
{
int x, y;
clrscr( );
x=1; 
while( x > 0)
{
y = x;
x + + ;
}
printf("\nMaximum value : %d ", y );
printf("\nMinimum value : %d", x);
getch( );
}

Output:- Maximum value : 32767
Minimum value : -32768

Similarly we can find the below datatypes,
long int ( %ld )
char ( %d )
unsigned ( %u )
.....

Thursday, October 29, 2009

Write a C program to Sort the list of integers using Bubble Sort

#include(stdio.h)  //note place '<' & '>' in place of '(' & ')'
#include(conio.h)
void bubble(int [ ], int); // func. declaration
void main( )
{
 int a[10],n,i;
clrscr();
printf("Enter the no. of elements u want to fill:");
scanf("%d",&n);
printf("\nEnter the array elements:");
for(i=0; i< n; i++)
scanf("%d",&a[i]);

bubble(a,n);  // calling function
getch();
}

void bubble(int b[], int no)  // called function
{
int i,j,temp;
for(i=0; i< no-1; i++)
{
for(j=0; j< no-i; j++)
{
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
printf("\nAfter sorting : ");
for(i=0; i< no; i++)
printf("%5d",b[i] );
}

Output:- Enter the no. of elements u want to fill: 5
Enter the array elements : 8  1  90  5  4
After sorting : 1  4  5  8  90

Monday, October 26, 2009

Write a C program to find gcd of each of the two consecutive elements of an array. write a function GCD(a,b) that would take two consecutive elements of the array as arguments and return the GCD.

#include(stdio.h) // Note place '<' & '>' in place of '(' & ')'
int gcd(int,int);
void main()
{
  int a[10],n,i,b[10];
  int x,y;
  clrscr();
  printf("Enter the size of the array:");
  scanf("%d",&n);
  printf("\nEnter the array elements:");
  for(i=0; i< n ;i++)
    scanf("%d",&a[i]);
 for(i=0; i< n-1 ;i++)
  {
     x=a[i];
     y=a[i+1];
     b[i]=gcd(x,y);
    printf("\nG.C.D of %d and %d is : %d",x,y,b[i]);
  }
getch();
}
int gcd(int a,int b)
{
  int rem;
  for(;;)
  {
    rem=a%b;
    if(rem==0)
       break;
    a=b;
    b=rem;
  }
 return b;
}

Friday, October 16, 2009

Write a C program to Create a Paint brush using graphics functions

#include(graphics.h) // note place '<' & '>' in place of '(' & ')'
#include(stdlib.h)
struct BYTEREGS
{
unsigned char al, ah, bl, bh;
unsigned char cl, ch, dl, dh;
};
struct WORDREGS
{
unsigned int ax, bx, cx, dx;
unsigned int si, di, cflag, flags;
};
union REGS
{
struct WORDREGS x;
struct BYTEREGS h;
};
void main()
{
int x,y,b,px,py,c,p,s,cl;
int d=0,m;
union REGS i,o;
initgraph(&d,&m,"c:\tc");
i.x.ax=1;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=20;
i.x.dx=450;
int86(0x33,&i,&o);
printf("Brush style insert number from 0 to 5 : ");
scanf("%d",&p);
printf("Brush size insert number from 1 to 7 : ");
scanf("%d",&s);
printf("Brush color insert number from 1 to 16 : ");
scanf("%d",&cl);
clrscr( );

cleardevice();
printf("\t\t**********DRAW IMAGE************");
while(!kbhit( ))
{
i.x.ax=3;
b=o.x.bx;
x=o.x.cx;
y=o.x.dx;
px=x;
py=y;
int86(0x33,&i,&o);
if(cl==16)
{
c=random(16);
}
else
{
c=cl;
}
setcolor(c);
if(b==1)
{
i.x.ax=3;
int86(0x33,&i,&o);
x=o.x.cx;
y=o.x.dx;
b=o.x.bx;
switch( p )
{
case 1: circle(px,py,s);break;
case 2: ellipse(px,py,0,270,s,s+2);break;
case 3: fillellipse(px,py,s+2,s);break;
case 4: rectangle(px,py,x,y);break;
case 5: sector(px,py,30,120,s,s);break;
default : line(px,py,x,y);
}
}
}
getch();
restorecrtmode();
closegraph();
}

Write a C program to display the message "Welcome to C" without a Semicolon.

// C program without a Semicolon.
This can done in three ways but one of them is infinite.(ie, while loop)
Solution:1
#include(stdio.h) // note place '<' & '>' in place of '(' & ')'
void main( )
{
if(printf("Welcome to C"))
{
}
}

Solution:2
void main( )
{
swicth(printf("Welcome to C"))
{
}
}

Solution:3
void main( )
{
while(printf("Welcome to C")) //infinite loop
{
}
}

Saturday, October 3, 2009

Write a C program to display the System date and Change the system date if necessary.

#include(stdio.h) // note use '<' & '>' in place of '(' & ')'
#include(conio.h)
#include(dos.h)
int main( )
{
int dd, mm, yy;
struct date s;
clrscr( );
getdate(&s);
printf("Present System Date ( dd/mm/yy ):-- %d / %d / %d ", s.da_day, s.da_mon, s.da_year);
printf("\nEnter the new date ( dd/mm/yy) :");
scanf("%d%d%d", &dd, &mm, &yy);
s.da_day=dd;
s.da_mon=mm;
s.da_year=yy;
setdate(&s);
printf("\n After changing, Present date (dd/mm/yy): %d / %d / %d", s.da_day, s.da_mon, s.da_year);
getch( );
return 0;
}

Output:- Present System Date ( dd/mm/yy): 04/10/2009
Enter the new date (dd/mm/yy): 10
10
2009
After Changing, Present date (dd/mm/yy): 10/10/2009

Write a C program to check whether the given number is single digit or more

#include(stdio.h) //note use '<' & '>' in place of '(' & ')'
#include(conio.h)
int main( )
{
int no;
clrscr( );
printf("Enter the required number to check:");
scanf("%d",&no);
if(no>=0 && no<=9)
printf("\nSingle Digit number");
else
printf("\nMore than one Digit number");
getch( );
return 0;
}

Output: Enter the required number to check: 9
Single Digit number

Write a program in c to accept any character and check whether the given character is capital or small using Ctype.h library file

// Using Library functions like isupper( ) or islower( )

#include(stdio.h) //note use '<' & '>' in place of '(' & ')'
#include(conio.h)
#include(ctype.h)
int main( )
{
char ch;
clrscr( );
printf("Enter any alphabet:");
scanf("%c", &ch);
if(isupper(ch))
printf("\nGiven Alphabet is in Upper Case");
else
printf("\nGiven Alphabet is in Lower case");
getch( );
return 0;
}

Output:- Enter any alphabet: a
Given Alphabet is in Lower case

Write a program in c to accept any character and check whether the given character is capital or small

// Method 1: Without Library functions
//using ASCII values

#include(stdio.h) //note use '<' & '>' in place of '(' & ')'
#include(conio.h)
int main( )
{
char ch;
clrscr( );
printf("Enter the any alphabet:");
scanf("%c ", &ch);
if(ch>= 65 && ch<=90) // or if(ch>='A' && ch<='Z')
printf("\nGiven Alphabet is in Upper case");
else
printf("\nGiven Alphabet is in Lower case");
getch( );
return 0;
}

Output:- Enter the any alphabet: A
Given Alphabet is in Upper case

Note :- ASCII value of 'A'=65 and 'Z'= 90