0% found this document useful (0 votes)
9 views9 pages

Unit 4

The document provides various C programming examples, including calculating factorial using loops and recursion, generating Fibonacci series, performing linear search, implementing insertion sort, calculating square roots, reversing an array, and reversing a string. Each example includes code snippets demonstrating the functionality. Additionally, the insertion sort section outlines its advantages and disadvantages.

Uploaded by

MonikaBansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Unit 4

The document provides various C programming examples, including calculating factorial using loops and recursion, generating Fibonacci series, performing linear search, implementing insertion sort, calculating square roots, reversing an array, and reversing a string. Each example includes code snippets demonstrating the functionality. Additionally, the insertion sort section outlines its advantages and disadvantages.

Uploaded by

MonikaBansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Unit-IV

Programming in C
Factorial Program using loops

#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is:
%d",number,fact);
return 0;
}
.
Factorial Program using recursion
#include<stdio.h>
int factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
void main()
{
int number;
int fact;
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ld\n",
number, fact);
return 0;
.}
Fibonacci Series up to n terms
#include <stdio.h>
int main() {
int i, n;
int t1 = 0, t2 = 1; // initialize first and second terms
int nextTerm = t1 + t2; // initialize the next term (3rd term)
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
. return 0;
Linear Search in C
#include <stdio.h>
int main()
{
int a[10], i, item,n;
printf("\nEnter number of elements of an array:\n");
scanf("%d",&n);
printf("\nEnter elements: \n");
for (i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nEnter item to search: ");
scanf("%d", &item);
for (i=0; i<=9; i++)
if (item == a[i])
{
printf("\nItem found at location %d", i+1);
break;
}
if (i > 9)
printf("\nItem does not exist.");
return 0;
} .
Insertion Sort
Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an
unsorted list into its correct position in a sorted portion of the list. It is a stable sorting algorithm,
meaning that elements with equal values maintain their relative order in the sorted output.
Insertion sort is like sorting playing cards in your hands. You split the cards into two groups: the sorted
cards and the unsorted cards. Then, you pick a card from the unsorted group and put it in the right
place in the sorted group.

Advantages
• Simple and easy to implement.
• Stable sorting algorithm.
• Efficient for small lists and nearly sorted lists.
• Space-efficient.
Disadvantages
• Inefficient for large lists.

.
Square Root
# include <stdio.h>
# include <conio.h>
# include <math.h>
int main()
{
// Declare an integer value as input
int num;
double out;
printf(“Enter a number = ”);
scanf(“%d”, &num);
// sqrt function implementation
out = sqrt(num);
printf(“The square root of %d is: %.2lf”,num, out);
return 0;
}

.
Reverse an Array

#include<stdio.h> for(i = 0; i < n/2; i++)


{
int main()
temp = arr[i];
{ arr[i] = arr[end];
int i, n, temp; arr[end] = temp;
end--;
printf("Enter the size of the array: ");
}
scanf("%d", &n); printf("The reversed array: ");
int arr[n]; for(i = 0; i < n; i++)
{
printf("Enter the elements: ");
printf("%d ", arr[i]);
for(i = 0; i < n; i++) }
{ }
scanf("%d", &arr[i]);
}
int end = n - 1;
.
Reverse a String
#include <stdio.h>
#include <string.h>
int main()
{
// string to be reversed.
char str[100] = "string";
printf("Original String: %s\n", str);
// string length
int len = strlen(str);
// for loop
for (int i = 0, j = len - 1; i <= j; i++, j--) {
// swapping characters
char c = str[i];
str[i] = str[j];
str[j] = c;
}
printf("Reversed String: %s", str);
return 0;
}
.

You might also like