C Programming Code Examples
C > Data Structures Code Examples
Add two long positive integers
/* Add two long positive integers */
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
#include<ctype.h>
struct node
{
int data;
struct node*next;
};
void insert(struct node**p,int num)
{
struct node*temp;
if(*p==NULL)
{
(*p)=(struct node*)malloc(sizeof(struct node));
(*p)->next=NULL;
(*p)->data=num;
}
else
{
temp=(struct node*)malloc(sizeof(struct node));
temp->next=(*p);
(*p)=temp;
(*p)->data=num;
}
}
void add_in(struct node*a,struct node*b,struct node**c)
{
int d,carry;
carry=0;
struct node*t;
while(a!=NULL&&b!=NULL)
{
d=(a->data+b->data+carry)%10;
insert(c,d);
if( (a->data+b->data+carry) >= 10)
{
carry=1;
}
else carry=0;
a=a->next;
b=b->next;
}
if(a==NULL&&b==NULL)
{
return;
}
else
{
if(a!=NULL&&b==NULL)
{
t=a;
}
else
{
t=b;
}
while(t!=NULL)
{
d=(carry+t->data)%10;
if((carry+t->data)>=10)
carry=1;
else
carry=0;
insert(c,d);
t=t->next;
}
if(carry==1)
insert(c,carry);
}
}
void numin(struct node**p)
{
*p=NULL;char c='c';
while(c!='n')
{
c=getch();
if(!isdigit(c))
return;
else
{
putch(c);
insert(p,c-'0');
}
}
}
void disp(struct node*p)
{
if(p==NULL)
return;
else
{
printf("%d",p->data);
disp(p->next);
}
}
void main()
{
struct node *a,*b,*c;
clrscr();
a=b=c=NULL;
printf("Enter the first number....
");
numin(&a);
printf("
Enter the second number....
");
numin(&b);
printf("
The added result is...
");
add_in(a,b,&c);
disp(c);
getch();
}
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming. These operators are used to perform logical operations and used with conditional statements like C if-else statements.
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
Function clrscr() clears the screen and moves the cursor to the upper left-hand corner of the screen. If you are using the GCC compiler, use system function to execute the clear/cls command. clrscr() function is also a non-standard function defined in "conio.h" header. This function is used to clear the console screen. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:
Allocate memory block. Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. The content of the newly allocated block of memory is not initialized, remaining with indeterminate values. If size is zero, the return value depends on the particular library implementation (it may or may not be a null pointer), but the returned pointer shall not be dereferenced. The "malloc" or "memory allocation" method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form. It doesn't Iniatialize memory at execution time so that it has initializes each block with the default garbage value initially.
The sizeof() operator is commonly used in C. It determines the size of the expression or the data type specified in the number of char-sized storage units. The sizeof() operator contains a single operand which can be either an expression or a data typecast where the cast is data type enclosed within parenthesis. The data type cannot only be primitive data types such as integer or floating data types, but it can also be pointer data types and compound data types such as unions and structs.
The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.
write a character to the console. The putch() function writes the character specified by the argument c to the console. putch() displays any alphanumeric characters to the standard output device. It displays only one character at a time. The putch() function is used for printing character to a screen at current cursor location. It is unformatted character output functions. It is defined in header file conio.h.
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.
While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed multiple times depending upon a given boolean condition. It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance. The while loop evaluates the test expression inside the parentheses (). If test expression is true, statements inside the body of while loop are executed. Then, test expression is evaluated again. The process goes on until test expression is evaluated to false. If test expression is false, the loop terminates.
Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,
Check if character is decimal digit. Checks whether c is a decimal digit character. This function accepts a character and classifies the character as a digit. It returns a value other than 0 to indicate success. The isdigit() function checks if the character is a numeric character. Although isdigit() accepts an integer as an argument, when a character is passed to the function it internally converts the character to an ASCII value.
The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program.
This C Program code finds the intersection & union of 2 Linked lists. Intersection is a set of elements that are common in both lists while union is a set of all Unique Elements in both...
Input length and width of a rectangle and find area of the given rectangle. How to calculate area of a rectangle in C programming. Input length and width of rectangle. Store it in two
In C programming language we use bitwise AND & operator to check status of any bit. Bitwise AND operation evaluate each bit of resultant value as 1, if corresponding bit of...
C Program uses recursive function & reverses the nodes in a Linked list and displays the list. Linked list is an ordered set of data elements, each 'containing a link' to its successor. This C
C programming code to input temperature in degree Fahrenheit and convert it to degree Centigrade. Code convert temperature from Fahrenheit to Celsius in the C programming.
The LCM of two integers num1 and num2 is the smallest positive integer that is perfectly divisible by both num1 and num2 (without a remainder). For example: the LCM of 72 and
In Each Iteration we are moving downward so increment "y". For each new 'Iteration' we are starting from beginning but one 'co-ordinate' back so decrement x. 'Moves Cursor Position'
C Program Code input Length in centimeter and convert it to meter and kilometer. How to convert length from centimeter to meter and kilometer in C. Input length centimeter