You are on page 1of 29

Source Code to Calculate Factorial Using Recursion

include<stdio.h>
int factorial(int n);
int main()
{
int n;
printf("Enter an positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, factorial(n));
return 0;
}
int factorial(int n)
{
if(n!=1)
return n*factorial(n-1);
}

25. C program to calculate factorial using recursion
#include<stdio.h>
int fact(int);
int main(){
int num,f;
printf("\nEnter a number: ");
scanf("%d",&num);
f=fact(num);
printf("\nFactorial of %d is: %d",num,f);
return 0;
}

int fact(int n){
if(n==1)
return 1;
else
return(n*fact(n-1));
}


24. Code for Program to insert and delete a node from the binary search tree in C
Programming
#include <stdio.h>
#include <conio.h>
#include <alloc.h>

#define TRUE 1
#define FALSE 0

struct btreenode
{
struct btreenode *leftchild ;
int data ;
struct btreenode *rightchild ;
} ;

void insert ( struct btreenode **, int ) ;
void delete ( struct btreenode **, int ) ;
void search ( struct btreenode **, int, struct btreenode **,
struct btreenode **, int * ) ;
void inorder ( struct btreenode * ) ;

void main( )
{
struct btreenode *bt ;
int req, i = 0, num, a[ ] = { 11, 9, 13, 8, 10, 12, 14, 15, 7 } ;

bt = NULL ; /* empty tree */


clrscr( ) ;

while ( i <= 8 )
{
insert ( &bt, a[i] ) ;
i++ ;
}
clrscr( ) ;
printf ( "Binary tree before deletion:\n" ) ;
inorder ( bt ) ;

delete ( &bt, 10 ) ;
printf ( "\nBinary tree after deletion:\n" ) ;
inorder ( bt ) ;

delete ( &bt, 14 ) ;
printf ( "\nBinary tree after deletion:\n" ) ;
inorder ( bt ) ;

delete ( &bt, 8 ) ;
printf ( "\nBinary tree after deletion:\n" ) ;
inorder ( bt ) ;

delete ( &bt, 13 ) ;
printf ( "\nBinary tree after deletion:\n" ) ;
inorder ( bt ) ;
}

/* inserts a new node in a binary search tree */
void insert ( struct btreenode **sr, int num )
{
if ( *sr == NULL )
{
*sr = malloc ( sizeof ( struct btreenode ) ) ;

( *sr ) -> leftchild = NULL ;
( *sr ) -> data = num ;
( *sr ) -> rightchild = NULL ;
}
else/* search the node to which new node will be attached */

{
/* if new data is less, traverse to left */
if ( num < ( *sr ) -> data )
insert ( &( ( *sr ) -> leftchild ), num ) ;
else/* else traverse to right */

insert ( &( ( *sr ) -> rightchild ), num ) ;
}
}











23. C programming code for Queue. This program will Insert and Delete elements in
Queue
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int q[25], n ,front=-1 , rear=-1 , item;
void insertion()
{

if((rear==n) && (front==(rear+1))||(front==rear+1))
{
printf(\nQueue Overflow\n);
}

else if (rear==0)
front = rear = 1;

else if(rear==n)
rear=1;

else
rear=rear+1;

printf(Enter the item : );
scanf(%d,&item);
q[rear] = item;
printf(%d is inserted\n\n,item);

}

void deletion()
{
if(front==0)
{
printf(\nQueue Underflow\n\n);

}

item=q[front];

if(front==rear)
{
front=0;
rear=0;
}

else if (front=n)
front=1;

else
front=front+1;

printf(\n%d is deleted\n\n,item);
}

void show()
{
for(int i=0;i<=rear;i++)
printf(%d\t,q[i]);
}



int main()
{
int op;

printf(Enter the size of the queue : );
scanf(%d,&n);

do
{
printf(\n1 : Insert);
printf(\n2 : Delete);
printf(\n3 : Print);
printf(\n4 : Exit);
printf(\nEnter your choice : );
scanf(%d,&op);

switch(op)
{
case 1:
insertion();
break;

case 2:
deletion();
break;

case 3:
show();
break;


//default:
// printf(Invalid Option. Try again.);

}
}while(op!=4);

printf(\nTHE END\n);
}
OUTPUT


22. C Program to implement Stack Operations Using Stack

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
/* stack structure*/
struct stack {
int s[size];
int top;
}st;
//-------------------------------------
int stfull()
{
if(st.top>=size-1)
return 1;
else
return 0;
}
//-------------------------------------
void push(int item)
{
st.top++;
st.s[st.top] =item;
}
//-------------------------------------
int stempty()
{
if(st.top==-1)
return 1;
else
return 0;
}
//-------------------------------------
int pop()
{
int item;
item=st.s[st.top];
st.top--;
return(item);
}
//-------------------------------------
void display()
{
int i;
if(stempty())
printf("n Stack Is Empty!");
else
{
for(i=st.top;i>=0;i--)
printf("n%d",st.s[i]);
}
}
//-------------------------------------
void main(void)
{
int item,choice;
char ans;
st.top=-1;
clrscr();
printf("ntt Implementation Of Stack");
do
{
printf("n Main Menu");
printf("n1.Pushn2.Popn3.Displayn4.exit");
printf("n Enter Your Choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("n Enter The item to be pushed");
scanf("%d",&item);
if(stfull())
printf("n Stack is Full!");
else
push(item);
break;
case 2:
if(stempty())
printf("n Empty stack!Underflow !!");
else
{
item=pop();
printf("n The popped element is %d",item);
}
break;
case 3:
display();
break;
case 4:
exit(0);
}
printf("n Do You want To Continue?");
ans=getche();
}while(ans =='Y'||ans =='y');
getch();
}

21. C program for binary search

#include <stdio.h>

int main()
{
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");
scanf("%d",&n);

printf("Enter %d integers\n", n);

for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);

printf("Enter value to find\n");
scanf("%d",&search);

first = 0;
last = n - 1;
middle = (first+last)/2;

while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);

return 0;
}

Output of program:




20. Bubble sort algorithm in c
/* Bubble sort code */

#include <stdio.h>

int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elements\n");
scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)
scanf("%d", &array[c]);

for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);

return 0;
}
Output of program:







19. write a c program to create display delete insert in a singly linked list
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define NULL '0'
struct n {
int info;
struct n *next;
};
typedef struct n node;
main()
{
node *first=NULL;
int ch;
node *create();
node *insert();
node *del();
void display();
clrscr();
do
{
printf("\n1.Create the linked list");
printf("\n2.Insert a new node");
printf("\n3.Delete a node at specified position");
printf("\n4.Display all elements of list");
printf("\n5.Quit");
printf("\nSelect any one of the above==>");
scanf("%d",&ch);
switch(ch)
{
case 1: first=create(first);
display(first);
break;
case 2: first=insert(first);
display(first);
break;
case 3: first=del(first);
display(first);
break;
case 4: display(first);
break;
default:printf("\nInvalid Choice! Please try again");
}
}while(ch!=5);
}
node *create(node *first)
{
node *temp,*prev=NULL,*ne;
int item;
first=prev;
printf("\nEnter the data & -999 to exit:");
scanf("%d",&item);
while(item!=-999)
{
temp=(node*)malloc(sizeof(node));
temp->next=NULL;
temp->info=item;
if(first==NULL)
first=temp;
else
{
prev=first;
for(ne=first;(ne!=NULL && ne->info<item);ne=ne->next)
prev=ne;
if(prev==first && ne==first)
{
first=temp;
temp->next=prev;
}
else
{
prev->next=temp;
temp->next=ne;
}
}
printf("\nEnter the data & -999 to exit:");
scanf("%d",&item);
}
return(first);
}
node *insert(node *first)
{
node *temp,*prev,*ne;
int item;
printf("\nEnter the data:");
scanf("%d",&item);
temp=(node*)malloc(sizeof(node));
temp->next=NULL;
temp->info=item;
if(first==NULL)
first=temp;
else
{
prev=first;
for(ne=first;(ne!=NULL && ne->info<item);ne=ne->next)
prev=ne;
if(prev==first && ne==first)
{
first=temp;
temp->next=prev;
}
else
{
prev->next=temp;
temp->next=ne;
}
}
return(first);
}
node *del(node *first)
{
node *temp,*prev;
int i=1,pos;
printf("Enter the position to be deleted:");
scanf("%d",&pos);
prev=first;
for(temp=first;(temp!=NULL && i<pos);temp=temp->next)
{
prev=temp;
i++;
}
if(prev==first && temp==first)
first=first->next;
if(temp!=NULL)
prev->next=temp->next;
else
printf("\nThe element was not found");
return(first);
}
void display(node *first)
{
node *temp;
for(temp=first;temp!=NULL;temp=temp->next)
{
printf("%d",temp->info);
if(temp->next!=NULL)
printf("====>");
}
getch();
}

17. Write program in c to sort array of string using pointers? *
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void print2d(char **, size_t);
void selectionSort2d(char **, size_t);

char *names[] = {"John", "Joe", "Genevieve", "Mark", "Luke", "Jane" };

int main(int argc, char *argv[]) {
char **strArray;
size_t i, len = sizeof(names) / sizeof(char *);

/* Allocate */
strArray = calloc(len,sizeof(char*));
for (i = 0; i < len; i++) {
strArray[i] = calloc(strlen(names[i]),sizeof(char));
strcpy(strArray[i],names[i]);
}

/* Display */
printf("\nUnsorted:\n");
print2d(strArray,len);

/* Sort */
selectionSort2d(strArray,len);

/* Display */
printf("\nSorted:\n");
print2d(strArray,len);

/* Free */
for (i = 0; i < len; i++) {
free(strArray[i]);
}
return 0;
}

void selectionSort2d(char **a, size_t rows) {
int i, j, min;
char *t;

for (i = 0; i < rows; i++) {
min = i;
for (j = i+1; j < rows; j++) {
if (strcmp(a[j],a[min]) < 0) {
min = j;
}
}
t = a[min];
a[min] = a[i];
a[i] = t;
}
}

void print2d(char **a, size_t rows) {
size_t i;

for (i = 0; i < rows; i++) {
printf("%s\n",a[i]);
}
}






/16. *C program that outputs minimum, maximum and average of integers*/

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

void main()
{
int i,a[5],min,max;
float avg;
printf("\n Enter any number : ");
scanf("%d",&a[0]);
max=a[0];
min=a[0];
avg=0;
for(i=1; i++;)
{
printf("\n Enter any0 Number : ");
scanf("%d",&a[i]);
if(a[i]>max)
{
max=a[i];
}
else
{
min=a[i];
}
avg=avg+a[i];

}

avg=avg/5;

printf("\n The minimum number is %d",min);
printf("\n The maximum number is %d",max);
printf("\n The average is %f",avg);

getch();

}







C Program to Multiply Two 3 X 3 Matrices

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

void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k;
int sum=0;
clrscr();


printf("nEnter First Matrix : n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}

printf("nEnter Second Matrix:n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
}

printf("The First Matrix Is:n");

for(i=0;i<3;i++)//print the first matrix
{
for(j=0;j<3;j++)
printf(" %d ",a[i][j]);
printf("n");
}

printf("The Second Matrix Is:n");

for(i=0;i<3;i++) // print the second matrix
{
for(j=0;j<3;j++)
printf(" %d ",b[i][j]);
printf("n");
}


for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
{
sum = 0;
for(k=0;k<=2;k++)
sum = sum + a[i][k] * b[k][j];
c[i][j]=sum;
}

printf("nMultiplication Of Two Matrices :nn");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf(" %d ",c[i][j]);
printf("n");
}

getch();
}


14. c program to add two matrix
#include <stdio.h>

int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];

printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);

printf("Enter the elements of second matrix\n");

for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &second[c][d]);

for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];

printf("Sum of entered matrices:-\n");

for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
printf("%d\t", sum[c][d]);

printf("\n");
}

return 0;
}



13. Write a c program to check given string is palindrome number or not

#include<string.h>
#include<stdio.h>
int main(){
char *str,*rev;
int i,j;
printf("\nEnter a string:");
scanf("%s",str);
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
if(strcmp(rev,str))
printf("\nThe string is not a palindrome");
else
printf("\nThe string is a palindrome");
return 0;
}









12. write a c program to accept a name and display the initials and surname
/*user types first name and lastr name. Output in specific format*/

#include <stdio.h>
#include <ctype.h>

int main()
{
char first_name, last_name,x,y,soak_up;

printf("Enter your first and last name: ");

while ((first_name = getchar()) == ' ')
soak_up=first_name; //soak up input, if the words
BEFORE the first name has spaces

x=toupper(first_name); //obtain first letter
and store in unrelated variable for later printing

while(first_name != ' ')
first_name = getchar(); //obtain the rest of the
first name

while ((last_name = getchar()) == ' ')
soak_up=last_name; //if there is any space after
the first name has been typed, soak up these inputs

y=toupper(last_name); //obtain first letter
and store in unrelated variable for later printing

while(last_name != '\n' && last_name != ' ')
{
printf("%c", last_name);
last_name=getchar();
}

printf(", %c", x);

getchar();getchar();
return 0;
}







10. Source Code to Check Armstrong Number

/* C program to check whether a number entered by user is Armstrong or not. */

#include <stdio.h>
int main()
{
int n, n1, rem, num=0;
printf("Enter a positive integer: ");
scanf("%d", &n);
n1=n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(num==n)
printf("%d is an Armstrong number.",n);
else
printf("%d is not an Armstrong number.",n);
}
Output
Enter a positive integer: 371
371 is an Armstrong number.









9. Source Code to Find HCF
/* C Program to Find Highest Common Factor. */
#include <stdio.h>
int main()
{
int num1, num2, i, hcf;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
for(i=1; i<=num1 || i<=num2; ++i)
{
if(num1%i==0 && num2%i==0) /* Checking whether i is a factor of both
number */
hcf=i;
}
printf("H.C.F of %d and %d is %d", num1, num2, hcf);
return 0;
}

Output
Enter two integers: 14
35
HCF of 14 and 35 is 7






1.
8.
//Program to print prime numbers within a given range
2.


3.

#include<stdio.h>
4.

main()
5.

{
6.

int i,j,n,prime;
7.

printf("\nEnter the limit\t:\t");
8.

scanf("%d",&n);
9.

printf("Prime no.s up to %d : \n",n);
10.

for(i=2; i<=n; i++)
11.

{
12.

prime=1;
13.

for(j=2; j<=i/2; j++)
14.

if(i%j == 0)
15.

{
16.

prime=0;
17.

break;
18.

}
19.

if(prime==1)
20.

printf("%d\t",i);
21.

}
22.

}





7. C code to print Fibonacci series by recursion
#include<stdio.h>

void printFibonacci(int);

int main(){

int k,n;
long int i=0,j=1,f;

printf("Enter the range of the Fibonacci series:
");
scanf("%d",&n);

printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n);

return 0;
}

void printFibonacci(int n){

static long int first=0,second=1,sum;

if(n>0){
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
printFibonacci(n-1);
}

}

Sample output:

Enter the range of the Fibonacci series: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89


6. C Program to check the given number is perfect
or not.

#include<stdio.h>
int main(){
int num,i=1,sum=0;

printf("Enter a number: ");
scanf("%d",&num);

while(i<num){
if(num%i==0)
sum=sum+i;
i++;
}
if(sum==num)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);

return 0;
}





4. Write C program to print the following pattern:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

01 #include<stdio.h>
02 int main() {
03 /* c taken for columns */
04 int i, j, c = 9, m, k;
05 for (i = 1; i <= 5; i++) {
06 /* k is used for spaces */
07 for (k = 1; k <= c; k++) {
08 printf(" ");
09 }
10
for (j = 1; j <= i; j++) { printf("%2d",
j); } for (m = j - 2; m > 0; m--) {
11 /* %2d ensures that the number
12 * is printed in two spaces
13 * for alignment */
14 printf("%2d", m);
15 }
16 printf("\n");
17 /* c is decremented by 2 */
18 c = c - 2;
19 }
20 return 0;
21 }


Output:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1


b)
1
22
333
4444
55555

#include<stdio.h>

main()
{
int n, c, k;

printf("Enter number of rows\n");
scanf("%d",&n);

for ( c = 1 ; c <= n ; c++ )
{
for( k = 1 ; k <= c ; k++ )
printf("%d", c);

printf("\n");
}

return 0;
}


a)
12345
2345
345
45
5

#include <stdio.h>

int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=i;j<=5;j++)
{
printf("%d",j);
}
printf("\n");
}

return 0;
}






c)
13579
3579
579
79
9

#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<=9;i+=2)
{
for(j=i;j<=9;j+=2)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}



/* Towers of Hanoi by recursion */

#include<stdio.h>
void toh(int,char,char,char);

int main()
{
int n=3;
toh(n,'A','B','C');
return 0;
}

void toh(int n,char a,char b,char c)
{
if(n==1)
printf("\nMoved from %c to %c",a,c);
else
{
toh(n-1,a,c,b);
toh(1,a,' ',c);
toh(n-1,b,a,c);
}
}

/* Print Armstrong Numbers upto N */
#include<stdio.h>

int main()
{
int i,j,sum,n;

printf("Please enter the value of N: ");
scanf("%d",&n);

for(i=2;i<=500;i++)
{
for(j=i,sum=n;j>=1;j=j/10)
sum=sum+(j%10)*(j%10)*(j%10);
if(sum==i)
printf("%d is Armstrong.\n",i);
}
return 0;
}

Related Links:
Check Armstrong Number

You might also like