Posts

Showing posts with the label c programming

C Program to Multiply two Matrices by Passing Matrix to a Function

C Program to Multiply two Matrices by Passing Matrix to a Function #include <stdio.h> void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond); void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int multResult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond); void display(int mult[][10], int rowFirst, int columnSecond); int main() {     int firstMatrix[10][10], secondMatrix[10][10], mult[10][10], rowFirst, columnFirst, rowSecond, columnSecond, i, j, k;     printf("Enter rows and column for first matrix: ");     scanf("%d %d", &rowFirst, &columnFirst);     printf("Enter rows and column for second matrix: ");     scanf("%d %d", &rowSecond, &columnSecond);     // If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix agai...

C Program to Write a Sentence to a File

C Program to Write a Sentence to a File #include <stdio.h> #include <stdlib.h>  /* For exit() function */ int main() {    char sentence[1000];    FILE *fptr;    fptr = fopen("program.txt", "w");    if(fptr == NULL)    {       printf("Error!");       exit(1);    }       printf("Enter a sentence:\n");    gets(sentence);    fprintf(fptr,"%s", sentence);    fclose(fptr);    return 0; }  

C Program to Display its own Source Code as Output

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

Program to Display English Alphabets in Uppercase and Lowercase

Program to Display English Alphabets in Uppercase and Lowercase #include <stdio.h> int main() {     char c;     printf("Enter u to display alphabets in uppercase. And enter l to display alphabets in lowercase: ");     scanf("%c", &c);     if(c== 'U' || c== 'u')     {        for(c = 'A'; c <= 'Z'; ++c)          printf("%c ", c);     }     else if (c == 'L' || c == 'l')     {         for(c = 'a'; c <= 'z'; ++c)          printf("%c ", c);     }     else        printf("Error! You entered invalid character.");     return 0; }

C Program to Display Characters from A to Z Using Loop

C Program to Display Characters from A to Z Using Loop  #include <stdio.h> int main() {     char c;     for(c = 'A'; c <= 'Z'; ++c)        printf("%c ", c);        return 0; }

C Program to Find the Frequency of Characters in a String

C Program to Find the Frequency of Characters in a String #include <stdio.h> int main() {    char str[1000], ch;    int i, frequency = 0;    printf("Enter a string: ");    gets(str);    printf("Enter a character to find the frequency: ");    scanf("%c",&ch);    for(i = 0; str[i] != '\0'; ++i)    {        if(ch == str[i])            ++frequency;    }    printf("Frequency of %c = %d", ch, frequency);    return 0; }  Output Enter a string: This website is awesome. Enter a character to find the frequency: e Frequency of e = 4

C Program to Find the Length of a String

C Program to Find the Length of a String #include <stdio.h> int main () { char s [ 1000 ], i ; printf ( "Enter a string: " ); scanf ( "%s" , s ); for ( i = 0 ; s [ i ] != '\0' ; ++ i ); printf ( "Length of string: %d" , i ); return 0 ; }  

C Program to Copy String Without Using strcpy()

C Program to Copy String Without Using strcpy() #include <stdio.h> int main () { char s1 [ 100 ], s2 [ 100 ], i ; printf ( "Enter string s1: " ); scanf ( "%s" , s1 ); for ( i = 0 ; s1 [ i ] != '\0' ; ++ i ) { s2 [ i ] = s1 [ i ]; } s2 [ i ] = '\0' ; printf ( "String s2: %s" , s2 ); return 0 ; }  

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("--...

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

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!",           ...

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() ...

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 , ...

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