Earn by twitter on twivert

Sign up for PayPal and start accepting credit card payments instantly.

Thursday, September 30, 2010

WAP to calculate sum of x-[x3/fact(3)]+[x5/fact(5)]...n terms


/*WAP to calculate sum of x-[x3/fact(3)]+[x5/fact(5)]...n terms*/
 
#include<stdio.h>
#include<conio.h>
void main()
{
      int d=1,i,x,n,fact=1;
      float s=0;
      clrscr();    // clear screen.
      printf("\nEnter value of x and n:");
      scanf("%d%d",&x,&n);
      for(i=1;i<=n*2-1;i++)   // loop executes n+2 times
      {
            d=d*x;                 // find x,x^2,x^3...
            fact=fact*i;          // count factorial of i
            if(i%2!=0)
                 s=s+(float)d/fact; //count sum only when i is odd
            else
                 d=-d;             // if even then d becomes -ve.
      }
      printf("The Sum of Series Is :%0.2f",s); // print result
      getch();
} 

Wednesday, September 29, 2010

WAP to use argc and argv


/*WAP to use argc and argv*/
#include<stdio.h>
#include<conio.h>
main ( int  argc, char  *argv[ ] )
{
 FILE  *fs, *ft ;
 char  ch ;

 if ( argc != 3 )
 {
  puts ( "Improper number of arguments" ) ;
  exit( ) ;
 }

 fs = fopen ( argv[1], "r" ) ;
 if ( fs == NULL )
 {
  puts ( "Cannot open source file" ) ;
  exit( ) ;
 }

 ft = fopen ( argv[2], "w" ) ;
 if ( ft == NULL )
 {
  puts ( "Cannot open target file" ) ;
  fclose ( fs ) ;
  exit( ) ;
 }

 while ( 1 )
 {
  ch = fgetc ( fs ) ;

  if ( ch == EOF )
   break ;
  else
   fputc ( ch, ft ) ;
 }

 fclose ( fs ) ;
 fclose ( ft ) ;
       getch();
}

WAP to Prints file contents on printer


/*WAP to Prints file contents on printer*/


#include<stdio.h>
#include<conio.h>
main( )
{
 FILE  *fp ;
 char  ch ; 

 fp = fopen ( "poem.txt", "r" ) ;

 if ( fp == NULL )
 {
  printf ( "Cannot open file" ) ;
  exit( ) ;
 }

 while ( ( ch = fgetc ( fp ) ) != EOF )
  fputc ( ch, stdprn ) ;
  
 fclose ( fp ) ;
}

Monday, September 27, 2010

WAP to File-copy which copies text, .com and .exe files


/*WAP to File-copy which copies text, .com and .exe files */
#include "fcntl.h"
#include "types.h"  /* if present in sys directory use 
          "c:tc\\include\\sys\\types.h" */
#include "stat.h"  /* if present in sys directory use 
         "c:\\tc\\include\\sys\\stat.h" */

main ( int  argc, char  *argv[ ] )
{
 char  buffer[ 512 ], source [ 128 ], target [ 128 ] ;
 int  inhandle, outhandle, bytes ;

 printf ( "\nEnter source file name" ) ;
 gets ( source ) ;

 inhandle = open ( source, O_RDONLY | O_BINARY ) ;
 if ( inhandle == -1 )
 {
  puts ( "Cannot open file" ) ;
  exit( ) ;
 }

 printf ( "\nEnter target file name" ) ;
 gets ( target ) ;

 outhandle = open ( target, O_CREAT | O_BINARY | O_WRONLY,       S_IWRITE ) ;
 if ( inhandle == -1 )
 {
  puts ( "Cannot open file" ) ;
  close ( inhandle ) ;
  exit( ) ;
 }

 while ( 1 )
 {
  bytes = read ( inhandle, buffer, 512 ) ;

  if ( bytes > 0 )
   write ( outhandle, buffer, bytes ) ;
  else 
   break ;
 }

 close ( inhandle ) ;
 close ( outhandle ) ;
}

WAP of A menu-driven program for elementary database management


/*WAP of A menu-driven program for elementary database management */

#include<stdio.h>
#include<conio.h>
main( )
{
 FILE  *fp, *ft ;
 char  another, choice ; 
 struct emp 
 {
  char  name[40] ;
  int  age ;
  float  bs ;
 } ;
 struct emp  e ;
 char  empname[40] ;
 long int  recsize ;

 fp = fopen ( "EMP.DAT", "rb+" ) ;

 if ( fp == NULL )
 {
  fp = fopen ( "EMP.DAT", "wb+" ) ;

  if ( fp == NULL )
  {
   puts ( "Cannot open file" ) ;
   exit( ) ;
  }
 }

 recsize = sizeof ( e ) ;

 while ( 1 )
 {
  clrscr( ) ;

  gotoxy ( 30, 10 ) ;
  printf ( "1. Add Records" ) ;
  gotoxy ( 30, 12 ) ;
  printf ( "2. List Records" ) ;
  gotoxy ( 30, 14 ) ;
  printf ( "3. Modify Records" ) ;
  gotoxy ( 30, 16 ) ;
  printf ( "4. Delete Records" ) ;
  gotoxy ( 30, 18 ) ;
  printf ( "0. Exit" ) ;
  gotoxy ( 30, 20 ) ;
  printf ( "Your choice" ) ;

  fflush ( stdin ) ;
  choice = getche( ) ;
  switch ( choice )
  {
   case '1' :

    fseek ( fp, 0 , SEEK_END ) ;
    another = 'Y' ;

    while ( another == 'Y' )
    {
   printf ( "\nEnter name, age and basic sal. " ) ;
    scanf ( "%s %d %f", e.name, &e.age, &e.bs ) ;
     fwrite ( &e, recsize, 1, fp ) ; 
    printf ( "\nAdd another Record (Y/N) " ) ;
     fflush ( stdin ) ;
     another = getche( ) ;
    }

    break ;
   
   case '2' :

    rewind ( fp ) ;

    while ( fread ( &e, recsize, 1, fp ) == 1 )
   printf ( "\n%s %d %f", e.name, e.age, e.bs ) ; 

    break ;

   case '3' :

    another = 'Y' ;
    while ( another == 'Y' )
    {
   printf ( "\nEnter name of employee to modify " ) ;
     scanf ( "%s", empname ) ;

     rewind ( fp ) ;
    while ( fread ( &e, recsize, 1, fp ) == 1 )
     {   
     if ( strcmp ( e.name, empname ) == 0 )
      {
    printf ( "\nEnter new name, age & bs" ) ;
    scanf ( "%s %d %f", e.name, &e.age,&e.bs ) ;
     fseek ( fp, - recsize, SEEK_CUR ) ; 
      fwrite ( &e, recsize, 1, fp ) ; 
       break ; 
      }
     }

    printf ( "\nModify another Record (Y/N) " ) ;
     fflush ( stdin ) ;
     another = getche( ) ;
    }

    break ;
   
   case '4' :

    another = 'Y' ;
    while ( another == 'Y' )
    {
   printf ( "\nEnter name of employee to delete " ) ;
     scanf ( "%s", empname ) ;

     ft = fopen ( "TEMP.DAT", "wb" ) ;

     rewind ( fp ) ;
    while ( fread ( &e, recsize, 1, fp ) == 1 )
     {
     if ( strcmp ( e.name, empname ) != 0 )
      fwrite ( &e, recsize, 1, ft ) ; 
     }

     fclose ( fp ) ;
     fclose ( ft ) ;
     remove ( "EMP.DAT" ) ;
     rename ( "TEMP.DAT", "EMP.DAT" ) ;

     fp = fopen ( "EMP.DAT", "rb+" ) ;

    printf ( "Delete another Record(Y/N) " ) ;
     fflush ( stdin ) ;
     another = getche( ) ;
    }
    break ;

   case '0' :
    fclose ( fp ) ;
    exit( ) ;
  }
 }
 getch();
}

WAP to Reads records from binary file and displays them on VDU


/*WAP to Reads records from binary file and displays them on VDU */
#include "stdio.h"
main( )
{
 FILE  *fp ;
 struct emp 
 {
  char  name[40] ;
  int  age ;
  float  bs ; 
 } ;
 struct emp  e ;

 fp = fopen ( "EMP.DAT", "rb" ) ;

 if ( fp == NULL )
 {
  puts ( "Cannot open file" ) ;
  exit( ) ;
 }

 while ( fread ( &e, sizeof ( e ), 1, fp ) == 1 )
  printf ( "\n%s %d %f", e.name, e.age, e.bs ) ; 

 fclose ( fp ) ;
}

WAP to Receives records from keyboard and writes them to a file in binary mode


/*WAP to Receives records from keyboard and writes
them to a file in binary mode */

#include "stdio.h"
main( )
{
 FILE  *fp ;
 char  another = 'Y' ; 
 struct emp 
 {
  char  name[40] ;
  int  age ;
  float  bs ;
 } ;
 struct emp  e ;
 
 fp = fopen ( "EMP.DAT", "wb" ) ;

 if ( fp == NULL )
 {
  puts ( "Cannot open file" ) ;
  exit( ) ;
 }

 while ( another == 'Y' )
 {
  printf ( "\nEnter name, age and basic salary: " ) ;
  scanf ( "%s %d %f", e.name, &e.age, &e.bs ) ;
  fwrite ( &e, sizeof ( e ), 1, fp ) ; 

  printf ( "Add another record (Y/N) " ) ;
  fflush ( stdin ) ;
  another = getche( ) ;
 }

 fclose ( fp ) ;
}

WAP to Read records from a file using structure


/*WAP to Read records from a file using structure */
#include "stdio.h"
main( )
{
 FILE  *fp ;
 struct emp 
 {
  char  name[40] ;
  int  age ;
  float  bs ;
 } ;
 struct emp  e ;

 fp = fopen ( "EMPLOYEE.DAT", "r" ) ;

 if ( fp == NULL )
 {
  puts ( "Cannot open file" ) ;
  exit( ) ;
 }

 while ( fscanf ( fp, "%s %d %f", e.name, &e.age, &e.bs ) != EOF )
  printf ( "\n%s %d %f", e.name, e.age, e.bs ) ; 

 fclose ( fp ) ;
}

WAP to Receives strings from keyboard and writes them to file


/*WAP to Receives strings from keyboard and writes them to file*/
#include "stdio.h"
main( )
{
 FILE  *fp ;
 char  s[80] ;

 fp = fopen ( "file1.TXT", "w" ) ;
 if ( fp == NULL )
 {
  puts ( "Cannot open file" ) ;
  exit( ) ;
 }

 printf ( "\nEnter a few lines of text:\n" ) ;
 while ( strlen ( gets ( s ) ) > 0 )
 {
  fputs ( s, fp ) ;
  fputs ( "\n", fp ) ;
 }
 
 fclose ( fp ) ;
}

WAP to read a file and write(copy) data in another file


/*WAP to read a file and write(copy) data in another file*/
#include<stdio.h>
#include<conio.h>
main( )
{
 FILE  *fs, *ft ;
 char  ch ;

 fs = fopen ( "pr1.c", "r" ) ;
 if ( fs == NULL )
 {
  puts ( "Cannot open source file" ) ;
  exit( ) ;
 }

 ft = fopen ( "pr2.c", "w" ) ;
 if ( ft == NULL )
 {
  puts ( "Cannot open target file" ) ;
  fclose ( fs ) ;
  exit( ) ;
 }

 while ( 1 )
 {
  ch = fgetc ( fs ) ;

  if ( ch == EOF )
   break ;
  else
   fputc ( ch, ft ) ;
 }

 fclose ( fs ) ;
 fclose ( ft ) ;
       getch();
}

WAP to Count chars, spaces, tabs and newlines in a file


/*WAP to Count chars, spaces, tabs and newlines in a file*/
#include<stdio.h>
#include<conio.h>
main( )
{
 FILE  *fp ;
 char  ch ;
 int  nol = 0, not = 0, nob = 0, noc = 0 ;
 
 fp = fopen ( "PR1.C", "r" ) ;

 while ( 1 )
 {
  ch = fgetc ( fp ) ;

  if ( ch == EOF )
   break ;

  noc++ ;

  if ( ch == '  ' )
   nob++ ;

  if ( ch == '\n' )
   nol++ ;

  if ( ch == '\t' )
   not++ ;
 }

 fclose ( fp ) ;
 printf ( "\nNumber of characters = %d", noc ) ;
 printf ( "\nNumber of blanks = %d", nob ) ;
 printf ( "\nNumber of tabs = %d", not ) ;
 printf ( "\nNumber of lines = %d", nol ) ;
       getch();
}


WAP to check existing file empty or not


/*WAP to check existing file empty or not*/
#include<stdio.h>
#include<conio.h>
main( )
{
 FILE  *fp ;

 fp = fopen ( "PR1.C", "r" ) ;
 if ( fp == NULL )
 {
  puts ( "cannot open file" ) ;
  exit( ) ;
 }
       getch();
}

WAP to Display contents of a file on screen


/*WAP to Display contents of a file on screen*/
#include<stdio.h>
#include<conio.h>

main( )
{
 FILE  *fp ;
 char  ch ;
 
 fp = fopen ( "PR1.C", "r" ) ;

 while ( 1 )
 {
  ch = fgetc ( fp ) ;

  if ( ch == EOF )
   break ;

  printf ( "%c", ch ) ;
 }

 fclose ( fp ) ;
  getch();
}

WAP to that includes a method inside a class



/*WAP to that includes a method inside a class*/
class Box {
double width;
double height;
double depth;
// display volume of a box
void volume() {
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
class BoxDemo3 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// display volume of first box
mybox1.volume();
// display volume of second box
mybox2.volume();
}
}

WAP to declares two Box class objects




/*WAP to declares two Box class objects*/
class Box {
double width;
double height;
double depth;
}
class BoxDemo2 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// compute volume of first box
vol = mybox1.width * mybox1.height * mybox1.depth;
System.out.println("Volume is " + vol);
// compute volume of second box
vol = mybox2.width * mybox2.height * mybox2.depth;
System.out.println("Volume is " + vol);
}
}

WAP that calculate volume of Box


/*WAP that calculate volume of Box
Save as BoxDemo.java
*/
class Box {
double width;
double height;
double depth;
}
// This class declares an object of type Box.
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}

Friday, September 24, 2010

WAP to Passing address of a structure variable


/*WAP to Passing address of a structure variable */
#include<stdio.h>
#include<conio.h>
struct book 
{
 char  name[25] ;
 char  author[25] ;
 int  callno ;
} ;

main( )
{
 struct book  b1 = { "Let us C", "YPK", 101 } ;
 display ( &b1 ) ;
       getch();
}

display ( struct book  *b )
{
 printf ( "\n%s %s %d", b->name, b->author, b->callno ) ;
}

WAP to Passing individual structure elements


/*WAP to Passing individual structure elements */
#include<stdio.h>
#include<conio.h>
main( )
{
 struct book 
 {
  char  name[25] ;
  char  author[25] ;
  int  callno ;
 } ;
 struct book b1 = { "Let us C", "YPK", 101 } ;
 
 display ( b1.name, b1.author, b1.callno ) ;
 getch();
}

display ( char  *s, char  *t, int  n )
{
 printf ( "\n%s %s %d", s, t, n ) ;
}

WAP take people datail using structure


/*WAP take people datail using structure*/
#include<stdio.h>
#include<conio.h>
main( )
{
 struct address
 {
  char  phone[15] ;
  char  city[25] ;
  int  pin ;
 } ;

 struct emp
 {
  char  name[25] ;
  struct address  a ;
 } ;
 struct emp  e = { "jeru", "531046", "nagpur", 10 }; 

 printf ( "\nname = %s phone = %s", e.name, e.a.phone ) ;
 printf ( "\ncity = %s pin = %d", e.a.city, e.a.pin ) ;
       getch();
}

WAP Using a do-while loop to process a menu selection


/*WAP Using a do-while loop to process a menu selection
save file as: Menu.java
*/
class Menu {
public static void main(String args[])
throws java.io.IOException {
char choice;
do {
System.out.println("Help on:");
- 84 -
System.out.println(" 1. if");
System.out.println(" 2. switch");
System.out.println(" 3. while");
System.out.println(" 4. do-while");
System.out.println(" 5. for\\n");
System.out.println("Choose one:");
choice = (char) System.in.read();
} while( choice < '1' || choice > '5');
System.out.println("\\n");
switch(choice) {
case '1':
System.out.println("The if:\\n");
System.out.println("if(condition) statement;");
System.out.println("else statement;");
break;
case '2':
System.out.println("The switch:\\n");
System.out.println("switch(expression) {");
System.out.println(" case constant:");
System.out.println(" statement sequence");
System.out.println(" break;");
System.out.println(" // ...");
System.out.println("}");
break;
case '3':
System.out.println("The while:\\n");
System.out.println("while(condition) statement;");
break;
case '4':
System.out.println("The do-while:\\n");
System.out.println("do {");
System.out.println(" statement;");
System.out.println("} while (condition);");
break;
case '5':
System.out.println("The for:\\n");
System.out.print("for(init; condition; iteration)");
System.out.println(" statement;");
break;
}
}
}

WAP to An improved version of the season program


/*WAP to An improved version of the season program*/
class Switch {
public static void main(String args[]) {
int month = 4;
String season;
switch (month) {
case 12:
case 1:
case 2:
season = "Winter";
break;
case 3:
case 4:
case 5:
season = "Spring";
break;
case 6:
case 7:
case 8:
season = "Summer";
break;
case 9:
case 10:
case 11:
season = "Autumn";
break;
default:
season = "Bogus Month";
}
System.out.println("April is in the " + season + ".");
}
}

WAP to check that In a switch, break statements are optional


/*WAP to check that In a switch, break statements are optional*/
class MissingBreak {
public static void main(String args[]) {
for(int i=0; i<12; i++)
switch(i) {
case 0:
case 1:
case 2:
case 3:
case 4:
System.out.println("i is less than 5");
break;
case 5:
case 6:
case 7:
case 8:
case 9:
System.out.println("i is less than 10");
break;
default:
System.out.println("i is 10 or more");
}
}
}

Thursday, September 23, 2010

WAP to Usage of an array of structures


/*WAP to Usage of an array of structures */
#include<stdio.h>
#include<conio.h>
main( )
{
 struct book
 {
  char  name ;
  float  price ;
  int  pages ;
 } ;

 struct book  b[100] ;
 int  i ;

 for ( i = 0 ; i <= 99 ; i++ )
 {
  printf ( "\nEnter name, price and pages " ) ;
  scanf ( "%c %f %d", &b[i].name, &b[i].price, &b[i].pages ) ;
 }

 for ( i = 0 ; i <= 99 ; i++ )
  printf ( "\n%c %f %d", b[i].name, b[i].price, b[i].pages ) ;
       getch();
}

linkfloat( )
{
      float a = 0, *b ;
 b = &a ;  /* cause emulator to be linked */
      a = *b ;    /* suppress the warning - variable not used */
}

WAP to Memory map of structure elements


/*WAP to Memory map of structure elements */
#include<stdio.h>
#include<conio.h>
main( )
{
 struct book
 { 
  char  name ;
  float  price ;
  int  pages ;
 } ;
 struct book  b1 = { 'B', 130.00, 550 } ;
 
 printf ( "\nAddress of name = %u", &b1.name ) ;
 printf ( "\nAddress of price = %u", &b1.price ) ;
 printf ( "\nAddress of pages = %u", &b1.pages ) ;
       getch();
}

WAP to take book datails using structure


/*WAP to take book datails using structure*/
#include<stdio.h>
#include<conio.h>
main( )
{
 struct book
 { 
  char  name ;
  float  price ;
  int  pages ;
 } ;
 struct book  b1, b2, b3 ;

 printf ( "\nEnter names, prices & no. of pages of 3 books\n" ) ;
 scanf ( "%c %f %d", &b1.name, &b1.price, &b1.pages ) ;
 scanf ( "%c %f %d", &b2.name, &b2.price, &b2.pages ) ;
 scanf ( "%c %f %d", &b3.name, &b3.price, &b3.pages ) ;

 printf ( "\nAnd this is what you entered" ) ;
 printf ( "\n%c %f %d", b1.name, b1.price, b1.pages ) ;
 printf ( "\n%c %f %d", b2.name, b2.price, b2.pages ) ;
 printf ( "\n%c %f %d", b3.name, b3.price, b3.pages ) ;
       getch();
}

WAP to take books detail


/*WAP to take books detail*/
#include<stdio.h>
#include<conio.h>
main( )
{
 char  name[3] ;
 float  price[3] ;
 int  pages[3], i ;

 printf ( "\nEnter names, prices and no. of pages of 3 books\n" ) ;

 for ( i = 0 ; i <= 2 ; i++ )
  scanf ( "%c %f %d", &name[i], &price[i], &pages[i] );

 printf ( "\nAnd this is what you entered\n" ) ;
 for ( i = 0 ; i <= 2 ; i++ )
  printf ( "%c %f %d\n", name[i], price[i], pages[i] );
       getch();
}

Tuesday, September 21, 2010

WAP to Demonstrate switch statements


/*WAP to Demonstrate switch statements*/
class SampleSwitch {
public static void main(String args[]) {
for(int i=0; i<6; i++)
switch(i) {
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 3.");
}
}
}

WAP to Demonstrate if-else-if statements


/*WAP to Demonstrate if-else-if statements*/
class IfElse {
public static void main(String args[]) {
int month = 4; // April
String season;
if(month == 12 || month == 1 || month == 2)
season = "Winter";
else if(month == 3 || month == 4 || month == 5)
season = "Spring";
else if(month == 6 || month == 7 || month == 8)
season = "Summer";
else if(month == 9 || month == 10 || month == 11)
season = "Autumn";
else
season = "Bogus Month";
System.out.println("April is in the " + season + ".");
}
}

WAP to Demonstrate ? : Ternary operator


/*WAP to Demonstrate ? : Ternary operator*/
class Ternary {
public static void main(String args[]) {
int i, k;
i = 10;
k = i < 0 ? -i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
i = -10;
k = i < 0 ? -i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
}
}

WAP to Demonstrate the boolean logical operators


/*WAP to Demonstrate the boolean logical operators*/
class BoolLogic {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
boolean c = a | b;
boolean d = a & b;
boolean e = a ^ b;
boolean f = (!a & b) | (a & !b);
boolean g = !a;
System.out.println(" a = " + a);
System.out.println(" b = " + b);
System.out.println(" a|b = " + c);
System.out.println(" a&b = " + d);
System.out.println(" a^b = " + e);
System.out.println("!a&b|a&!b = " + f);
System.out.println(" !a = " + g);
}
}

WAP of bit


/*WAP of bit*/
class OpBitEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3;
a |= 4;
b >>= 1;
c <<= 1;
a ^= c;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}

Monday, September 20, 2010

WAP to use array of pointers and Use of malloc


/*WAP to use array of pointers */
/* and Use of malloc */

#include<stdio.h>
#include<conio.h>
#include "alloc.h"
main( )
{
 char  *names[6] ;
 char n[50] ;
 int  len, i ;
 char *p ;

 for ( i = 0 ; i <= 5 ; i++ )
 {
  printf ( "\nEnter name " ) ;
  scanf ( "%s", n ) ;
  len = strlen ( n ) ;
  p = malloc ( len + 1 ) ;
  strcpy ( p, n ) ;
  names[i] = p ;
 }

 for ( i = 0 ; i <= 5 ; i++ )
  printf ( "\n%s", names[i] ) ;
}

WAP to interchange string3 with string4


/*WAP to interchange string3 with string4*/
#include<stdio.h>
#include<conio.h>
main( )
{
 char  *names[ ] = {
      "akshay",
      "parag",
      "raman",
      "srinivas",
      "gopal",
      "rajesh"
   } ;
 char  *temp ;

 printf ( "Original: %s %s", names[2], names[3] ) ;

 temp = names[2] ;
 names[2] = names[3] ;
 names[3] = temp ;

 printf ( "\nNew: %s %s", names[2], names[3] ) ;
}

WAP to Exchange names using 2-D array of characters


/*WAP to Exchange names using 2-D array of characters */
#include<stdio.h>
#include<conio.h>
main( )
{
 char  names[ ][10] = {
      "akshay",
      "parag",
      "raman",
      "srinivas",
      "gopal",
      "rajesh"
        } ;
 int  i ;
 char  t ;

 printf ( "\nOriginal: %s %s", &names[2][0], &names[3][0] ) ;  

 for ( i = 0 ; i <= 9 ; i++ )
 {
  t = names[2][i] ;
  names[2][i] = names[3][i] ;
  names[3][i] = t ;
 }

 printf ( "\nNew: %s %s", &names[2][0], &names[3][0] ) ;
}

WAP to search a string in two dimensional array


/*WAP to search a string in two dimensional array */
#include<stdio.h>
#include<conio.h>
#define FOUND 1
#define NOTFOUND 0
main( )
{
 char  masterlist[6][10] = {
       "akshay",
       "parag",
       "raman",
       "srinivas",
       "gopal",
       "rajesh"
         } ;
 int  i, flag, a ;
 char  yourname[10] ;
 printf ( "\nEnter your name " ) ;
 scanf ( "%s", yourname ) ;

 flag = NOTFOUND ;
 for ( i = 0 ; i <= 5 ; i++ )
 {
  a = strcmp ( &masterlist[i][0], yourname ) ;
  if ( a == 0 )
  {
   printf ( "Welcome, you can enter the palace" ) ;
   flag = FOUND ;
   break ;
  }
 }

 if ( flag == NOTFOUND )
  printf ( "Sorry, you are a trespasser" ) ;
}

WAP to Use of strcmp() function


/*WAP to Use of strcmp() function */
#include<stdio.h>
#include<conio.h>
main( )
{
 char  string1[ ] = "Jerry" ;
 char  string2[ ] = "Ferry" ;
 int  i, j, k ;

 i = strcmp ( string1, "Jerry" ) ;
 j = strcmp ( string1, string2 ) ;
 k = strcmp ( string1, "Jerry boy" ) ;

 printf ( "\n%d %d %d", i, j, k ) ;
}

WAP to Use of strcat() function


/*WAP to Use of strcat() function */
#include<stdio.h>
#include<conio.h>
main( )
{
 char  source[ ] = "Folks!" ;
 char  target[30] = "Hello" ;

 strcat ( target, source ) ;
 printf ( "\nsource string = %s", source ) ;
 printf ( "\ntarget string = %s", target ) ;
}

WAP to Use of const keyword


/*WAP to Use of const keyword */
#include<stdio.h>
#include<conio.h>
main( )
{
 float r, a ;
 const float pi = 3.14 ;

 printf ( "\nEnter radius of circle " ) ;
 scanf  ( "%f", &r ) ;
 a = pi * r * r ;
 printf ( "\nArea of circle = %f", a ) ;
}

WAP to copy string without changing source string


/*WAP to copy string without changing source string */
#include<stdio.h>
#include<conio.h>
main( )
{
 char  source[ ] = "codezworld" ;
 char  target[20] ;

 xstrcpy ( target, source ) ;
 printf ( "\nsource string = %s", source ) ;
 printf ( "\ntarget string = %s", target ) ;
}

void xstrcpy ( char *t, const char *s )
{
 while ( *s != '\0' )
 {
  *t = *s ;
  s++ ;
  t++ ;
 }
 *t = '\0' ;
}

Sunday, September 19, 2010

WAP that look-alike of the function strcopy( )


/*WAP that look-alike of the function strcopy( ) */
#include<stdio.h>
#include<conio.h>
main( )
{
 char  source[ ] = "codezworld" ;
 char  target[20] ;

 xstrcpy ( target, source ) ;
 printf ( "\nsource string = %s", source ) ;
 printf ( "\ntarget string = %s", target ) ;
}

xstrcpy ( char  *t, char  *s )
{
 while ( *s != '\0' )
 {
  *t = *s ;
  s++ ;
  t++ ;
 }
 *t = '\0' ;
}

WAP to Use of string copy strcopy() function


/*WAP to Use of string copy strcopy() function */
#include<stdio.h>
#include<conio.h>
main( )
{
 char  source[ ] = "codezworld" ;
 char  target[20] ;

 strcpy ( target, source ) ;
 printf ( "\nsource string = %s", source ) ;
 printf ( "\ntarget string = %s", target ) ;
}

WAP that look-alike of the function strlen( )


/*WAP that look-alike of the function strlen( ) */
#include<stdio.h>
#include<conio.h>
main( )
{
 char  arr[ ] = "codezworld" ;
 int  len1, len2 ;

 len1 = xstrlen ( arr ) ;
 len2 = xstrlen ( "A blog of programs" ) ;

 printf ( "\nstring = %s length = %d", arr, len1 ) ;
 printf ( "\nstring = %s length = %d", "A blog of programs", len2 ) ;
}

xstrlen ( char  *s )
{
 int  length = 0 ;

 while ( *s != '\0' )
 {
  length++ ;
  s++ ;
 }

 return ( length ) ;
} 

WAP to find length of string using strlen() function


/*WAP to find length of string using strlen() function */
#include<stdio.h>
#include<conio.h>
main( )
{
 char  arr[ ] = "codezworld" ;
 int  len1, len2 ;

 len1 = strlen ( arr ) ;
 len2 = strlen ( "A blog of programs" ) ;

 printf ( "\nstring = %s length = %d", arr, len1 ) ;
 printf ( "\nstring = %s length = %d", "A blog of programs", len2 ) ;
}

WAP to Input of strings containing spaces


/*WAP to Input of strings containing spaces */
#include<stdio.h>
#include<conio.h>
main( ) 
{
 char  name[25] ;

 printf ( "Enter your full name " ) ;
 gets ( name ) ;
 puts ( "Hello!" ) ;
 puts ( name ) ;
}

WAP to input & print a string


/*WAP to input & print a string */
#include<stdio.h>
#include<conio.h>
main( )
{
 char  name[25] ;
 printf ( "Enter your name " ) ;
 scanf ( "%s", name ) ;
 printf ( "Hello %s!", name ) ;
}

WAP to Unsigned shifting a byte value


/*WAP to Unsigned shifting a byte value.
Save As: ByteUShift.java
*/
class ByteUShift {
static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xf1;
byte c = (byte) (b >> 4);
byte d = (byte) (b >>> 4);
byte e = (byte) ((b & 0xff) >> 4);
System.out.println(" b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
System.out.println(" b >> 4 = 0x" + hex[(c >> 4) & 0x0f] + hex[c & 0x0f]);
System.out.println(" b >>> 4 = 0x" + hex[(d >> 4) & 0x0f] + hex[d & 0x0f]);
System.out.println("(b & 0xff) >> 4 = 0x" + hex[(e >> 4) & 0x0f] + hex[e & 0x0f]);
}
}

WAP to Masking sign extension


/*WAP to Masking sign extension.
Save As : HexByte.java
*/
class HexByte {
static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xf1;
System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b &
0x0f]);
}
}

WAP to Left shifting as a quick way to multiply by 2


/*WAP to Left shifting as a quick way to multiply by 2.
Save As: MultByTwo.java
*/
class MultByTwo {
public static void main(String args[]) {
int i;
int num = 0xFFFFFFE;
for(i=0; i<4; i++) {
num = num << 1;
System.out.println(num);
}
}
}

WAP to Left shifting a byte value


/*WAP to Left shifting a byte value.
Save As ByteShift.java
*/
class ByteShift {
public static void main(String args[]) {
byte a = 64, b;
int i;
i = a << 2;
b = (byte) (a << 2);
System.out.println("Original value of a: " + a);
System.out.println("i and b: " + i + " " + b);
}
}

WAP to Demonstrate the bitwise logical operators


/*WAP to Demonstrate the bitwise logical operators
Save As: BitLogic.java
*/
class BitLogic {
public static void main(String args[]) {
String binary[] = {
"0000", "0001", "0010", "0011", "0100", "0101", "0110",
"0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110",
"1111"
};
int a = 3; // 0 + 2 + 1 or 0011 in binary
int b = 6; // 4 + 2 + 0 or 0110 in binary
int c = a | b;
int d = a & b;
int e = a ^ b;
int f = (~a & b) | (a & ~b);
int g = ~a & 0x0f;
System.out.println(" a = " + binary[a]);
System.out.println(" b = " + binary[b]);
System.out.println(" a|b = " + binary[c]);
System.out.println(" a&b = " + binary[d]);
System.out.println(" a^b = " + binary[e]);
System.out.println("~a&b|a&~b = " + binary[f]);
System.out.println(" ~a = " + binary[g]);
}
}

WAp to Demonstrate ++ operator


/*WAp to Demonstrate ++ operator
Save as: IncDec.java
*/
class IncDec {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}

WAP to Demonstrate several assignment operators


/*WAP to Demonstrate several assignment operators*/
class OpEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3;
a += 5;
b *= 4;
c += a * b;
c %= 6;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}

WAP to Demonstrate the % operator


/*WAP to Demonstrate the % operator*/
class Modulus {
public static void main(String args[]) {
int x = 42;
double y = 42.3;
System.out.println("x mod 10 = " + x % 10);
System.out.println("y mod 10 = " + y % 10);
}
}

Saturday, September 18, 2010

WAP to Using pointer to access array elements


/*WAP to Using pointer to access array elements */
#include<stdio.h>
#include<conio.h>
main( )
{
 char  name[ ] = "Goldfish" ;
 char  *ptr ;

 ptr = name ;  /* store base address of string */

 while ( *ptr != `\0' )
 {
  printf ( "%c", *ptr ) ;
  ptr++ ;
 }
}

WAP to demonstrate printing of string 2


/*WAP to demonstrate printing of string 2*/
#include<stdio.h>
#include<conio.h>
main( )
{
 char  name[ ] = "Klinsman" ;
 int  i = 0 ;

 while ( name[i] != `\0' )
 {
  printf ( "%c", name[i] ) ;
  i++ ;
 }
}

WAP to demonstrate printing of a string


/*WAP to demonstrate printing of a string */
#include<stdio.h>
#include<conio.h>
main( )
{
 char  name[ ] = "Goldfish" ;
 int  i = 0 ;

 while ( i <= 9 )
 {
  printf ( "%c", name[i] ) ;
  i++ ;
 }
}

WAP to Usage of pointer to an array


/*WAP to Usage of pointer to an array */
#include<stdio.h>
#include<conio.h>
main( )
{
 int  s[5][2] = {
     { 1234, 56 },
     { 1212, 33 },
     { 1434, 80 },
     { 1312, 78 }
       } ;  
 int  ( *p )[2] ;
 int  i, j, *pint ;
 
 for ( i = 0 ; i <= 3 ; i++ )
 {
   p = &s[i] ;
  pint = p ;
  printf ( "\n" ) ;
  for ( j = 0 ; j <= 1 ; j++ )
   printf ( "%d ", *( pint + j ) ) ; 
 }
}

WAP to Accessing array elements in different ways


/*WAP to Accessing array elements in different ways */
#include<stdio.h>
#include<conio.h>
main( )
{
 int  num[ ] = { 24, 34, 12, 44, 56, 17 } ;
 int  i ;

 for ( i = 0 ; i <= 5 ; i++ )
 {
  printf ( "\naddress = %u ", &num[i] ) ;
  printf ( "element = %d %d ", num[i], *( num + i ) ) ; 
  printf ( "%d %d", *( i + num ), i[num] ) ;
 }
}

Thursday, September 16, 2010

WAP to access array elements


/*WAP to access array elements */
#include<stdio.h>
#include<conio.h>
main( )
{
 int  num[ ] = { 24, 34, 12, 44, 56, 17 } ;
 int  i ;

 for ( i = 0 ; i <= 5 ; i++ )
 {
  printf ( "\naddress = %u ", &num[i] ) ;
  printf ( "element = %d", num[i] ) ;
 }
}

WAP to print out the memory locations


/*WAP to print out the memory locations*/
#include<stdio.h>
#include<conio.h>
main( )
{
 int  num[ ] = { 24, 34, 12, 44, 56, 17 } ;
 int  i ;

 for ( i = 0 ; i <= 5 ; i++ )
 {
  printf ( "\nelement no. %d ", i ) ;
  printf ( "address = %u", &num[i] ) ;
 }
}

WAP to Comparison of two pointer variables


/*WAP to Comparison of two pointer variables */
#include<stdio.h>
#include<conio.h>
main( )
{
 int  arr[ ] = { 10, 20, 36, 72, 45, 36 } ;
 int  *j, *k ;

 j = &arr [ 4 ] ;
 k = ( arr + 4 ) ;

 if ( j == k )
  printf ( "The two pointers point to the same location" ) ;
 else
  printf ( "The two pointers do not point to the same location" ) ;
}

WAP to Subtraction of one pointer from another


/*WAP to Subtraction of one pointer from another*/
#include<stdio.h>
#include<conio.h>
main( )
{
 int  arr[ ] = { 10, 20, 30, 45, 67, 56, 74 } ;
 int  *i, *j ;

 i = &arr[1] ;
 j = &arr[5] ;
 printf ( "%d %d", j - i, *j - *i ) ;
}

WAP to Introducing pointers


/*WAP to Introducing pointers */
#include<stdio.h>
#include<conio.h>
main( )
{
 int  i = 3, *x ;
 float  j = 1.5, *y ;
 char  k = 'c', *z ;

 printf ( "\nValue of i = %d", i ) ;
 printf ( "\nValue of j = %f", j ) ;
 printf ( "\nValue of k = %c", k ) ;
 x = &i ;
 y = &j ;
 z = &k ;
 printf ( "\nOriginal address in x = %u", x ) ;
 printf ( "\nOriginal address in y = %u", y ) ;
 printf ( "\nOriginal address in z = %u", z ) ;
 x++ ;
 y++ ;
 z++ ;
 printf ( "\nNew address in x = %u", x ) ;
 printf ( "\nNew address in y = %u", y ) ;
 printf ( "\nNew address in z = %u", z ) ;
}


WAP to pass an array to a function by address


/*WAP to pass an array to a function by address*/
#include<stdio.h>
#include<conio.h>
main( )
{
 int  i ;
 int  marks[ ] = { 55, 65, 75, 56, 78, 78, 90 } ;

 for ( i = 0 ; i <= 6 ; i++ )
  disp ( &marks[i] ) ;
}

disp ( int  *n )
{
 show ( &n ) ;
}

show ( int  *m )
{
 printf("%d  ",*m)
}

WAP to Demonstration of call by value


/*WAP to Demonstration of call by value*/
#include<stdio.h>
#include<conio.h>
main( )
{
 int  i ;
 int  marks[ ] = { 55, 65, 75, 56, 78, 78, 90 } ;

 for ( i = 0 ; i <= 6 ; i++ )
  display ( marks[i] ) ;
} 

display ( int  m )
{
 printf ( "%d ", m ) ;
}

Wednesday, September 15, 2010

WAP to Demonstrate the basic arithmetic operators


/*WAP to Demonstrate the basic arithmetic operators*/
class BasicMath {
public static void main(String args[]) {
// arithmetic using integers
System.out.println("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
// arithmetic using doubles
System.out.println("\\nFloating Point Arithmetic");
double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
double dd = dc - a;
double de = -dd;
System.out.println("da = " + da);
System.out.println("db = " + db);
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);
}
}

WAP to Demonstrate a three-dimensional array


/*WAP to Demonstrate a three-dimensional array*/
class threeDMatrix {
public static void main(String args[]) {
int threeD[][][] = new int[3][4][5];
int i, j, k;
for(i=0; i<3; i++)
for(j=0; j<4; j++)
for(k=0; k<5; k++)
threeD[i][j][k] = i * j * k;
for(i=0; i<3; i++) {
for(j=0; j<4; j++) {
for(k=0; k<5; k++)
System.out.print(threeD[i][j][k] + " ");
System.out.println();
}
System.out.println();
}
}
}

Tuesday, September 14, 2010

WAP to Use of # pragma 2


/*WAP to Use of # pragma */
/* Pogram to supress specific warning */
#include<stdio.h>
#include<conio.h>
#pragma warn -rvl /* return value */
#pragma warn -par /* parameter not used */ 
#pragma warn -rch /* unreachable code */

int  f1( )
{
 int  a = 5 ;
}

void  f2 ( int  x )
{
 printf ( "\nInside f2" ) ;
}

int  f3( )
{
 int  x = 6 ;
 return x ;
 x++  ;
}

void  main( )
{
 f1( ) ;
 f2 (  7 ) ;
 f3( ) ;
 getch();
}

WAP to Use of #pragma


/*WAP to Use of #pragma */
/* Program to change execution of functions */
#include<stdio.h>
#include<conio.h>
void fun1( ) ;
void fun2( ) ;

#pragma startup fun1
#pragma exit fun2

main( )
{
 printf ( "\nInside maim" ) ;
 getch();
}

void fun1( )
{
 printf ( "\nInside fun1" ) ;
}

void fun2( )
{
 printf ( "\nInside fun2" ) ;
}

WAP to Multiple line in macros expansion


/*WAP to Multiple line in macros expansion*/
/* Program to draw horizontal & vertical line */
#include<stdio.h>
#include<conio.h>
#define HLINE   for ( i = 0 ; i < 79 ; i++ ) \  
                             printf ( "%c", 196 ) ;

#define VLINE( X, Y )  {\gotoxy ( X, Y ) ; \
       printf ( "%c", 179 ) ; \
     }
main( )
{
 int  i, y ;
 clrscr( ) ;

 gotoxy ( 1, 12 ) ;
 HLINE

 for ( y = 1 ; y < 25 ; y++ )
  VLINE ( 39, y ) ;
 getch();
}


WAP to Macros expansion with arguments


/*WAP to Macros expansion with arguments */
/* Program to test whether entered character is digit */
#define ISDIGIT(y) ( y >= 48 && y <= 57 )
main( )
{
 char  ch ;

 printf ( "Enter any digit " ) ;
 scanf ( "%c", &ch ) ;

 if ( ISDIGIT ( ch ) )
  printf ( "\nYou entered a digit" ) ;
 else
  printf ( "\nIllegal input" ) ;
       getch();
} 


WAP to use Macros expansion with arguments


/*WAP to use Macros expansion with arguments */
#include<stdio.h>
#include<conio.h>
#define AREA(x) ( 3.14 * x * x )
main( )
{
 float  r1 = 6.25, r2 = 2.5, a ;
 
 a = AREA ( r1 ) ;
 printf ( "\nArea of circle = %f", a ) ;
 a = AREA ( r2 ) ;
 printf ( "\nArea of circle = %f", a ) ;
       getch();
}

WAP to # define statement used to replace entire C statement


/*WAP to # define statement used to replace entire C statement */
#include<stdio.h>

#define FOUND printf ( "The Yankee Doodle Virus" ) ;
main( )
{
 char  signature ;

 if ( signature == 'Y' ) 
  FOUND
 else
  printf ( "Safe... as yet !" ) ;
 getch();
}


WAP to Nested macro expansion


/*WAP to Nested macro expansion*/
#include<stdio.h>
#include<conio.h>
#define AND &&
#define ARANGE ( a > 25 AND a < 50 )
main( )
{
 int  a = 30 ;

 if ( ARANGE )
  printf ( "within range" ) ;
 else
  printf ( "out of range" ) ;
 getch();
}

WAP to use Macro expansion Logical operator


/*WAP to use Macro expansion Logical operator*/
#include<stdio.h>
#include<conio.h>
#define AND &&
#define OR  ||
main( )
{
 int  f = 1, x = 4, y = 90 ;

 if ( ( f < 5 ) AND ( x <= 20 OR y <= 45 ) )
  printf ( "\nYour PC will always work fine..." ) ;
 else
  printf ( "\nIn front of the maintenance man" ) ;
 getch();
}

WAP to Use of macro expansion2


/*WAP to Use of macro expansion*/
#include<stdio.h>
#include<conio.h>
#define PI  3.1415
main( )
{
 float  r = 6.25 ;
 float  area ;

 area = PI * r * r ;
 printf ( "\nArea of circle = %f", area ) ;
 getch();
}

WAP to Use of macro expansion


/*WAP to Use of macro expansion*/
#include<stdio.h>
#include<conio.h>
#define UPPER 25
main( )
{
 int  i ;
 for ( i = 1 ; i <= UPPER ; i++ )
  printf ( "\n%d", i ) ;
}

WAP to Use of continue keyword in for loop


/*WAP to Use of continue keyword in for loop */
#include<stdio.h>
#include<conio.h>
main( )
{
 int   i, j ;

 for ( i = 1 ; i <= 2 ; i++ )
 {
  for ( j = 1 ; j <= 2 ; j++ )
  {
   if ( i == j )
    continue ;

   printf ( "\n%d %d\n", i, j ) ;
  }
 }
 getch();
}

Execution of a loop an unknown number of times


/* Execution of a loop an unknown number of times */
#include<stdio.h>
#include<conio.h>
main( )
{
 char  another ;
 int  num ;
 do
 {
  printf ( "Enter a number " ) ;
  scanf ( "%d", &num ) ;
  printf ( "square of %d is %d", num, num * num ) ;
  printf ( "\nWant to enter another number y/n " ) ;
  scanf ( " %c", &another ) ;
 } while ( another == 'y' ) ;
 getch();
}


WAP to Demonstration of nested loops


/*WAP to Demonstration of nested loops */
#include<stdio.h>
#include<conio.h>
main( )
{
 int   r, c, sum ;
 for ( r = 1 ; r <= 3 ; r++ )  /* outer loop */
 {
  for ( c = 1 ; c <= 2 ; c++ )  /* inner loop */
  {
   sum = r + c ;
   printf ( "r = %d c = %d sum = %d\n", r, c, sum ) ;
  }
 }
 getch();
}


WAP to Shape


/*WAP to Shape*/
#include<stdio.h>
#include<conio.h> 
void main()
{
int i,j,n;
clrscr();  //clear screen

for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
printf("%d ",i);
printf("\n");
}
getch();
}

WAP to shap


/*WAP to shap3*/
#include<stdio.h>
#include<conio.h> 
void main()
{
 int i,j;
clrscr();   //clear screen
for(i=5;i>=1;i--)   //loop starts from n for row
{
for(j=1;j<=i;j++)  // loop executes i times for colom
printf("%d",j);   // print j
printf("\n");   // goto next line
}
getch();
}

WAP to Demonstrate casts


/*WAP to Demonstrate casts*/
class Conversion {
public static void main(String args[]) {
byte b;
int i = 257;
double d = 323.142;
System.out.println("\\nConversion of int to byte.");
b = (byte) i;
System.out.println("i and b " + i + " " + b);
System.out.println("\\nConversion of double to int.");
i = (int) d;
System.out.println("d and i " + d + " " + i);
System.out.println("\\nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);
}
getch();
}

WAP to Demonstrate dynamic initialization


/*WAP to Demonstrate dynamic initialization*/
class DynInit {
public static void main(String args[]) {
double a = 3.0, b = 4.0;
// c is dynamically initialized
double c = Math.sqrt(a * a + b * b);
System.out.println("Hypotenuse is " + c);
}
getch();
}

WAP to Demonstrate boolean values


/*WAP to Demonstrate boolean values*/
class BoolTest {
public static void main(String args[]) {
boolean b;
b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);
// a boolean value can control the if statement
if(b) System.out.println("This is executed.");
b = false;
if(b) System.out.println("This is not executed.");
// outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));
}
}

Monday, September 13, 2010

WAP ot calculate salary of an employee


/*WAP ot calculate salary of an employee */
#include<stdio.h>
#include<conio.h>
main( )
{
 char   g ;
 int   yos, qual, sal ;

 printf ( "Enter Gender, Years of Service and 
   Qualifications ( 0 = G, 1 = PG ):" ) ;
 scanf ( "%c%d%d", &g, &yos, &qual ) ;

 if ( g == 'm' && yos >= 10 && qual == 1 )
  sal = 15000 ;
 else if ( ( g == 'm' && yos >= 10 && qual == 0 ) || 
  ( g == 'm' && yos < 10 && qual == 1 ) )
  sal = 10000 ;
 else if ( g == 'm' && yos < 10 && qual == 0 )
  sal = 7000 ;
 else if ( g == 'f' && yos >= 10 && qual == 1 )
  sal = 12000 ;
 else if ( g == 'f' && yos >= 10 && qual == 0 )
  sal = 9000 ;
 else if ( g == 'f' && yos < 10 && qual == 1 )
  sal = 10000 ; 
 else if ( g == 'f' && yos < 10 && qual == 0 )
  sal = 6000 ;

 printf ( "\nSalary of Employee = %d", sal ) ;
 getch();
}

Insurance of driver - using logical operators - 2


/* Insurance of driver - using logical operators */
#include<stdio.h>
#include<conio.h>
main( )
{
 char   sex, ms ;
 int   age ;

 printf ( "Enter age, sex, marital status " ) ;
 scanf ( "%d %c %c" &age, &sex, &ms ) ; 

 if ( ( ms == 'M') || ( ms == 'U' && sex == 'M' && age > 30 ) || 
    ( ms == 'U' && sex == 'F' && age > 25 ) )
  printf ( "Driver is insured" ) ;
 else
  printf ( "Driver is not insured" ) ;
 getch();
}

Insurance of driver - without using logical operators


/* Insurance of driver - without using logical operators */
#include<stdio.h>
#include<conio.h>
main( )
{
 char   sex, ms ;
 int   age ;

 printf ( "Enter age, sex, marital status " ) ;
 scanf ( "%d %c %c", &age, &sex, &ms ) ;

 if ( ms == 'M' ) 
  printf ( "Driver is insured" ) ;
 else
 {
  if ( sex == 'M' )
  {
   if ( age > 30 )
    printf ( "Driver is insured" ) ;
   else 
    printf ( "Driver is not insured" ) ;
  }
  else
  {
   if ( age > 25 )
    printf ( "Driver is insured" ) ;
   else 
    printf ( "Driver is not insured" ) ;
  }
 }
 getch();
}

WAP of nested if-else


/*WAP of nested if-else */
#include<stdio.h>
#include<conio.h>
main( )
{
 int   i ;

 printf ( "Enter either 1 or 2 " ) ;
 scanf ( "%d", &i ) ;

 if ( i == 1 )
  printf ( "You would go to heaven !" ) ;
 else
 {
  if ( i == 2 )
   printf ( "Hell was created with you in mind" ) ;
  else
   printf ( "How about mother earth !" ) ;
 }
getch();
}

WAP to Calculation of gross salary


/*WAP to Calculation of gross salary */
#include<stdio.h>
#include<conio.h>
main( )
{
 float   bs, gs, da, hra ;

 printf ( "Enter basic salary " ) ;
 scanf ( "%f", &bs ) ;

 if ( bs < 1500 )
 {
  hra = bs * 10 / 100 ;
  da = bs * 90 / 100 ;
 }
 else
 {
  hra = 500 ;
  da = bs * 98 / 100 ;
 }

 gs = bs + hra + da ;
 printf ( "gross salary = Rs. %f", gs ) ;
 getch();
} 

Calculation of bonus


/* Calculation of bonus */
#include<stdio.h>
#include<conio.h>
main( )
{
 int   bonus, cy, yoj, yr_of_ser ;

 printf ( "Enter current year and year of joining " ) ;
 scanf ( "%d %d", &cy, &yoj ) ;

 yr_of_ser = cy - yoj ;

 if ( yr_of_ser > 3 )
 {
  bonus = 2500 ;
  printf ( "Bonus = Rs. %d", bonus ) ;
 }
}

WAP to Demonstration of if statement


/*WAP to Demonstration of if statement */
#include<stdio.h>
#include<conio.h>
main( )
{
 int   num ;

 printf ( "Enter a number less than 10 " ) ;
 scanf ( "%d", &num ) ;

 if ( num <= 10 )
  printf ( "Entered number is less than 10=%d",num) ;
 getch();
}

WAP to use Objects are passed by reference


/*WAP to use Objects are passed by reference*/
class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
// pass an object
void meth(Test o) {
o.a *= 2;
o.b /= 2;
}
}
class CallByRef {
public static void main(String args[]) {
Test ob = new Test(15, 20);
System.out.println("ob.a and ob.b before call: " +
ob.a + " " + ob.b);
ob.meth(ob);
System.out.println("ob.a and ob.b after call: " +
ob.a + " " + ob.b);
}
}

Sunday, September 12, 2010

WAP to Calculation of simple interest


/*WAP to Calculation of simple interest */
#include<stdio.h>
#include<conio.h>
main( )
{
 int   p, n ;
 float   r, si ; 

 p = 1000 ;
 n = 3 ;
 r = 4.5 ;

 /* formula for simple interest */
 si = p * n * r / 100 ;  

 printf ( "%f" , si ) ;
} 

Addition of two equations-FUN


Q: Addition of two equations.
equ1- Study=Don't Fail
equ2- Don't Study=Fail
Ans: Study+Don't Study = Fail+Don't Fail
=>Study(1+Don't)=Fail(1+Don't)
=>Study=Fail

WAP to Convert Decimal to Binary


/*Convert Decimal to Binary*/
#include<stdio.h>
#include<conio.h>
void main()
{
 clrscr();
 int a,b,x,y,i[5],j,r;
 printf("Enter  no.");
 scanf("%d",&a);
 x=a%2;
 y=a/2;
 b=(y*2)+(x);
 for(j=0;b!=0;b/=2)
 {
  i[j]=b%2;
  j++;
 }
 for(r=j-1;r>=0;r--)
 {
  printf("%d",i[r]);
 }
 getch();
}

Saturday, September 11, 2010

Write a program to convert Octal to Binary


/*Write a program to convert Octal to Binary*/
#include<stdio.h>

#include<conio.h>

void main()

{

 clrscr();

 int a,b;

 printf("Enter 2 digit octal no.");

 scanf("%d",&a);

 int x,y;

 x=a%10;

 y=a/10;

 b=(y*8)+(x);//changing no. to decimal

 int i[5];

 for(int j=0;b!=0;b/=2)

 {



  i[j]=b%2;

  j++;

 }

 for(int r=j-1;r>=0;r--)

 {

  printf("%d",i[r]);
 }
 getch();
}

Thursday, September 9, 2010

WAP to print star


/*WAP to print star*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,j;
clrscr();   //clear screen

for(i='A';i<='E';i++) //loop starts from 1 for row
{
for(j='A';j<=i;j++)   // loop executes i times for colom
printf("%c",j);   // print j
printf("\n");    // goto next line
}
getch();
}

Wednesday, September 8, 2010

WAP to use of array


/*WAP to use of array*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,marks[5]={50,40,60,70,80,};
clrscr();
for(i=0;i<=5;i++)
{
printf("\n%d",marks[i]);
}
getch();
}

Tuesday, September 7, 2010

WAP to sum 1d-array


/*WAP to sum 1d-array*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3],b[3],c[3],i;
clrscr();
printf("enter 1 array elements no.s");
for(i=0;i<3;i++)
{
scanf("%d",&a[i]);
}
printf("enter 2 array elements no.s");
for(i=0;i<3;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<3;i++)
{
printf("sum of 2 array");
for(i=0;i<3;i++)
{
c[i]=a[i]+b[i];
printf("\n%d",c[i]);
}
}
getch();
}

Friday, September 3, 2010

WAP of display Alphabet


/*WAP of display Alphabet*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,j;
clrscr();   //clear screen

for(i='A';i<='E';i++) //loop starts from 1 for row
{
for(j='A';j<=i;j++)   // loop executes i times for colom
printf("%c",i);   // print j
printf("\n");    // goto next line
}
getch();
}

ADS

How to earn online

How to earn online:
Step - 1 :
signup for PayPal, to recieve your online earning
click here:
https://www.paypal.com/in/mrb/pal=CZ7224TZBMCBL
step - 2 : Singup these 4 sites & earn & enjoy! Read site to know how to earn.
1. trekpay
Earn up to $0.02 (2 cents) per click.
http://www.trekpay.com/?ref=34690
2. neobux
Earn up to $0.02 (2 cents) per click.
Click here:
http://www.neobux.com/?r=Moneyearner786

AddMe

Bookmark and Share