C Articles - Page 16 of 95
249 Views
ProblemHow to identify a total number of upper-case alphabets in a string using C Programming?SolutionThe logic we used to count number of upper-case letters in a sentence is as follows −for(a=string[0];a!='\0';i++){ a=string[i]; if (isupper(a)){ counter=counter+1; //counter++; } }Example 1 Live Demo#include #include void main(){ //Declaring integer for number determination, string// int i=0; char a; char string[50]; int counter=0; //Reading User I/p// printf("Enter the string :"); gets(string); //Using For loop and predefined function to count upper case alpha's// for(a=string[0];a!='\0';i++){ a=string[i]; ... Read More
403 Views
ProblemHow to display the current date and time in ISO standard format using C Programming language?SolutionThe current date and time of the input will be taken and we are trying to print the system time and date in ISO format.For example, Monday, Dec 15, 2020 10.50p.The built-in functions that we used in this program are −Time() − returns current time.Strftime() − converts the time to string form, this function include in time.h.Example Live Demo#include #include int main(){ time_t current = time(NULL); char datetime[20]; strftime(datetime, sizeof(datetime), "%a, %d%b%y %H:%M", localtime(¤t)); puts(datetime); return 0; }OutputThu, 31 Dec 20 ... Read More
589 Views
The atexit() is a function that allows the user to register a function that has to be called based on program termination.It is a predefined function that is included in stdlib header files.Example 1 Live Demo#include #include void welcome(void){ printf("Welcome to New, "); } void world(void){ printf("World"); } int main(){ //test atexit ,call user defined function atexit(world); atexit(welcome); return 0; }OutputWelcome to New, WorldExample 2 Live Demo#include #include void first(void){ printf("This is a beautiful, "); } void second(void){ printf("Wonderful life"); } int main(){ //test atexit ,call user defined function atexit(second); atexit(first); ... Read More
948 Views
ProblemWrite a C program to calculate the deposited amount incremented after some years with interestSolutionThe formula for calculating interest is −M=((r/100) * t); A=P*exp(M);Where r= rate of interest t=no. of years P=amount to be deposited M=temporary variable A= Final amount after interestAlgorithmSTART Step 1: declare double variables Step 2: read amount to be deposited Step 3: read rate of interest Step 4: read years you want to deposit Step 5: Calculate final amount with interest I. ... Read More
24K+ Views
ProblemWrite a C program for storing the details of 5 students into a file and print the same using fread() and fwrite()SolutionThe fread() function reads the entire record at a time.Syntaxfread( & structure variable, size of (structure variable), no of records, file pointer);Examplestruct emp{ int eno; char ename [30]; float sal; } e; FILE *fp; fread (&e, sizeof (e), 1, fp);The fwrite() function writes an entire record at a time.Syntaxfwrite( & structure variable , size of structure variable, no of records, file pointer);Examplestruct emp{ int eno: char ename [30]; float sal; } e; FILE ... Read More
885 Views
ProblemWrite a C program to concatenate n characters from source string to destination string using strncat library functionSolutionThe strcat functionThis function is used for combining or concatenating two strings.The length of the destination string must be greater than the source string.The resultant concatenated string will be in the source string.Syntaxstrcat (Destination String, Source string);Example 1#include main(){ char a[50] = "Hello"; char b[20] = "Good Morning"; clrscr ( ); strcat (a, b); printf("concatenated string = %s", a); getch ( ); }OutputConcatenated string = Hello Good MorningThe strncat functionThis function is used for combining or ... Read More
17K+ Views
ProblemHow to read a series of items that are present in a file and display the data in columns or tabular form using C ProgrammingSolutionCreate a file in write mode and write some series of information in the file and close it again open and display the series of data in columns on the console.Write mode of opening the fileFILE *fp; fp =fopen ("sample.txt", "w");If the file does not exist, then a new file will be created.If the file exists, then old content gets erased & current content will be stored.Read mode of opening the file FILE *fp fp =fopen ... Read More
1K+ Views
ProblemWrite a C program to define the structure and display the size and offsets of member variablesStructure − It is a collection of different datatype variables, grouped together under a single name.General form of structure declarationdatatype member1; struct tagname{ datatype member2; datatype member n; };Here, struct - keywordtagname - specifies name of structuremember1, member2 - specifies the data items that make up structure.Examplestruct book{ int pages; char author [30]; float price; };Structure variablesThere are three ways of declaring structure variables −Method 1struct book{ int pages; char author[30]; float price; }b;Method 2struct{ ... Read More
2K+ Views
ProblemMention some of the legal and illegal declarations and initializations while doing C programming?Before discussing the legal and illegal statements let’s see how to declare and initialize the variables in C.Variable declarationFollowing is the syntax of variable declaration −SyntaxDatatype v1, v2, … vn;Where v1, v2, ...vn are names of the variables.For example, int sum;float a, b;Variable can be declared in two ways −local declarationglobal declarationThe ‘local declaration’ is declaring a variable inside the main block and its value is available within that block.The ‘global declaration’ is declaring a variable outside the main block and its value is available throughout the ... Read More
2K+ Views
In this program, we are adding random numbers that are generated in between 0 and 100.After every runtime, the result of sum of random numbers is different, i.e., we get a different result for every execution.The logic we use to calculate the sum of random numbers in between 0 to 100 is −for(i = 0; i
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP