Earn by twitter on twivert

Sign up for PayPal and start accepting credit card payments instantly.
Showing posts with label C Program. Show all posts
Showing posts with label C Program. Show all posts

Monday, December 5, 2011

Student record using Array of Structure in c

/*WAP of Student record using Array of Structure in c*/
#include<stdio.h>
#include<conio.h>
struct student
{
 int rollno, marks1,marks2,marks3,tot;
 char name[25],grade;
 float per;
};
void main()
{
 struct student s[5];     //Data type of '*s' is struct student
 int i;
 clrscr();
 printf("\t*Students Records*\n");
 //take input from user
 for (i=0; i<5; i++)
 {
 printf("\nEnter Student Roll Number: ");
 scanf("%d", &s[i].rollno);
 printf("\nEnter Student name: ");
 scanf("%s", s[i].name);
 printf("\nEnter Student 3 subject's marks: ");
 scanf("%d%d%d", &s[i].marks1,&s[i].marks2,&s[i].marks3);
 //calculation
 s[i].tot = s[i].marks1 + s[i].marks2 + s[i].marks3;
 s[i].per = s[i].tot/3;
 if(s[i].per>=75){
  s[i].grade = 'A';
 }
 else if(s[i].per<75 && s[i].per>=60){
  s[i].grade = 'B';
 }
 else if(s[i].per<60 && s[i].per>=50){
  s[i].grade = 'C';
 }
 else if(s[i].per<50 && s[i].per>=40){
  s[i].grade = 'D';
 }
 else if(s[i].per<40 && s[i].per>=33){
  s[i].grade = 'E';
 }
 else {
  s[i].grade = 'F';
 }
 }
 //Display result
 for (i=0; i<5; i++)
 {
 printf("\n==================================\n");
 printf("\nStudent's Roll no. - %d", s[i].rollno);
 printf("\nStudent's Name - %s", s[i].name);
 printf("\nStudent's Total Marks - %d", s[i].tot);
 printf("\nStudent's Percentage - %f", s[i].per);
 printf("\nStudent's Grade - %c", s[i].grade);
 }
 getch();
}

Student record using Structure in c

/*WAP of Student record using Structure in c*/
#include<stdio.h>
#include<conio.h>
struct student
{
 int rollno, marks1,marks2,marks3,tot;
 char name[25],grade;
 float per;
};
void main()
{
 struct student s;  //Data type of '*s' is struct student
 clrscr();
 //take input from user
 printf("Enter Student Roll Number: ");
 scanf("%d", &s.rollno);
 printf("\nEnter Student name: ");
 scanf("%s", s.name);
 printf("\nEnter Student 3 subject's marks: ");
 scanf("%d%d%d", &s.marks1,&s.marks2,&s.marks3);
 //calculation
 s.tot = s.marks1 + s.marks2 + s.marks3;
 s.per = s.tot/3;
 if(s.per>=75){
  s.grade = 'A';
 }
 else if(s.per<75 && s.per>=60){
  s.grade = 'B';
 }
 else if(s.per<60 && s.per>=50){
  s.grade = 'C';
 }
 else if(s.per<50 && s.per>=40){
  s.grade = 'D';
 }
 else if(s.per<40 && s.per>=33){
  s.grade = 'E';
 }
 else {
  s.grade = 'F';
 }
 //Display result
 printf("\n==================================\n");
 printf("\nStudent's Roll no. - %d", s.rollno);
 printf("\nStudent's Name - %s", s.name);
 printf("\nStudent's Total Marks - %d", s.tot);
 printf("\nStudent's Percentage - %f", s.per);
 printf("\nStudent's Grade - %c", s.grade);

 getch();
}

Tuesday, November 15, 2011

length of array in c

/*WAP to Calculate length of array in c*/

#include<stdio.h>
#include<conio.h>

int array[6]12345};
void main() {
  clrscr();
  int len=sizeof(array)/sizeof(int);
  printf("Length Of Array=%d", len);
  getch();
}

 

Sunday, November 14, 2010

WAP to search a character in a string


/*WAP to search a character in a string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>

main()
{
 char *ptr, str[50];
 int ch;
 // Input of String & character
 printf("\n Enter the String \n");
 gets(str);
 printf("\n Enter the character to search ");
 ch = getchar();
 // Searching the character
 ptr = strchr(str,ch);
 if(ptr == NULL)
  printf("\n Not Found");
 else
  printf("\n The character %c is at the position %d\n",ch,ptr-str);
 getch();
 return 0;
}

Monday, November 1, 2010

WAP function using a pointer to a function


/*WAP Invoking a function using a pointer to a function */
#include<stdio.h>
#include<conio.h>
main( )
{
 int  display( ) ;
 int  ( *func_ptr )( ) ;

 func_ptr = display ;  /* assign address of function */ 
 printf ( "\nAddress of function display is %u", func_ptr ) ;
 ( *func_ptr )( ) ;  /* invokes the function display( ) */
         getch();
}

int  display( )
{
 puts ( "\nLong live viruses!!" ) ;
}

Friday, October 8, 2010

WAP to using putch (),putchar (), fputchar (), putch (), fputchar ()


/*WAP to using putch (),putchar (), fputchar (), putch (), fputchar (),*/
#include<stdio.h>
#include<conio.h>
main( )
{
 char  ch = 'A' ;

 putch ( ch ) ; 
 putchar ( ch ) ;
 fputchar ( ch ) ;
 putch ( 'Z' ) ;
 putchar ( 'Z' ) ;
 fputchar ( 'Z' ) ;
       getch();
}

WAP of take user input for continue using getch( ), getche( ), getchar( ), fgetchar( )


/*WAP of take user input for continue using getch( ), getche( ), getchar( ), fgetchar( )*/
#include<stdio.h>
#include<conio.h>
main( )
{
 char  ch ;

 printf ( "\nPress any key to continue" ) ;
 getch( ) ;  /* will not echo the character */

 printf ( "\nType any character" ) ;
 ch = getche( ) ;  /* will echo the character typed */

 printf ( "\nType any character" ) ;
 getchar( ) ;  
/* will echo character, must be followed by enter key */ 
 printf ( "\nContinue Y/N" ) ;
 fgetchar( ) ;  
/* will echo character, must be followed by enter key */ 
       getch();
}

WAP to Formatting


/*WAP to Formatting*/
#include<stdio.h>
#include<conio.h>
main( )
{
 int  i = 10 ;
 char  ch = 'A' ;
 float  a = 3.14 ;
 char  str[20] ; 

 printf ( "\n%d %c %f", i, ch, a ) ;
 sprintf ( str, "%d %c %f", i, ch, a ) ;
 printf ( "\n%s", str ) ;
       getch();
}

WAP of Formatting2


/*WAP of Formatting2*/
#include<stdio.h>
#include<conio.h>
main( )
{
 char  ch = 'z' ;
 int  i = 125 ;
 float  a = 12.55 ;
 char  s[ ] = "hello there !" ;

 printf ( "\n%c %d %f", ch, ch, ch ) ;
 printf ( "\n%s %d %f", s, s, s ) ;
 printf ( "\n%c %d %f",i ,i, i ) ;
 printf ( "\n%f %d\n", a, a ) ;
       getch();
}

WAP to Formatting strings with 't' tab


/*WAP to Formatting strings with 't' tab */
#include<stdio.h>
#include<conio.h>
main( )
{
 printf ( "You\tmust\tbe\tcrazy\nto\thate\tthis\tbook" ) ;
       getch();
}

WAP to Formatting strings with printf( )


/*WAP to Formatting strings with printf( ) */
#include<stdio.h>
#include<conio.h>
main( )
{
 char  firstname1[ ] = "Sandy" ;
 char  surname1[ ] = "Malya" ;
 char  firstname2[ ] = "AjayKumar" ;
 char  surname2[ ] = "Gurubaxani" ;

 printf ( "\n%20s%20s", firstname1, surname1 ) ;
 printf ( "\n%20s%20s", firstname2, surname2 ) ;
       getch();
}

WAP to formatting integer


/*WAP to formatting integer*/
#include<stdio.h>
#include<conio.h>
main( )
{
 int  weight = 63 ;

 printf ( "\nweight is %d kg", weight ) ;
 printf ( "\nweight is %2d kg", weight ) ;
 printf ( "\nweight is %4d kg", weight ) ;
 printf ( "\nweight is %6d kg", weight ) ;
 printf ( "\nweight is %-6d kg", weight ) ;
       getch();
}

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 ) ;
}

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