Replace All Zeros with One in a Given Integer

Bhanu Priya
Updated on 26-Mar-2021 06:56:47

3K+ Views

ProblemWrite a program to replace all zeros (0's) with 1 in a given integer.Given an integer as an input, all the 0's in the number has to be replaced with 1.SolutionConsider an example given below −Here, the input is 102410 and the output is 112411.AlgorithmRefer an algorithm given below to replace all the 0’s to 1 in an integer.Step 1 − Input the integer from the user.Step 2 − Traverse the integer digit by digit.Step 3 − If a '0' is encountered, replace it by '1'.Step 4 − Print the integer.ExampleGiven below is the C program to replace all 0's ... Read More

Explain Queue by Using Linked List in C Language

Bhanu Priya
Updated on 26-Mar-2021 06:52:11

1K+ Views

Queue overflow and Queue under flow can be avoided by using linked list.Operations carried out under queue with the help of linked lists in C programming language are as follows −InsertDeleteInsertionThe syntax is as follows −Syntax&item : Newnode = (node*) mallac (sizeof (node)); newnode ->data = item; newnode ->link = NULL; if ((front = = NULL) || (rear = = NULL)){    front= newnode;    rear = newnode; }else{    Rear->link = newnode;    rear = newnode; }DeletionThe syntax is as follows −Syntaxif ((front= = NULL)) printf("Deletion is not possible, Queue is empty"); else{    temp = front;    front ... Read More

Differentiate Array of Structures and Arrays within a Structure in C

Bhanu Priya
Updated on 26-Mar-2021 06:50:51

780 Views

In C programming language, the most common use of structure is an array of structures.To declare an array of structures, first the structure must be defined and then, an array variable of that type has to be defined.For example,struct book b[10];//10 elements in an array of structures of type ‘book’ExampleFollowing is the C program for the array of structures − Live Demostruct marks{    int sub1;    int sub2;    int sub3;    int total; }; main(){    int i;    struct marks student[3] = {{20,17,11,10},       {175,23,169,10},       {27,56,27,01}};    struct marks total;    for(i = 0; i

Compare Structure Variables in C

Bhanu Priya
Updated on 26-Mar-2021 06:47:28

9K+ Views

In C programming language, a structure is a collection of different datatype variables, which are grouped together under a single name.Declaration and initialization of structuresThe general form of a structure declaration is as follows −datatype member1; struct tagname{    datatype member2;    datatype member n; };Here, struct is a keyword.tagname specifies the name of structure.member1, member2 specifies the data items that make up structure.For example, struct book{    int pages;    char author [30];    float price; };Structure variablesThere are three methods of declaring structure variables, which are as follows −First methodstruct book{    int pages;    char author[30];   ... Read More

Explain Stack Using Linked List in C

Bhanu Priya
Updated on 26-Mar-2021 06:47:11

3K+ Views

Stack over flow and stack under flow can be avoided by allocating memory dynamically.Operations carried out under stack in C programming language are as follows −PushPopPushFollowing is the basic implementation of a linked list −&item = 10 newnode = (node*) malloc (sizeof (node)); newnode ->data = item; newnode ->link = NULL; newnode ->link = start; start = newnode;popThe syntax is as follows −Syntaxif (start = = NULL) printf("Deletion is not possible.List is empty") else{    temp = start;    start = start link;    free (temp); }ProgramFollowing is the C program for stack by using linked lists −#include #include ... Read More

Sort Names in Alphabetical Order using C String Functions

Bhanu Priya
Updated on 26-Mar-2021 06:43:54

973 Views

ProblemSort the names given by the user at runtime in an alphabetical order by using the bubble sort technique.SolutionThe logic used to print the names in alphabetical order is as follows −for (i=1; i < ITEMS; i++){    for (j=1; j 0){ /* Exchange of contents */          strcpy (dummy, string[j-1]);          strcpy (string[j-1], string[j]);          strcpy (string[j], dummy );       }    } }ExampleFollowing is the C program to sort the names in an alphabetical order by using the string functions − Live Demo#define ITEMS 5 #define MAXCHAR ... Read More

Deletion of Element in Linked List

Bhanu Priya
Updated on 26-Mar-2021 06:40:12

710 Views

Linked lists use dynamic memory allocation i.e. they grow and shrink accordingly. They are defined as a collection of nodes. Here, nodes have two parts, which are data and link. The representation of data, link and linked lists is given below −Operations on linked listsThere are three types of operations on linked lists in C language, which are as follows −InsertionDeletionTraversingDeletionConsider an example given below −Delete node 2Delete node 1Delete node 3ProgramFollowing is the C program for deletion of the elements in linked lists − Live Demo#include #include struct Node{    int data;    struct Node *next; }; void ... Read More

Calculate Standard Deviation in C

Bhanu Priya
Updated on 25-Mar-2021 11:48:20

6K+ Views

Standard deviation is used to measure deviation of data from its mean. The mathematical formula to calculate the standard deviation is as follows −$$s=\sqrt{Variance}$$whereVariance$$=\frac{1}{n}\:\:\displaystyle\sum\limits_{i=1}^n (x_{i}-m)^{2}$$and$$m=mean=\frac{1}{n}\:\displaystyle\sum\limits_{i=1}^n x_{i}$$AlgorithmRefer an algorithm given below to calculate the standard deviation for the given numbers.Step 1 − Read n items.Step 2 − Calculate sum and mean of the items.Step 3 − Calculate variance.Step 4 − Calculate standard deviation.The logic used in the program for calculating standard deviation is as follows −for (i = 1 ; i

Find the Median of a Given List in C

Bhanu Priya
Updated on 25-Mar-2021 11:39:46

19K+ Views

If the elements of the list are arranged in order, then, the middle value which divides the items into two parts with equal number of items on either side is called the median.Odd numbers of items have just one middle value whereas; even numbers of items have two middle values.The median for even number of items is therefore, designated as the average of the two middle values.AlgorithmRefer an algorithm given below to calculate the median.Step 1 − Read the items into an array while keeping a count of the items.Step 2 − Sort the items in increasing order.Step 3 − ... Read More

C Program to Represent a Multiplication Table

Bhanu Priya
Updated on 25-Mar-2021 11:35:48

891 Views

ProblemWrite a program to print the multiplication table from 1 x 1 to 12 x 10 as given below −1 2 3 4 5 6 7 8 9 10 2 4 6 8 ……………….20 3 6 9…………………….30 4 8 12 16……………..40 - - - 12 24……………………..120SolutionUse two do while loops in nested form to display the multiplication table.The logic used to display the multiplication table is as follows −Inner loop is controlled by the variable column and is executed 10 times, whenever each time the outer loop is executed.Outer loop is executed 12 times and controlled by variable row.do /*......OUTER LOOP BEGINS........*/{    column = 1;    do /*.......INNER LOOP BEGINS.......*/{       y = row * column;       printf("%4d", y);       column = column + 1;    }    while (column

Advertisements