Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Sunday, 30 March 2014

C Program for Count chars, spaces, tabs and newlines in a file

/* Count chars, spaces, tabs and newlines in a file */
# include "stdio.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 ) ;
}

Thursday, 27 March 2014

Write a Program to convert Decimal to Binary in C#.NET

Program 
using System;
class myclass
{
    static void Main()
    {
        int num;
        Console.Write("Enter a Number : ");
        num = int.Parse(Console.ReadLine());
        int quot;
        string rem = "";
        while (num >= 1)
        {
            quot = num / 2;
            rem += (num % 2).ToString();
            num = quot;
        }
        // Reversing the  value
        string bin = "";
        for (int i = rem.Length - 1; i >= 0; i--)
        {
            bin = bin + rem[i];
        }
        Console.WriteLine("The Binary format for given number is {0}",bin);

        Console.Read();
    }
}


Output

Enter a Number : 100
The Binary format for given number is 1100100

Friday, 21 March 2014

C Program for Demonstration of nested loops


/* Demonstration of nested loops */
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 ) ;
}
}
}

Wednesday, 19 March 2014

C Program for Comparison of two pointer variables

/* Comparison of two pointer variables */
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" ) ;
}

C Program to interchange string3 with string4

/* Program to interchange string3 with string4 */
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] ) ;
}

C Program for Use of strcmp() function

/* Use of strcmp() function */
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 ) ;
}

C Program for Demo of union at work

/* Demo of union at work */
main( )
{
union a
{
int  i ;
char  ch[2] ;
} ;
union a  key ;
key.i = 512 ;
printf ( "\nkey.i = %d", key.i ) ;
printf ( "\nkey.ch[0] = %d", key.ch[0] ) ;
printf ( "\nkey.ch[1] = %d", key.ch[1] ) ;
}

C Program for Two dimensional array

/* Two dimensional array */
main( )
{
int  stud[4][2] ;
int  i, j ;
for ( i = 0 ; i <= 3 ; i++ )
{
printf ( "\n Enter roll no. and marks" ) ;
scanf ( "%d %d", &stud[i][0], &stud[i][1] ) ;
}
for ( i = 0 ; i <= 3 ; i++ )
printf ( "\n%d %d", stud[i][0], stud[i][1] ) ;
}

C Program for Reads records from binary file and displays them on VDU


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

C Program for Demonstration of passing an entire array to a function

/* Demonstration of passing an entire array to a function */
main( )
{
int  num[ ] = { 24, 34, 12, 44, 56, 17 } ;
dislpay ( &num[0], 6 ) ;
}
display ( int  *j, int  n )
{
int  i ;
for ( i = 0 ; i <= n - 1 ; i++ )
{
printf ( "\nelement = %d", *j ) ;
j++ ;  /* increment pointer to point to next element */
}
}

C Program for Use of const keyword

/* Use of const keyword */
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 ) ;
}

C Program for Passing address of a structure variable

/* Passing address of a structure variable */
struct book
{
char  name[25] ;
char  author[25] ;
int  callno ;
} ;
main( )
{
struct book  b1 = { "Let us C", "YPK", 101 } ;
display ( &b1 ) ;
}
display ( struct book  *b )
{
printf ( "\n%s %s %d", b->name, b->author, b->callno ) ;
}

C Program for Use of #pragma

/* Use of #pragma */
/* Program to change execution of functions */
void fun1( ) ;
void fun2( ) ;
#pragma startup fun1
#pragma exit fun2
main( )
{
printf ( "\nInside maim" ) ;
}
void fun1( )
{
printf ( "\nInside fun1" ) ;
}
void fun2( )
{
printf ( "\nInside fun2" ) ;
}

C Program for Use of stringcopy function

/* Use of stringcopy function */
main( )
{
char  source[ ] = "Sayonara" ;
char  target[20] ;
strcpy ( target, source ) ;
printf ( "\nsource string = %s", source ) ;
printf ( "\ntarget string = %s", target ) ;
}

C Program for Insurance of driver - without using logical operators

/* Insurance of driver - without using logical operators */
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" ) ;
}
}
}

C Program for Invoking a function using a pointer to a function

/* Invoking a function using a pointer to a function */
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( ) */
}
int  display( )
{
puts ( "\nLong live viruses!!" ) ;
}

C Program for Use of break keyword in while loop

/* Use of break keyword in while loop */
main( )
{
int  i = 1 , j = 1 ;
while ( i++ <= 100 )
{
while ( j++ <= 200 )
{
if ( j == 150 )
break ;
else
printf ( "%d %d\n", i, j ) ;
}
}
}

C Prgram for Writes records to a file using structure

/* Writes records to a file using structure */
#include <stdio.h>
main( )
{
FILE  *fp ;
char  another = 'Y' ;
struct emp
{
char  name[40] ;
int  age ;
float  bs ;
} ;
struct emp  e ;
fp = fopen ( "EMPLOYEE.DAT", "w" ) ;
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 ) ;
fprintf ( fp, "%s %d %f\n", e.name, e.age, e.bs ) ;
printf ( "Add another record (Y/N) " ) ;
fflush ( stdin ) ;
another = getche( ) ;
}
fclose ( fp ) ;
}

C Program for A look-alike of the function strlen( )

/* A look-alike of the function strlen( ) */
main( )
{
char  arr[ ] = "Bamboozled" ;
int  len1, len2 ;
len1 = xstrlen ( arr ) ;
len2 = xstrlen ( "Humpty Dumpty" ) ;
printf ( "\nstring = %s length = %d", arr, len1 ) ;
printf ( "\nstring = %s length = %d", "Humpty Dumpty", len2 ) ;
}
xstrlen ( char  *s )
{
int  length = 0 ;
while ( *s != '\0' )
{
length++ ;
s++ ;
}
return ( length ) ;

C Program for Passing individual structure elements

/* Passing individual structure elements */
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 ) ;
}

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