Posts

Showing posts from August, 2016

program counts the number of bytes in a C source file

This program counts the number of bytes in a C source file. The program prompts the user for a file name and then concatenates the ".c" extension to this name. It uses the function getc to read the characters. #include<stdlib.h> #include<stdio.h> #include<string.h> main( ) { FILE* fptr; char extension[] = ".c"; char file_name[FILENAME_MAX]; /* defined in stdio.h*/ int char_count; printf("\n\n\tFile name (NO extension):\t"); scanf("%s",file_name); strcat(file_name,extension); fptr=fopen(file_name,"rb"); for(char_count=0; getc(fptr) !=EOF; ++char_count); printf("\n\tByte size:\t%d", char_count); fclose(fptr); return EXIT_SUCCESS; }

Invoke function without main in C Language

Invoke function without main in C Language. #include<stdio.h> #include<conio.h> #define FIRST   0 #define SECOND  1 #define THIRD   2 //Variables int a,b,c,ch; float d; //Function Prototype void Read(); void Operation(); void Display(); #pragma startup Read       0 //First_Priority #pragma startup Operation  1 //Second_Priority #pragma exit Display     //Third_Priority void main() {  printf(" Enter to main() ");  getch();  printf(" Exit From main() ");  getch(); } void Read() {  clrscr();  printf(" Enter the value of a : ");  scanf("%d",&a);  printf(" Enter the value of b : ");  scanf("%d",&b); } void Operation() {  printf(" ArithMetic Operations ");  printf("--------------------- ");  printf("1  -> Addition ");  printf("2  -> Subtraction ");  printf("3  -> Multiplication ");  printf("--------------------- ");  scanf("%d",&am

Decimal to Binary, Octal and HEX converter

Decimal to Binary, Octal and HEX converter. #include<stdio.h> #include<conio.h> #define MAX 79 #define ESC 27 void main() {          char res,r;          int i,k=0,y;          long j=0,l=0,o=0;          textcolor(15);          for(i=0;i<5000;i++)                   cprintf(" ");          while(1)          {                   clrscr();                   printf("Enter Any Number To Be Converted : ");                   scanf("%ld",&j);                   l=j;                   o=l;                   k=0;                   if(j>2145555550)                   {                            textcolor(4);                            gotoxy(15,5);                            cprintf("The Number Is Greater Than Range");                            textcolor(15);                            getch();                            goto end;                   }                   else                   {                            gotoxy(1,6);   

CPU Scheduling algorithm implementation

CPU Scheduling algorithm implementation.   CPU Scheduling algorithm implementation #include<graphics.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> #include<dos.h>   union REGS i, o ;   int x1,y1,click;   int count=1;   char *comments[]={                            "                              ",                            "Amazed ?...Try it again!",                            "C'mon give yourself a chance!",                            "You can do it !",                            "Think about $ 1 Million!",                            "Don't give up !",                            "LOOSER..!!",                            "Sorry..You loose !"                            }; void main() {    int x=50,y=50,gdriver = 9, gmode=2;    initgraph(&gdriver, &gmode, "c:\tc\bgi");    setcolor(LIGHTRED);    settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);   

Calendar of Thousands of Years

Calendar of Thousands of Years. #include<stdio.h> #include<conio.h> static char *months[]={"January",               "February",               "March",               "April",               "May",               "June",               "July",               "August",               "September",               "October",               "November",               "December"}; void main() { static int days[12]={31,28,31,30,31,30,31,31,30,31,30,31}; long int ndays,ldays,tydays,tdays; int d,i,m,fday,y; char ch; textcolor(LIGHTGREEN); textbackground(LIGHTBLUE); clrscr(); printf("Enter year(1900 onwards) & month(number):"); scanf("%d%d",&y,&m); while(1) { ndays=(y-1)*365l; ldays=(y-1)/4-(y-1)/100+(y-1)/400; tdays=ndays+ldays;//total days //check for leap year if((y%100==0 && y%400==0)||(y%4==0 && y%100!=0)) days[1]=29; e

C Program to Display its own Source Code as its Output

C Program to Display its own Source Code as its Output. #include <stdio.h>   int main ( ) { FILE * fp ; char ch ;   fp = fopen ( __FILE__ , "r" ) ; do { ch = getc ( fp ) ; putchar ( ch ) ; } while ( ch != EOF ) ; fclose ( fp ) ; return 0 ; }  

C Program to Read a Grade & Display the Equivalent Descriptio

C Program to Read a Grade & Display the Equivalent Descriptio This C Program reads grade & displays the equivalent description. If grade is S, it prints super, if grade is A, it prints very good, if grade is B, it prints fair, if grade is Y, it prints absent, if grade is F, it prints fails. #include <stdio.h> #include <ctype.h> #include <string.h>   void main ( ) { char remark [ 15 ] ; char grade ;   printf ( "Enter the grade \n " ) ; scanf ( "%c" , & grade ) ; /* lower case letter to upper case */ grade = toupper ( grade ) ; switch ( grade ) { case 'S' : strcpy ( remark , " SUPER" ) ; break ; case 'A' : strcpy ( remark , " VERY GOOD" ) ; break ; case 'B' : strcpy ( remark , " FAIR" ) ; break ; case 'Y' : strcpy ( remark , &qu

C Program to Accept the Height of a Person & Categorize as Taller, Dwarf & Average

C Program to Accept the Height of a Person & Categorize as Taller, Dwarf & Average.   #include <stdio.h> void main ( ) { float height ;   printf ( "Enter the Height (in centimetres) \n " ) ; scanf ( "%f" , & height ) ; if ( height < 150.0 ) printf ( "Dwarf \n " ) ; else if ( ( height >= 150.0 ) && ( height <= 165.0 ) ) printf ( " Average Height \n " ) ; else if ( ( height >= 165.0 ) && ( height <= 195.0 ) ) printf ( "Taller \n " ) ; else printf ( "Abnormal height \n " ) ; }  

C Program to Display the ATM Transaction

C Program to Display the ATM Transaction.   #include <stdio.h> unsigned long amount = 1000 , deposit , withdraw ; int choice , pin , k ; char transaction = 'y' ;   void main ( ) { while ( pin != 1520 ) { printf ( "ENTER YOUR SECRET PIN NUMBER:" ) ; scanf ( "%d" , & pin ) ; if ( pin != 1520 ) printf ( "PLEASE ENTER VALID PASSWORD \n " ) ; } do { printf ( "********Welcome to ATM Service************** \n " ) ; printf ( "1. Check Balance \n " ) ; printf ( "2. Withdraw Cash \n " ) ; printf ( "3. Deposit Cash \n " ) ; printf ( "4. Quit \n " ) ; printf ( "******************?**************************?* \n \n " ) ; printf ( "Enter your choice: " ) ; scanf ( "%d" , & choice ) ; switch ( choice ) { case 1 : printf ( " \n YOUR BALANCE IN Rs : %lu " , amount ) ; bre

C Program to Display the IP Address of the System

C Program to Display the IP Address of the System. #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h> #include <unistd.h> #include <arpa/inet.h>   int main ( ) { int n ; struct ifreq ifr ; char array [ ] = "eth0" ;   n = socket ( AF_INET , SOCK_DGRAM , 0 ) ; //Type of address to retrieve - IPv4 IP address ifr. ifr_addr . sa_family = AF_INET ; //Copy the interface name in the ifreq structure strncpy ( ifr. ifr_name , array , IFNAMSIZ - 1 ) ; ioctl ( n , SIOCGIFADDR , & ifr ) ; close ( n ) ; //display result printf ( "IP Address is %s - %s \n " , array , inet_ntoa ( ( ( struct sockaddr_in * ) & ifr. ifr_addr ) -> sin_addr ) ) ; return 0 ; }  

C Program to Illustrate how User Authentication is Done

C Program to Illustrate how User Authentication is Done. #include <stdio.h>   void main ( ) { char password [ 10 ] , username [ 10 ] , ch ; int i ;   printf ( "Enter User name: " ) ; gets ( username ) ; printf ( "Enter the password < any 8 characters>: " ) ; for ( i = 0 ; i < 8 ; i ++ ) { ch = getchar ( ) ; password [ i ] = ch ; ch = '*' ; printf ( "%c" , ch ) ; } password [ i ] = ' \0 ' ; /* Original password can be printed, if needed */ printf ( " \n Your password is :" ) ; for ( i = 0 ; i < 8 ; i ++ ) { printf ( "%c" , password [ i ] ) ; } }  

C Program to Multiply given Number by 4 using Bitwise Operators

C Program to Multiply given Number by 4 using Bitwise Operators. #include <stdio.h>   void main ( ) { long number , tempnum ;   printf ( "Enter an integer \n " ) ; scanf ( "%ld" , & number ) ; tempnum = number ; /* left shift by two bits */ number = number << 2 ; printf ( "%ld x 4 = %ld \n " , tempnum , number ) ; }  

C Program to Convert Numbers to Roman Numerals

C Program to Convert Numbers to Roman Numerals. #include <stdio.h>   void predigit ( char num1 , char num2 ) ; void postdigit ( char c , int n ) ;   char romanval [ 1000 ] ; int i = 0 ; int main ( ) { int j ; long number ;   printf ( "Enter the number: " ) ; scanf ( "%d" , & number ) ; if ( number <= 0 ) { printf ( "Invalid number" ) ; return 0 ; } while ( number != 0 ) { if ( number >= 1000 ) { postdigit ( 'M' , number / 1000 ) ; number = number - ( number / 1000 ) * 1000 ; } else if ( number >= 500 ) { if ( number < ( 500 + 4 * 100 ) ) { postdigit ( 'D' , number / 500 ) ; number = number - ( number / 500 ) * 500 ; }

C Program to Convert Binary Code of a Number into its Equivalent Gray’s Code using Recursion

C Program to Convert Binary Code of a Number into its Equivalent Gray’s Code using Recursion. #include <stdio.h>   int bintogray ( int ) ;   int main ( ) { int bin , gray ;   printf ( "Enter a binary number: " ) ; scanf ( "%d" , & bin ) ; gray = bintogray ( bin ) ; printf ( "The gray code of %d is %d \n " , bin , gray ) ; return 0 ; }   int bintogray ( int bin ) { int a , b , result = 0 , i = 0 ;   if ( ! bin ) { return 0 ; } else { a = bin % 10 ; bin = bin / 10 ; b = bin % 10 ; if ( ( a && ! b ) || ( ! a && b ) ) { return ( 1 + 10 * bintogray ( bin ) ) ; } else { return ( 10 * bintogray ( bin ) ) ; } } }  

C Program to Convert Binary Code of a Number into its Equivalent Gray’s Code without using Recursion

C Program to Convert Binary Code of a Number into its Equivalent Gray’s Code without using Recursion. #include <stdio.h> #include <math.h>   int bintogray ( int ) ;   int main ( ) { int bin , gray ;   printf ( "Enter a binary number: " ) ; scanf ( "%d" , & bin ) ; gray = bintogray ( bin ) ; printf ( "The gray code of %d is %d \n " , bin , gray ) ; return 0 ; }   int bintogray ( int bin ) { int a , b , result = 0 , i = 0 ;   while ( bin != 0 ) { a = bin % 10 ; bin = bin / 10 ; b = bin % 10 ; if ( ( a && ! b ) || ( ! a && b ) ) { result = result + pow ( 10 , i ) ; } i ++; } return result ; }