Professional Documents
Culture Documents
C Programming
C Programming
C Programming
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Enter the size of array: ");
scanf("%d", &n);
int *arr = (int *)malloc(n * sizeof(int));
// input array
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// print array
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
getch();
return 0;
}
1|Page
(Program 2)
Program to find smallest element in an array of
integers
#include <stdio.h>
int main()
{
int arr[] = {2, 5, -5, 15, 0};
int n = sizeof(arr) / sizeof(int);
return 0;
}
2|Page
(Program 3)
Program to swap the smallest and the largest
element in an array of integers
#include <stdio.h>
// find largest
int largest = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] > arr[largest])
largest = i;
}
// swap smallest and largest element
int temp = arr[smallest];
arr[smallest] = arr[largest];
arr[largest] = temp;
}
int main()
{
int arr[] = {9, 4, 8, -10, 1, 21};
int n = sizeof(arr) / sizeof(int);
3|Page
{
printf("%d ", arr[i]);
}
printf("\n");
swapLargestSmallest(arr, n);
printf("After Swap\n");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
4|Page
(Program 4)
Program to Find second largest element in an array
of integers
#include <stdio.h>
#include <limits.h>
int main()
{
int arr[] = {3, 1, 19, 2, 15};
5|Page
int n = sizeof(arr) / sizeof(int);
return 0;
}
6|Page
(Program 5)
Program to Find whether an array of integers
contains a duplicate element
#include <stdio.h>
#include <stdbool.h>
int main()
{
int arr[] = {2, 3, 1, 9, 4, 1};
int n = sizeof(arr) / sizeof(int);
if (hasduplicateElements(arr, n))
printf("Array has duplicate Elements");
else
printf("Array doesn't have duplicate Elements");
return 0;
}
7|Page
(Program 6)
Program to Insert an element at a specific location
in an array of integers
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n = 7;
int *arr = (int *)calloc(n, sizeof(int));
int pos;
8|Page
int val;
printf("\nEnter position & number to be inserted:
");
scanf("%d", &pos);
scanf("%d", &val);
if (pos < 1 || pos > n + 1)
{
printf("Enter valid position!!");
exit(1);
}
insertElement(arr, &n, pos, val);
printf("\nAfter Inserting element\n");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
free(arr);
return 0;
}
9|Page
(Program 7)
Program to Insert an element in a sorted array of
integers
arr[pos - 1] = num;
++*size;
}
int main()
{
int n = 6;
int *arr = (int *)malloc(n * sizeof(int));
10 | P a g e
arr[0] = 2;
arr[1] = 9;
arr[2] = 11;
arr[3] = 15;
arr[4] = 18;
arr[5] = 20;
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
int num;
printf("\nEnter the value to be inserted: ");
scanf("%d", &num);
insertNumSorted(arr, &n, num);
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
free(arr);
return 0;
}
11 | P a g e
(Program 8)
Program to Delete an element from a specific
position in an array
#include <stdio.h>
#include <stdlib.h>
--*n;
}
int main()
{
int arr[] = {2, 8, 9, 1, 5, 14};
int n = sizeof(arr) / sizeof(int);
int pos;
printf("\nEnter position of element to be deleted:
");
scanf("%d", &pos);
if (pos < 1 || pos > n)
{
printf("Enter valid position!!");
exit(1);
12 | P a g e
}
deleteElement(arr, &n, pos);
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
13 | P a g e
(Program 9)
Program to Delete an element in a sorted array of
integers
#include <stdio.h>
#include <stdlib.h>
if (pos == 0)
pos = *n;
--*n;
}
int main()
{
int arr[] = {1, 2, 5, 8, 9, 14};
int n = sizeof(arr) / sizeof(int);
14 | P a g e
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
int num;
printf("\nEnter the number to be deleted: ");
scanf("%d", &num);
return 0;
}
15 | P a g e
(Program 10)
Program to merge two sorted arrays
while(j!=m)
{
arr3[index++]=arr2[j++];
}
}
int main()
{
int arr1[] = {1, 4, 7, 9, 11};
int n = sizeof(arr1) / sizeof(int);
16 | P a g e
printf("Array1: ");
for (int i = 0; i < n; i++)
{
printf("%d ", arr1[i]);
}
int arr3[10];
printf("\n\nMerged Array: ");
mergeArrays(arr1, n, arr2, m, arr3);
for (int i = 0; i < n + m; i++)
{
printf("%d ", arr3[i]);
}
return 0;
}
17 | P a g e
(Program 11)
Program to Implement Linear Search
// program to search an element in an array
#include <stdio.h>
return -1;
}
int main()
{
int arr[] = {2, 3, 9, 1, 13, 5};
int n = sizeof(arr) / sizeof(int);
int target;
printf("Enter the target to be searched: ");
scanf("%d", &target);
int targetIndex = searchTarget(arr, n, target);
if (targetIndex == -1)
printf("Element doesn't exist");
else
printf("%d is at %dth position", target,
targetIndex);
return 0;
}
18 | P a g e
(Program 12)
Program to read and print an array of N numbers
using function
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Enter the size of array: ");
scanf("%d", &n);
int *arr = (int *)malloc(n * sizeof(int));
inputArray(arr, n);
printArray(arr, n);
19 | P a g e
return 0;
}
20 | P a g e
(Program 13)
Program to swap the largest and the smallest
numbers in an array using function
#include <stdio.h>
// find largest
int largest = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] > arr[largest])
largest = i;
}
// swap smallest and largest element
int temp = arr[smallest];
arr[smallest] = arr[largest];
arr[largest] = temp;
}
int main()
{
int arr[] = {9, 4, 8, -10, 1, 21};
int n = sizeof(arr) / sizeof(int);
21 | P a g e
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
swapLargestSmallest(arr, n);
return 0;
}
22 | P a g e
(Program 14)
Program to print the elements of a 2-D array
#include <stdio.h>
int main()
{
int arr[][4] = {{1, 2, 3, 4},
{5, 9, 4, 4},
{0, -1, 6, 4}};
return 0;
}
23 | P a g e
(Program 15)
Program to generate pascal triangle
#include <stdio.h>
denm *= i;
}
return (numr / denm);
}
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
24 | P a g e
for (int j = 0; j <= i; j++)
{
printf("%d ",nCr(i,j));
}
printf("\n");
}
return 0;
}
25 | P a g e
(Program 16)
Program to find the length of a string
int main()
{
char string1[5];
printf("Enter the string: ");
gets(string1);
printf("\nstring1: %s\n", string1);
printf("\nLength of string string1: %d\n",
strlen(string1));
return 0;
}
26 | P a g e
(Program 17)
Program to convert characters of a string to upper
case
#include <stdio.h>
#include <string.h>
int main()
{
char string1[100];
printf("Enter the string: ");
gets(string1);
printf("\nstring1: %s\n", string1);
strupr(string1);
printf("\nconverting string1 to uppercase: \n");
puts(string1);
return 0;
}
27 | P a g e
(Program 18)
Program to convert characters of a string to lower
case
#include <stdio.h>
#include <string.h>
int main()
{
char string1[100];
printf("Enter the string: ");
gets(string1);
printf("\nstring1: %s\n", string1);
strlwr(string1);
printf("\nconverting string1 to lowercase: \n");
puts(string1);
return 0;
}
28 | P a g e
(Program 19)
Program to concatenate two strings
#include <stdio.h>
#include <string.h>
int main()
{
char string1[20] = "Shailav ";
printf("string1: %s\n", string1);
char string2[20] = "Malik";
printf("string2: %s\n", string2);
strcat(string1, string2);
printf("\nAfter concatenation,\nstring1: %s",
string1);
return 0;
}
29 | P a g e
(Program 20)
Program to append a string to another string
#include <stdio.h>
#include <string.h>
int main()
{
char string1[20] = "Ram";
printf("string1: %s\n", string1);
char string2[20] = "aman";
printf("string2: %s\n", string2);
puts(string1);
return 0;
}
30 | P a g e
(Program 21)
Program to compare two strings
#include <stdio.h>
#include <string.h>
int main()
{
char string1[20] = "Shailav";
printf("string1: %s\n", string1);
char string2[20] = "Shailove";
printf("string2: %s\n", string2);
int c=strcmp(string1,string2);
if (!c)
printf("\nBoth strings are same");
else
printf("\nstring1 & string2 are different");
return 0;
}
31 | P a g e
(Program 22)
Program to reverse the given string
#include <stdio.h>
#include <string.h>
int main()
{
char string1[10];
printf("Enter the string: ");
scanf("%s", &string1);
printf("string1: %s\n", string1);
strrev(string1);
printf("\nAter reversing string,\nstring1: %s",
string1);
return 0;
}
32 | P a g e
(Program 23)
Program to read and print the names of n students
of a class
#include <stdio.h>
int main()
{
int n;
printf("Enter the no. of students: ");
scanf("%d", &n);
char name[10][10];
printf("Now enter the names: \n");
for (int i = 0; i < n; i++)
{
scanf("%s", &name[i]);
}
return 0;
}
33 | P a g e
(Program 24)
Program to read a sentence and count number of
words in it
#include <stdio.h>
#include <string.h>
return words;
}
int main()
{
char string1[50];
printf("Enter the string: ");
gets(string1);
return 0;
}
34 | P a g e
(Program 25)
Program to delete last character of a string
int main()
{
char string1[50];
printf("Enter the string: ");
gets(string1);
int prevChar = -1;
for (int i = 0; string1[i] != '\0'; i++)
{
prevChar++;
}
string1[prevChar] = '\0';
printf("\nstring1 after deleting last character:
\n");
puts(string1);
return 0;
}
35 | P a g e
(Program 26)
Program to delete first character of a string
#include <stdio.h>
int main()
{
char string1[50];
printf("Enter the string: ");
gets(string1);
deleteFirstChar(string1);
printf("\nstring1 after deleting first
character:\n%s", string1);
return 0;
}
36 | P a g e
(Program 27)
Program to print “Hello World” using pointers
int main()
{
char st[] = "Hello World";
char *ptr = &st[0];
37 | P a g e
(Program 28)
Program to convert a floating-point number into an
integer using pointer
#include <stdio.h>
int main()
{
float num = 4.55;
float *ptr = #
printf("num = %f\n", *ptr);
return 0;
}
38 | P a g e
(Program 29)
Program to find biggest of three numbers using
pointer
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers: ");
scanf("%d%d%d", &a, &b, &c);
int *p1 = &a;
int *p2 = &b;
int *p3 = &c;
return 0;
}
39 | P a g e
(Program 30)
Program to test whether a number is positive,
negative or zero using pointer
#include <stdio.h>
int main()
{
int num, *ptr;
printf("Enter the number: ");
scanf("%d", &num);
ptr = #
if (*ptr > 0)
printf("positive", num);
else if (*ptr < 0)
printf("negative", num);
else
printf("zero", num);
return 0;
}
40 | P a g e
(Program 31)
Program to print all even numbers from m to n
using pointer
#include <stdio.h>
int main()
{
int m, n, *ptr,firstEven;
printf("Enter the starting and ending number: ");
scanf("%d%d", &m, &n);
ptr=&m;
// find first even number and then keep adding 2 to
it
// until it is less than n
if (*ptr & 1) // condition for odd
firstEven = *ptr + 1;
else
firstEven = *ptr;
return 0;
}
41 | P a g e
(Program 32)
Program to read and display information of a
student using structure
#include <stdio.h>
#include <string.h>
struct student
{
char name[15];
int marks;
float percent;
int rollNo;
} student1;
int main()
{
printf("Enter name: ");
gets(student1.name);
printf("Enter rollNo: ");
scanf("%d", &student1.rollNo);
printf("Enter marks: ");
scanf("%d", &student1.marks);
student1.percent = ((float)student1.marks / 600) *
100;
return 0;
}
42 | P a g e
(Program 33)
Program to find biggest of three numbers using
structure
#include <stdio.h>
struct comparison
{
int a;
int b;
int c;
};
int main()
{
struct comparison comp;
printf("Enter three numbers: ");
scanf("%d%d%d", &comp.a, &comp.b, &comp.c);
return 0;
}
43 | P a g e
(Program 34)
Program to read and display information of a
student using structure within a structure.
#include <stdio.h>
struct student
{
char name[15];
int rollNo;
// nesting structure
struct marks
{
int totalMarks;
int maxMarks;
float percent;
} m;
};
int main()
{
struct student s1;
// inputing student information
printf("Enter name: ");
gets(s1.name);
printf("Enter rollNo: ");
scanf("%d", &s1.rollNo);
44 | P a g e
// calculating percentage
s1.m.percent = ((float)s1.m.totalMarks /
s1.m.maxMarks) * 100;
return 0;
}
45 | P a g e
(Program 35)
Program to read and display information of all
students in the class using array of structures.
#include <stdio.h>
struct students
{
char name[10];
int rollNo;
char phone[10];
};
int main()
{
int n;
printf("Enter the no. of students: ");
scanf("%d", &n);
struct students s[2];
// inputing data
for (int i = 0; i < n; i++)
{
printf("Student %d:\n", i + 1);
printf("Enter name: ");
scanf("%s", &s[i].name);;
printf("Enter Roll No: ");
scanf("%d", &s[i].rollNo);
printf("Enter Mobile No: ");
scanf("%s", &s[i].phone);
printf("\n");
}
// printing data
printf("\nPrinting students details: \n");
for (int i = 0; i < n; i++)
{
46 | P a g e
printf("Student %d:\n", i + 1);
printf("Name: %s\n", s[i].name);
printf("Roll No: %d\n", s[i].rollNo);
printf("Mobile No: %s\n", s[i].phone);
printf("\n");
}
return 0;
}
47 | P a g e
(Program 36)
Program to read a file character by character, and
display it simultaneously on the screen
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr = fopen("36.txt", "r");
if (fptr == NULL)
{
printf("File doesn't exist\n");
return -1;
}
return 0;
}
48 | P a g e
(Program 37)
Program to count number of characters and lines
in a file
#include <stdio.h>
int main()
{
FILE *fptr = fopen("36.txt", "r");
if (fptr == NULL)
{
printf("File doesn't exist\n");
return -1;
}
int characters = 0;
int lines = 1;
characters++;
if (ch == '\n')
lines++;
}
return 0;
}
49 | P a g e
(Program 38)
Program to print the text of a file on screen by
printing the text line by line and displaying line
numbers before the text in each line & Using
command line argument to enter the filename
#include <stdio.h>
return 0;
}
50 | P a g e
(Program 39)
Program to copy one file into another (copy one
character at a time)
// Program to copy 36.txt to new.txt
#include <stdio.h>
int main()
{
printf("Opening file 36.txt in read mode \n");
FILE *fptr1 = fopen("36.txt", "r");
if (fptr1 == NULL)
{
printf("File doesn't exist\n");
return -1;
}
printf("Opening file new.txt in write mode \n");
FILE *fptr2 = fopen("new.txt", "w");
fclose(fptr1);
printf("file 36.txt closed successfully\n");
fclose(fptr2);
fclose(fptr2);
printf("file new.txt closed successfully\n");
return 0;
}
51 | P a g e
(Program 40)
Program to print half pyramid of *
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
return 0;
}
52 | P a g e
(Program 41)
Program to print half pyramid of numbers
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
return 0;
}
53 | P a g e
(Program 42)
Program to print half pyramid of alphabets
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
return 0;
}
54 | P a g e
(Program 43)
Program to print inverted half pyramid of numbers
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
return 0;
}
55 | P a g e
(Program 44)
Program to print full pyramid of *
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
return 0;
}
56 | P a g e
(Program 45)
Program to print inverted full pyramid of *
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
return 0;
}
57 | P a g e