C Programs A - Z
C Programs A - Z
int main()
{
printf("Hello world\n");
return 0;
}
Purpose of Hello world program may be to say hello to people or the users of your software or
application.
Output of program:
We may store "hello world" in a character array as a string constant and then print it.
#include <stdio.h>
int main()
{
char string[] = "Hello World";
printf("%s\n", string);
return 0;
}
C programming code
#include <stdio.h>
int main()
{
int a;
printf("Enter an integer\n");
scanf("%d", &a);
return 0;
}
Output of program:
#include <stdio.h>
int main ()
{
char n[1000];
printf("Input an integer\n");
scanf("%s", n);
printf("%s", n);
return 0;
}
Output of program:
Input an integer
13246523123156432123154131212341564313219
13246523123156432123154131212341564313219
C programming code
#include<stdio.h>
int main()
{
int a, b, c;
c = a + b;
return 0;
}
Output of program:
#include<stdio.h>
main()
{
int a = 1, b = 2;
return 0;
}
#include <stdio.h>
int main()
{
int a, b, c;
char ch;
while (1) {
printf("Inut two integers\n");
scanf("%d%d", &a, &b);
getchar();
c = a + b;
return 0;
}
Output of program:
#include<stdio.h>
main()
{
long first, second, sum;
printf("%ld\n", sum);
return 0;
}
result = a + b;
return result;
}
C PROGRAM TO CHECK ODD OR EVEN USING MODULUS OPERATOR
#include <stdio.h>
int main()
{
int n;
printf("Enter an integer\n");
scanf("%d", &n);
if (n%2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}
#include <stdio.h>
int main()
{
int n;
printf("Enter an integer\n");
scanf("%d", &n);
if (n & 1 == 1)
printf("Odd\n");
else
printf("Even\n");
return 0;
}
#include <stdio.h>
int main()
{
int n;
printf("Input an integer\n");
scanf("%d", &n);
return 0;
}
#include <stdio.h>
int main()
{
int n;
printf("Enter an integer\n");
scanf("%d", &n);
if ((n/2)*2 == n)
printf("Even\n");
else
printf("Odd\n");
return 0;
}
C program to perform basic arithmetic operations which are addition, subtraction, multiplication
and division of two numbers. Numbers are assumed to be integers and will be entered by the
user.
C programming code
#include <stdio.h>
int main()
{
int first, second, add, subtract, multiply;
float divide;
printf("Enter two integers\n");
scanf("%d%d", &first, &second);
printf("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);
return 0;
}
Output of program:
This code checks whether an input alphabet is a vowel or not. Both lower-case and upper-case
are checked.
C programming code
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character\n");
scanf("%c", &ch);
return 0;
}
Output of program:
#include <stdio.h>
int main()
{
char ch;
printf("Input a character\n");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is not a vowel.\n", ch);
}
return 0;
}
Function to check vowel
int check_vowel(char a)
{
if (a >= 'A' && a <= 'Z')
a = a + 'a' - 'A'; /* Converting to lower case or use a = a + 32 */
return 0;
}
C program to check leap year: c code to check leap year, year will be entered by the user.
C programming code
#include <stdio.h>
int main()
{
int year;
if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
Output of program:
C program to add digits of a number: Here we are using modulus operator(%) to extract
individual digits of number and adding them.
C programming code
#include <stdio.h>
int main()
{
int n, t, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}
If you wish you can modify input variable (n) and do not use an additional variable (t) but it is
not recommended.
Output of program:
C program to find sum of digit of an integer which does not use modulus operator. Our program
uses a character array (string) for storing an integer. We convert every character of string into an
integer and add all these integers.
#include <stdio.h>
int main()
{
int c, sum, t;
char n[1000];
printf("Input an integer\n");
scanf("%s", n);
sum = c = 0;
return 0;
}
An advantage of this method is that input integer can be very large which may not be stored in
int or long long data type see an example below in output.
Output of program:
Input an integer
123456789123456789123456789
Sum of digits of 123456789123456789123456789 = 135
#include <stdio.h>
int add_digits(int);
int main()
{
int n, result;
scanf("%d", &n);
result = add_digits(n);
printf("%d\n", result);
return 0;
}
int add_digits(int n) {
static int sum = 0;
if (n == 0) {
return 0;
}
Static variable sum is used and is initialized to 0, it' value will persists after function calls i.e. it is
initialized only once when a first call to function is made.
Factorial program in c
Factorial program in c: c code to find and print factorial of a number, three methods are given,
first one uses for loop, second uses a function to find factorial and third using recursion. Factorial
is represented using '!', so five factorial will be written as (5!), n factorial as (n!). Also
n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0! = 1.
#include <stdio.h>
int main()
{
int c, n, fact = 1;
return 0;
}
#include <stdio.h>
long factorial(int);
int main()
{
int number;
long fact = 1;
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
return result;
}
#include<stdio.h>
long factorial(int);
int main()
{
int n;
long f;
if (n < 0)
printf("Negative integers are not allowed.\n");
else
{
f = factorial(n);
printf("%d! = %ld\n", n, f);
}
return 0;
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
Recursion is a technique in which a function calls itself, for example in above code factorial
function is calling itself. To solve a problem using recursion you must first express its solution in
recursive form.
C program to find hcf and lcm: The code below finds highest common factor and least common
multiple of two integers. HCF is also known as greatest common divisor(GCD) or greatest
common factor(gcf).
C programming code
#include <stdio.h>
int main() {
int a, b, x, y, t, gcd, lcm;
printf("Enter two integers\n");
scanf("%d%d", &x, &y);
a = x;
b = y;
while (b != 0) {
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x*y)/gcd;
return 0;
}
Output of program:
#include <stdio.h>
int main() {
long x, y, hcf, lcm;
return 0;
}
#include <stdio.h>
int main() {
long x, y, hcf, lcm;
return 0;
}
while (y != 0) {
if (x > y) {
x = x - y;
}
else {
y = y - x;
}
}
return x;
}
C program to convert decimal to binary: c language code to convert an integer from decimal
number system(base-10) to binary number system(base-2). Size of integer is assumed to be 32
bits. We use bitwise operators to perform the desired task. We right shift the original number by
31, 30, 29, ..., 1, 0 bits using a loop and bitwise AND the number obtained with 1(one), if the
result is 1 then that bit is 1 otherwise it is 0(zero).
C programming code
#include <stdio.h>
int main()
{
int n, c, k;
if (k & 1)
printf("1");
else
printf("0");
}
printf("\n");
return 0;
}
Download Decimal binary program.
Output of program:
Above code only prints binary of integer, but we may wish to perform operations on binary so in
the code below we are storing the binary in a string. We create a function which returns a pointer
to string which is the binary of the number passed as argument to the function.
#include <stdio.h>
#include <stdlib.h>
char *decimal_to_binary(int);
main()
{
int n, c, k;
char *pointer;
pointer = decimal_to_binary(n);
printf("Binary string of %d is: %s\n", n, t);
free(pointer);
return 0;
}
char *decimal_to_binary(int n)
{
int c, d, count;
char *pointer;
count = 0;
pointer = (char*)malloc(32+1);
if ( pointer == NULL )
exit(EXIT_FAILURE);
if ( d & 1 )
*(pointer+count) = 1 + '0';
else
*(pointer+count) = 0 + '0';
count++;
}
*(pointer+count) = '\0';
return pointer;
}
Memory is allocated dynamically because we can't return a pointer to a local variable (character
array in this case). If we return a pointer to local variable then program may crash or we get
incorrect result.
C program to find nCr and nPr: This code calculate nCr which is n!/(r!*(n-r)!) and nPr = n!/(n-r)!
#include <stdio.h>
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
int main()
{
int n, r;
long ncr, npr;
return 0;
}
result = factorial(n)/(factorial(r)*factorial(n-r));
return result;
}
result = factorial(n)/factorial(n-r);
return result;
}
long factorial(int n) {
int c;
long result = 1;
return result;
}
Output of program:
Another way to calculate nPr and nCr using functions
We use long long data type in our program to handle large numbers.
#include <stdio.h>
#define ll long long
int main() {
int n, r;
ll ncr, npr;
return 0;
}
ll find_npr(int n, int r) {
ll result = 1;
int c = 1;
while (c <= r) {
result = result * (n - r + c);
c++;
}
return result;
}
ll factorial(int n) {
int c;
ll result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;
}
This c program add n numbers which will be entered by the user. Firstly user will enter a number
indicating how many numbers user wishes to add and then user will enter n numbers. In the first
c program to add numbers we are not using an array, and using array in the second code.
C programming code
#include <stdio.h>
int main()
{
int n, sum = 0, c, value;
printf("Enter %d integers\n",n);
return 0;
}
You can use long int data type for sum variable.
Download Add n numbers program.
Output of program:
#include <stdio.h>
int main()
{
int n, sum = 0, c, array[100];
scanf("%d", &n);
printf("Sum = %d\n",sum);
return 0;
}
The advantage of using array is that we have a record of numbers inputted by user and can use
them further in program if required and obviously storing numbers will require additional
memory.
#include <stdio.h>
int main()
{
int n, c, array[100];
long result;
scanf("%d", &n);
return 0;
}
if (n == 0)
return sum;
C program to swap two numbers with and without using third variable, swapping in c using
pointers, functions (Call by reference) and using bitwise XOR operator, swapping means
interchanging. For example if in your c program you have taken two variable a and b where a = 4
and b = 5, then before swapping a = 4, b = 5 after swapping a = 5, b = 4
In our c program to swap numbers we will use a temp variable to swap two numbers.
#include <stdio.h>
int main()
{
int x, y, temp;
return 0;
}
Output of program:
You can also swap two numbers without using temp or temporary or third variable. In that case c
program will be as shown :-
#include <stdio.h>
int main()
{
int a, b;
a = a + b;
b = a - b;
a = a - b;
#include <stdio.h>
int main()
{
int x, y, *a, *b, temp;
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
return 0;
}
#include <stdio.h>
int main()
{
int x, y;
return 0;
}
temp = *b;
*b = *a;
*a = temp;
}
#include <stdio.h>
int main()
{
int x, y;
x = x ^ y;
y = x ^ y;
x = x ^ y;
return 0;
}
Swapping is used in sorting algorithms that is when we wish to arrange numbers in a particular
order either in ascending order or in descending.
C program to reverse a number
C Program to reverse a number :- This program reverse the number entered by the user and then
prints the reversed number on the screen. For example if user enter 123 as input then 321 is
printed as output. In our program we use modulus(%) operator to obtain the digits of a number.
To invert number look at it and write it from opposite direction or the output of code is a number
obtained by writing original number from right to left. To reverse or invert large numbers use
long data type or long long data type if your compiler supports it, if you still have large numbers
then use strings or other data structure.
C programming code
#include <stdio.h>
int main()
{
int n, reverse = 0;
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
return 0;
}
Output of program:
#include <stdio.h>
long reverse(long);
int main()
{
long n, r;
scanf("%ld", &n);
r = reverse(n);
printf("%ld\n", r);
return 0;
}
long reverse(long n) {
static long r = 0;
if (n == 0)
return 0;
r = r * 10;
r = r + n % 10;
reverse(n/10);
return r;
}
Palindrome Numbers
Palindrome number in c: A palindrome number is a number such that if we reverse it, it will not
change. For example some palindrome numbers examples are 121, 212, 12321, -454. To check
whether a number is palindrome or not first we reverse it and then compare the number obtained
with the original, if both are same then number is palindrome otherwise not. C program for
palindrome number is given below.
#include <stdio.h>
int main()
{
int n, reverse = 0, temp;
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
if ( n == reverse )
printf("%d is a palindrome number.\n", n);
else
printf("%d is not a palindrome number.\n", n);
return 0;
}
Output of program:
These program prints various different patterns of numbers and stars. These codes illustrate how
to create various patterns using c programming. Most of these c programs involve usage of
nested loops and space. A pattern of numbers, star or characters is a way of arranging these in
some logical manner or they may form a sequence. Some of these patterns are triangles which
have special importance in mathematics. Some patterns are symmetrical while other are not.
Please see the complete page and look at comments for many different patterns.
*
***
*****
*******
*********
We have shown five rows above, in the program you will be asked to enter the numbers of rows
you want to print in the pyramid of stars.
C programming code
#include <stdio.h>
int main()
{
int row, c, n, temp;
printf("Enter the number of rows in pyramid of stars you wish to see ");
scanf("%d",&n);
temp = n;
temp--;
printf("\n");
}
return 0;
}
For more patterns or shapes on numbers and characters see comments below and also see codes
on following pages:
Floyd triangle
Pascal triangle
#include <stdio.h>
int main()
{
int n, c, k;
printf("\n");
}
return 0;
}
Using these examples you are in a better position to create your desired pattern for yourself.
Creating a pattern involves how to use nested loops properly, some pattern may involve
alphabets or other special characters. Key aspect is knowing how the characters in pattern
changes.
C pattern programs
Pattern:
*
*A*
*A*A*
*A*A*A*
#include<stdio.h>
main()
{
int n, c, k, space, count = 1;
space = n;
printf("\n");
space--;
count = 1;
}
return 0;
}
Pattern:
1
232
34543
4567654
567898765
C program:
#include<stdio.h>
main()
{
int n, c, d, num = 1, space;
scanf("%d",&n);
space = n - 1;
space--;
return 0;
}
Diamond pattern in c: This code print diamond pattern of stars. Diamond shape is as follows:
*
***
*****
***
*
C programming code
#include <stdio.h>
int main()
{
int n, c, k, space = 1;
space = n - 1;
space--;
printf("\n");
}
space = 1;
for (k = 1; k <= n - 1; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space++;
printf("\n");
}
return 0;
}
Output of program:
#include <stdio.h>
scanf("%d", &rows);
print(rows);
return 0;
}
if (r <= 0)
return;
space = r - 1;
stars += 2;
printf("\n");
print(--r);
space = r + 1;
stars -= 2;
printf("\n");
}
C program for prime number
Prime number program in c: c program for prime number, this code prints prime numbers using c
programming language. To check whether a number is prime or not see another code below.
Prime number logic: a number is prime if it is divisible only by one and itself. Remember two is
the only even and also the smallest prime number. First few prime numbers are 2, 3, 5, 7, 11, 13,
17....etc. Prime numbers have many applications in computer science and mathematics. A
number greater than one can be factorized into prime numbers, For example 540 = 22*33*51
#include<stdio.h>
int main()
{
int n, i = 3, count, c;
if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}
return 0;
}
#include<stdio.h>
main()
{
int n, c = 2;
return 0;
}
#include<stdio.h>
int check_prime(int);
main()
{
int n, result;
result = check_prime(n);
if ( result == 1 )
printf("%d is prime.\n", n);
else
printf("%d is not prime.\n", n);
return 0;
}
int check_prime(int a)
{
int c;
There are many logic to check prime numbers, one given below is more efficient then above
method.
for ( c = 2 ; c <= (int)sqrt(n) ; c++ )
Only checking from 2 to square root of number is sufficient.
There are many more efficient logic available.
C programming code
#include <stdio.h>
int main()
{
int n, sum = 0, temp, remainder, digits = 0;
printf("Input an integer\n");
scanf("%d", &n);
temp = n;
// Count number of digits
while (temp != 0) {
digits++;
temp = temp/10;
}
temp = n;
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
printf("%d is an Armstrong number.\n", n);
else
printf("%d is not an Armstrong number.\n", n);
return 0;
}
return p;
}
Output of program:
We will use long long data type in our program so that we can check numbers up to 2^64-1.
#include <stdio.h>
int main () {
long long n;
printf("Input a number\n");
scanf("%lld", &n);
if (check_armstrong(n) == 1)
printf("%lld is an armstrong number.\n", n);
else
printf("%lld is not an armstrong number.\n", n);
return 0;
}
temp = n;
while (temp != 0) {
digits++;
temp = temp/10;
}
temp = n;
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
return 1;
else
return 0;
}
return p;
}
Output of program:
Input a number
35641594208964132
35641594208964132 is an Armstrong number.
C program to generate Armstrong numbers. In our program user will input two integers and we
will print all Armstrong numbers between two integers. Using a for loop we will check numbers
in the desired range. In our loop we call our function check_armstrong which returns 1 if number
is Armstrong and 0 otherwise. If you are not familiar with Armstrong numbers see Check
Armstrong number program.
C programming code
#include <stdio.h>
int check_armstrong(int);
int power(int, int);
int main () {
int c, a, b;
return 0;
}
int check_armstrong(int n) {
long long sum = 0, temp;
int remainder, digits = 0;
temp = n;
while (temp != 0) {
digits++;
temp = temp/10;
}
temp = n;
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
return 1;
else
return 0;
}
return p;
}
Output of program:
In the sample output we are printing Armstrong numbers in range [0, 1000000].
Fibonacci series in c
Fibonacci series in c programming: c program for Fibonacci series without and with recursion.
Using the code below you can print as many numbers of terms of series as desired. Numbers of
Fibonacci sequence are known as Fibonacci numbers. First few numbers of series are 0, 1, 1, 2,
3, 5, 8 etc, Except first two terms in sequence every other term is the sum of two previous terms,
For example 8 = 3 + 5 (addition of 3, 5). This sequence has many applications in mathematics
and Computer Science.
int main()
{
int n, first = 0, second = 1, next, c;
return 0;
}
Output of program:
#include<stdio.h>
int Fibonacci(int);
main()
{
int n, i = 0, c;
scanf("%d",&n);
printf("Fibonacci series\n");
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
Recursion method is less efficient as it involves function calls which uses stack, also there are
chances of stack overflow if function is called frequently for calculating larger Fibonacci
numbers.
This program performs addition of two numbers using pointers. In our program we have two two
integer variables x, y and two pointer variables p and q. Firstly we assign the addresses of x and
y to p and q respectively and then assign the sum of x and y to variable sum. Note that & is
address of operator and * is value at address operator.
C programming code
#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
return 0;
}
Output of program:
#include <stdio.h>
int main()
{
long first, second, *p, *q, sum;
return 0;
}
sum = *x + *y;
return sum;
}
This code find maximum or largest element present in an array. It also prints the location or
index at which maximum element occurs in array. This can also be done by using pointers (see
both codes). The algorithm to find maximum is first we assume that maximum element occurs at
beginning of array and stores that value in a variable. Then we compare it with other array
elements one by one, if any element is greater than our assumed maximum then maximum value
and index at which it occurs is updated. Similarly we can find minimum element in an array.
C programming code
#include <stdio.h>
int main()
{
int array[100], maximum, size, c, location = 1;
maximum = array[0];
If maximum occurs two or more times times in array then index at which it occurs first is printed
or maximum value at smallest index. You can easily modify this code this code to print largest
index at which maximum occur. You can also store all indices at which maximum occur in an
array.
Output of program:
#include <stdio.h>
int main() {
int c, array[100], size, location, maximum;
max = a[0];
index = 0;
return index;
}
#include <stdio.h>
int main()
{
long array[100], *maximum, size, c, location = 1;
maximum = array;
*maximum = *array;
printf("Maximum element found at location %ld and it's value is %ld.\n", location,
*maximum);
return 0;
}
C code to find minimum or smallest element present in an array. It also prints the location or
index at which minimum element occurs in array. This can also be done by using pointers (see
both the codes). Our algorithm first assumes first element as minimum and then compare it with
other elements if an element is smaller than it then it becomes the new minimum and this process
is repeated till complete array is scanned.
C programming code
#include <stdio.h>
int main()
{
int array[100], minimum, size, c, location = 1;
minimum = array[0];
If minimum occurs two or more times times in array then index at which it occurs first is printed
or minimum value at smallest index. You can modify this code this code to print largest index at
which minimum occur. You can also store all indices at which minimum occur in an array.
Output of program:
#include <stdio.h>
int main() {
int c, array[100], size, location, minimum;
min = a[0];
index = 0;
return index;
}
#include <stdio.h>
int main()
{
int array[100], *minimum, size, c, location = 1;
minimum = array;
*minimum = *array;
printf("Minimum element found at location %d and it's value is %d.\n", location, *minimum);
return 0;
}
Linear search in c
Linear search in c programming: The following code implements linear search (Searching
algorithm) which is used to find whether a given number is present in an array and if it is present
then at what location it occurs. It is also known as sequential search. It is very simple and works
as follows: We keep on comparing each element with the element to search until the desired
element is found or list ends. Linear search in c language for multiple occurrences and using
function.
#include <stdio.h>
int main()
{
int array[100], search, c, n;
Output of program:
In the code below we will print all the locations at which required element is found and also the
number of times it occur in the list.
#include <stdio.h>
int main()
{
int array[100], search, c, n, count = 0;
return 0;
}
Output of code:
#include <stdio.h>
int main()
{
long array[100], search, c, n, position;
if (position == -1)
printf("%d is not present in array.\n", search);
else
printf("%d is present at location %d.\n", search, position+1);
return 0;
}
return -1;
}
return -1;
}
Time required to search an element using linear search algorithm depends on size of list. In best
case element is present at beginning of list and in worst case element is present at the end. Time
complexity of linear search is O(n).
C program for binary search
C program for binary search: This code implements binary search in c language. It can only be
used for sorted arrays, but it's fast as compared to linear search. If you wish to use binary search
on an array which is not sorted then you must sort it using some sorting technique say merge sort
and then use binary search algorithm to find the desired element in the list. If the element to be
searched is found then its position is printed.
The code below assumes that the input numbers are in ascending order.
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
Output of program:
Binary search is faster than linear search but list should be sorted, hashing is faster than binary
search and perform searches in constant time.
C program to reverse an array: This program reverses the array elements. For example if a is an
array of integers with three elements such that
a[0] = 1
a[1] = 2
a[2] = 3
Then on reversing the array will be
a[0] = 3
a[1] = 2
a[0] = 1
C programming code
#include <stdio.h>
int main()
{
int n, c, d, a[100], b[100];
/*
* Copying elements into array b starting from end of array a
*/
/*
* Copying reversed array into original.
* Here we are modifying original array, this is optional.
*/
return 0;
}
#include <stdio.h>
int main() {
int array[100], n, c, t, end;
scanf("%d", &n);
end = n - 1;
end--;
}
return 0;
}
C program to reverse an array using pointers
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, c, *pointer;
scanf("%d",&n);
pointer = (int*)malloc(sizeof(int)*n);
reverse_array(pointer, n);
free(pointer);
return 0;
}
s = (int*)malloc(sizeof(int)*n);
if( s == NULL )
exit(EXIT_FAILURE);
free(s);
}
Array is passed to function and a new array is created and contents of passed array (in reverse
order) are copied into it and finally contents of new array are copied into array passed to
function.
This code will insert an element into an array, For example consider an array a[10] having three
elements in it initially and a[0] = 1, a[1] = 2 and a[2] = 3 and you want to insert a number 45 at
location 1 i.e. a[0] = 45, so we have to move elements one step below so after insertion a[1] = 1
which was a[0] initially, and a[2] = 2 and a[3] = 3. Array insertion does not mean increasing its
size i.e array will not be containing 11 elements.
C programming code
#include <stdio.h>
int main()
{
int array[100], position, c, n, value;
array[position-1] = value;
printf("Resultant array is\n");
return 0;
}
Output of program:
This program delete an element from an array. Deleting an element does not affect the size of
array. It is also checked whether deletion is possible or not, For example if array is containing
five elements and you want to delete element at position six which is not possible.
C programming code
#include <stdio.h>
int main()
{
int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
return 0;
}
Output of program:
C program for bubble sort
C program for bubble sort: c programming code for bubble sort to sort numbers or arrange them
in ascending order. You can easily modify it to print numbers in descending order.
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
return 0;
}
#include <stdio.h>
int main()
{
long array[100], n, c, d, swap;
bubble_sort(array, n);
return 0;
}
void bubble_sort(long list[], long n)
{
long c, d, t;
t = list[d];
list[d] = list[d+1];
list[d+1] = t;
}
}
}
}
You can also sort strings using Bubble sort, it is less efficient as its average and worst case
complexity is high, there are many other fast sorting algorithms.
insertion sort in c
Insertion sort in c: c program for insertion sort to sort numbers. This code implements insertion
sort algorithm to arrange numbers of an array in ascending order. With a little modification it
will arrange numbers in descending order.
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
d--;
}
}
return 0;
}
Output of program: