You are here

C programs

Sum of n natural numbers in C

C program to find the sum of n natural numbers using the formula and a for loop.

First n natural numbers are 1, 2, 3, ...,n. The numbers form an arithmetic progression (AP) with one as the first term as well as the common difference.

Sum of first n natural numbers = n*(n+1)/2.

C program to find sum of n natural numbers

#include <stdio.h>

C program to check if an array is sorted or not (ascending or descending or none)

C program to check if an array is sorted or not, i.e., in ascending order or descending order. Variables "a" and "d" in the program are for ascending and descending respectively and are initialized to one. If anyone of its element is greater than the next one, then it isn't in ascending order (a = 0). If anyone of its element is less than the next one, then it isn't in descending order (d = 0). We check the conditions for all consecutive elements of the array. If both the conditions are TRUE, then it isn't sorted. If anyone of them is FALSE, then it's sorted.

Linked list in C

Linked list
Linked lists are useful data structures and offer many advantages. A new element can be inserted at the beginning or at the end in constant time (in doubly linked lists). Memory utilization is efficient as it's allocated when we add new elements to a list and list size can increase/decrease as required. They are useful when the size of a list is unknown and changes frequently. It uses a node that stores data and a pointer to the next node.

C program to convert string to integer without using atoi function

C program to convert string to integer: It is frequently required to convert a string to an integer in applications. String should consists of digits only and an optional '-' (minus) sign at beginning for integers. For string containing other characters we can stop conversion as soon as a non digit character is encountered but in our program we will handle ideal case when only valid characters are present in string. Library function atoi can be used to convert string to an integer but we will create our own function.

Compiler used: 
0

C program to merge two arrays

C program to merge two arrays into third array: Arrays are assumed to be sorted in ascending order. You enter two short sorted arrays and combine them to get a large array.

C programming code to merge two sorted arrays

#include <stdio.h>

void merge(int [], int, int [], int, int []);

int main() {
  int a[100], b[100], m, n, c, sorted[200];

  printf("Input number of elements in first array\n");
  scanf("%d", &m);

  printf("Input %d integers\n", m);
  for (c = 0; c < m; c++) {
    scanf("%d", &a[c]);
  }

Pages

Subscribe to RSS - C programs