C program to cyclically permute the elements of an array.

Example programs to solve the problems of Arrays in C. In This program we, cyclically permute the array elements i.e. arr[0] becomes arr[1], arr[1] becomes arr[2]…so ..on. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>

void main ()
{
int i,n,number[30];
printf("Enter the value of the n = ");
scanf ("%d", &n);
printf ("Enter the numbersn");
for (i=0; i<n; ++i)
{
scanf ("%d", &number[i]);
}
number[n] = number[0];

for (i=0; i<n; ++i)
{
number[i] = number[i+1];
}
printf ("Cyclically permted numbers are given below n");
for (i=0; i<n; ++i)
printf ("%dn", number[i]);
}
Read more Similar C Programs
Array In C

Simple C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to Demonstrate the increment and decrement operators

C program which demonstrates the working of increment(++) and decrement(–) operators. Increment operator ++ adds 1 to its operand and Decrement operator — subtracts 1 from its operand.

These operators may be used either as a prefix operator or post-fix operator. Read more here: Increment and decrement operators

The Program

/* C Program to demonstrate increment and decrement operators */
main()
{
/*
* ++i -> i incremented before i is used.
* --i -> i decremented before i is used.
* j++ -> j is incremented AFTER j has been used.
* j-- -> j is decremented AFTER j has been used.
*/
int i=1,j=1;
puts("\tDemo 1");
printf("\t%d %d\n",++i, j++); /* O/P 2 1 */
printf("\t%d %d\n",i, j); /* O/P 2 2 */
i=1; j=1;
puts("\n\tDemo 2");
printf("\t%d \n", i=j++); /* O/P 1 */
printf("\t%d \n", i=++j); /* O/P 3 */
i = 0; j = 0;
puts("\n\tDemo 3");
if ( (i++ == 1) && (j++ == 1)) puts("Some text");
/* Will i and j get incremented? The answer is NO! Because
* the expression in the left of '&&' resolves to false the
* compiler does NOT execute the expression on the right and
* so 'j' does not get executed!!!!! */
printf("\t%d %d\n",i, j); /* O/P 1 0 */
}

Sample Output

Increment and decrement operators in c

Related programs

Airthmatic Operators
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page.

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to find GCD and LCM using Recursion

C Program to find the GCD and LCM. In this program we used the Recursion method. Recursion is the programming technique that a process invoking itself again and again. Here find_gcd() and find_(lcm) are the recursive methods. for example LCM and GCD of 8,12 is 24 and 4 Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

/*
* http://www.c-program-example.com/2011/10/c-program-to-find-gcd-and-lcm-using.html
*/

/* Find this on GitHub:
* https://github.com/snadahalli/cprograms/blob/master/gcd_lcm_rec.c
*/

#include "stdio.h"
int find_gcd(int,int);
int find_lcm(int,int);
int main(){
int num1,num2,gcd,lcm;
printf("nEnter two numbers:n ");
scanf("%d %d",&num1,&num2);
gcd=find_gcd(num1,num2);

printf("nnGCD of %d and %d is: %dnn",num1,num2,gcd);

if(num1>num2)
lcm = find_lcm(num1,num2);
else
lcm = find_lcm(num2,num1);

printf("nnLCM of %d and %d is: %dnn",num1,num2,lcm);
return 0;
}

int find_gcd(int n1,int n2){
while(n1!=n2){
if(n1>n2)
return find_gcd(n1-n2,n2);
else
return find_gcd(n1,n2-n1);
}
return n1;
}

int find_lcm(int n1,int n2){

static int temp = 1;

if(temp % n2 == 0 && temp % n1 == 0)
return temp;
temp++;
find_lcm(n1,n2);

return temp;
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to print Fibonacci numbers using Recursion

C Program to print Fibonacci numbers. In this program we used the Recursion method. Recursion is the programming technique that a process invoking itself again and again. Fibonacci numbers are sequence of numbers starts from 0 and 1 , continue by adding previous number. 0,1,1,2,3,5,8,13,…….. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
int fib(int);
int f=1,fib1=0,fib2=0,i=0,j;
void main()
{
int num;
clrscr();
printf(" How many Fibonacci numbers do you want?n");
scanf("%d",&num);
printf("nFibonacci Numbers are:n");
f=0;
printf("n%dn",f);
f=1;
printf("n%dn",f);
for(j=0;j<num-2;j++)
{
f=fib(num);
printf("n%dn",f);
}
getch();
}
int fib(int n)
{

while(i<n)
{
if(i<=n)
{
i++;
fib1=fib2;
fib2=f;
f=fib2+fib1;
fib(1);
return f;
}
}

}


Read more Similar C Programs
Learn C Programming
Recursion
Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to calculate factorial using Recursive function

C Program to find the factorial of a number. In this program we used the Recursion method. Recursion is the programming technique that a process invoking itself again and again. The standard recursive function for factorial is factorial=n*fact(n-1). factorial of number denoted by ‘!’, means product of all non negative integers from 1 to number. example: 5!=5*4*3*2*1=120. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
int factorial(int);

void main()
{
int num;
printf("Enter the number to calculate Factorial :");
scanf("%d",&num);
printf("nFactorial : %d", factorial (num));

}
int factorial (int i)
{
int f;
if(i==1)
return 1;
else
f = i* factorial (i-1);
return f;
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to sort the string, using shell sort technique.

C Program to sort the string, using shell sort technique. Shell sort is the one of the oldest sorting technique, quite well for all types of arrays in c. The shell sort is a “diminishing increment sort”, better known as a “comb sort” to the unwashed programming masses. The algorithm makes multiple passes through the list, and each time sorts a number of equally sized sets using the insertion sort . The size of the set to be sorted gets larger with each pass through the list, until the set consists of the entire list. This sets the insertion sort up for an almost-best case run each iteration with a complexity that approaches O(n) . Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void shell_sort(char *chars, int c) {

register int i, j, space, k;
char x, a[5];

a[0]=9; a[1]=5; a[2]=3; a[3]=2; a[4]=1;

for(k=0; k < 5; k++) {
space = a[k];
for(i=space; i < c; ++i) {
x = chars[i];
for(j=i-space; (x < chars[j]) && (j >= 0); j=j-space)
chars[j+space] = chars[j];
chars[j+space] = x;
}
}
}

int main() {
char string[300];
printf("Enter a string:");
gets(string);
shell_sort(string, strlen(string));
printf("The sorted string is: %s.n", string);
return 0;
}
Read more Similar C Programs
Array In C

Sorting Techniques

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to implement MERGE sort.

Merge sort is based on the divide conquer strategy. Array is divided in to two halves.if the array length is n, then it is divided into n/2,n/4,n/8…. and each part is sorted independently, then conquered into the sorted array. The efficiency of merge sort is O(n log n). Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/



#include <stdio.h>
#include <stdlib.h>

#define MAX_ARY 10

void merge_sort(int x[], int end, int start);

int main(void) {
int ary[MAX_ARY];
int j = 0;

printf("nnEnter the elements to be sorted: n");
for(j=0;j<MAX_ARY;j++)
scanf("%d",&ary[j]);

/* array before mergesort */
printf("Before :");
for(j = 0; j < MAX_ARY; j++)
printf(" %d", ary[j]);

printf("n");

merge_sort(ary, 0, MAX_ARY - 1);

/* array after mergesort */
printf("After Merge Sort :");
for(j = 0; j < MAX_ARY; j++)
printf(" %d", ary[j]);

printf("n");
getch();
}

/* Method to implement Merge Sort*/
void merge_sort(int x[], int end, int start) {
int j = 0;
const int size = start - end + 1;
int mid = 0;
int mrg1 = 0;
int mrg2 = 0;
int executing[MAX_ARY];

if(end == start)
return;

mid = (end + start) / 2;

merge_sort(x, end, mid);
merge_sort(x, mid + 1, start);

for(j = 0; j < size; j++)
executing[j] = x[end + j];

mrg1 = 0;
mrg2 = mid - end + 1;

for(j = 0; j < size; j++) {
if(mrg2 <= start - end)
if(mrg1 <= mid - end)
if(executing[mrg1] > executing[mrg2])
x[j + end] = executing[mrg2++];
else
x[j + end] = executing[mrg1++];
else
x[j + end] = executing[mrg2++];
else
x[j + end] = executing[mrg1++];
}
}
Read more Similar C Programs
Array In C

Sorting Techniques

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to implement Insertion sort.

Insertion sorting technique is the elementary sorting technique. Insertion sort sorts one element at a time, It is just like manual sorting by humans. Insertion sort is better for small set of elements. Insertion sort is slower than heap sort, shell sort, quick sort,and merge sort. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include<stdio.h>
#include<conio.h>

void inst_sort(int []);

void main()
{
int num[5],count;
clrscr();
printf("nEnter the Five Elements to sort:n");

for (count=0;count<5;count++)
scanf("%d",&num[count]);
inst_sort(num);

printf("nnElements after sorting: n");
for(count=0;count<5;count++)
printf("%dn",num[count]);
getch();
}

// Function for Insertion Sorting
void inst_sort(int num[])
{
int i,j,k;
for(j=1;j<5;j++)
{
k=num[j];
for(i=j-1;i>=0 && k<num[i];i--)
num[i+1]=num[i];
num[i+1]=k;
}
}
Read more Similar C Programs
Array In C

Sorting Techniques

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to sort a linked list.

Data structures using C, Linked list is a data structure in which the objects are arranged in a linear order. In this program, we sort the list elements in ascending order. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/




#include <stdio.h>
#include <stdlib.h>
#define NULL 0

struct linked_list
{
int number;
struct linked_list *next;
};
typedef struct linked_list node;

main ()
{
int n;
node *head = NULL;
void print(node *p);
node *insert_Sort(node *p, int n);

printf("Input the list of numbers.n");
printf("At end, type -999.n");
scanf("%d",&n);

while(n != -999)
{
if(head == NULL) /* create 'base' node */
{
head = (node *)malloc(sizeof(node));
head ->number = n;
head->next = NULL;

}

else /* insert next item */
{
head = insert_Sort(head,n);
}
scanf("%d", &n);
}
printf("n");
print(head);
print("n");
}
node *insert_Sort(node *list, int x)
{
node *p1, *p2, *p;
p1 = NULL;
p2 = list; /* p2 points to first node */

for( ; p2->number < x ; p2 = p2->next)
{
p1 = p2;

if(p2->next == NULL)
{
p2 = p2->next; /* p2 set to NULL */
break; /* insert new node at end */
}
}

/* key node found */
p = (node *)malloc(sizeof(node)); /* space for new node */
p->number = x; /* place value in the new node */
p->next = p2; /* link new node to key node */
if (p1 == NULL)
list = p; /* new node becomes the first node */
else
p1->next = p; /* new node inserted after 1st node */
return (list);
}
void print(node *list)
{
if (list == NULL)
printf("NULL");
else
{
printf("%d-->",list->number);
print(list->next);
}
return;
}
Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to reverse the first n characters in a file.

C Program to reverse the first n characters in a file using the command line arguments. Here we read the file name and n are specified on the command line. If file exists, it reverse the n characters, else it gives the error message. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>

void main(int argc, char *argv[])
{
char a[15];
char s[20];
char n;
int k;
int j=0;
int i;
int len;
FILE *fp;

if(argc!=3)
{
puts("Improper number of arguments.");
exit(0);
}
fp = fopen(argv[1],"r");
if(fp == NULL)
{
puts("File cannot be opened.");
exit(0);
}

k=*argv[2]-48;
n = fread(a,1,k,fp);
a[n]='';
len=strlen(a);
for(i=len-1;i>=0;i--)
{
s[j]=a[i];
printf("%c",s[j]);
j=j+1;
}
s[j+1]='';
getch();
}/*(Note: The file name and n are specified on the command line.)*/

Read more Similar C Programs
File operations

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com