Saturday 15 March 2014

C Program Calculation of bonus

/* Calculation of bonus */
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 ) ;
}
}

C Program for File encryption utility

/* File encryption utility */
#include "stdio.h"
main( )
{
encrypt( ) ;
}
encrypt( )
{
FILE  *fs, *ft ;
char   ch ;
fs = fopen ( "SOURCE.C", "r" ) ;  /* normal file */
ft = fopen ( "TARGET.C”, "w" ) ;  /* encrypted file */
if ( fs == NULL || ft == NULL )
{
printf ( "\nFile opening error!" ) ;
exit ( 1 ) ;
}
while ( ( ch = getc ( fs ) ) != EOF )
  putc ( ~ch, ft ) ;
fclose ( fs ) ;
fclose ( ft ) ;
}

C Program Demonstration of call by value

/* Demonstration of call by value */
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 ) ;
}

C Program Calculation of total expenses

/* Calculation of total expenses */
main( )
{
int   qty, dis = 0 ;
float   rate, tot ;
printf ( "Enter quantity and rate " ) ;
scanf ( "%d %f", &qty, &rate) ;
if ( qty > 1000 )
dis = 10 ;
tot = ( qty * rate ) - ( qty * rate * dis / 100 ) ;
printf ( "Total expenses = Rs. %f", tot ) ;
}

C Program Display contents of a file on screen.I

#include <stdio.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 ) ;
}

C Program Display contents of a file on screen.

/* Display contents of a file on screen. */
# include "stdio.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 ) ;
}

Friday 14 March 2014

C Program to calculate average marks of 30 students using array

/* Use of array */
/* Program to calculate average marks of 30 students */
main( )
{
int  avg, sum = 0 ;
int  i ;
int  marks[30] ;  /* array declaration */
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ;  /* store data in array */
}
for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i] ;  /* read data from an array*/
avg = sum / 30 ;
printf ( "\nAverage marks = %d", avg ) ;
}

C Program to demonstrate printing of a string

/* Program to demonstrate printing of a string */
main( )
{
char  name[ ] = "Klinsman" ;
int  i = 0 ;
while ( i <= 7 )
{
printf ( "%c", name[i] ) ;
i++ ;
}
}

C Program Use of macro expansion

/* Use of macro expansion */
#define UPPER 25
main( )
{
int  i ;
for ( i = 1 ; i <= UPPER ; i++ )
printf ( "\n%d", i ) ;
}

C Program Demonstration of if statement

/* Demonstration of if statement */
main( )
{
int   num ;
printf ( "Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( "What an obedient servant you are !" ) ;
}

C program Calculation of simple interest

/* Calculation of simple interest */
/* Author gekay  Date: 25/05/2004 */
main( )
{
int   p, n ;
float   r, si ;
p = 1000 ;
n = 3 ;
r = 8.5 ;
/* formula for simple interest */
si = p * n * r / 100 ;
printf ( "%f" , si ) ;